We have been trying to create engagements with data linked the contact in hubspot back to the customer feedback data the user has entered on to our customer feedback portal. The feedback from the end user is that these engagements are not visible on to them, even thought the API responds with success status codes.
To do this we are searching for the contact in Hubspot system and using the contacts ‘canonical-vid’ to connect it to the newly created attachment.
A sample version of our code is here:
#!/usr/bin/env python
#from event import Events
import requests
import json
import time
import datetime
class Events:
response_end = datetime.datetime.now()
event_id = 1234567
CANDIDATE_EMAIL="a@example.com"
SUNDIAL_APIKEY="***-***-***"
class HubSpotAPIError(RuntimeError):pass
class HubSpotConnector(object):
def __init__(self,apikey,oauth , **kwargs ):
self.owner = kwargs.get('owner',None)
self.apikey = apikey
self.host = kwargs.get('host',"https://api.hubapi.com")
def _do_hs_query(self,net_method,url,data = None):
query = self.host + url +"?hapikey="+self.apikey
kwargs = {}
if data is not None:
kwargs['json'] =data
response = net_method(query, **kwargs)
print query, response.reason
return response
def get_contact_json_by_email(self,email):
"""Returns a JSONPODobject representing the hubspot contact"""
base_url = "/contacts/v1/contact/email/%s/profile" % email
response = self._do_hs_query(requests.get,base_url)
doc = response.json()
try:
if doc['status'] == 'error':
raise HubSpotAPIError(doc['message'])
except KeyError:pass
return doc
def add_engagement(self,contact, txt):
base_url = "/engagements/v1/engagements"
engagement_doc = {
"engagement": {
"active": True,
"type": "NOTE",
"timestamp": int(time.mktime(time.gmtime()))
},
"associations": {
"contactIds": [ contact['canonical-vid'] ],
"companyIds": [ ],
"dealIds": [ ],
"ownerIds": [ ]
},
"metadata": {
"body": txt
}
}
if self.owner:
engagement_doc['engagement']['ownerId'] = self.owner
resp =self._do_hs_query(requests.post,base_url, data = engagement_doc)
if resp.status_code != 200:
raise HubSpotAPIError(resp.reason)
return resp
def main():
hs = HubSpotConnector(SUNDIAL_APIKEY,None)
contact = hs.get_contact_json_by_email(CANDIDATE_EMAIL)
#If not found raises and exceptionand exits
# e = Events(None,event_id)
e = Events()
note = """
GuestRate feedback submitted %(response_end)s
Link: https://bdrconline.com/cgi-bin/guestrate/report.py?e=%(event_id)i
"""%{
'event_id':e.event_id,
'response_end':e.response_end,
}
r = hs.add_engagement(contact,note)
print r.status_code,r.reason
print repr(r.text)
if __name__ == "__main__":
main()
When run we see following output.
https://api.hubapi.com/contacts/v1/contact/email/a@example.om/profile?hapikey=***-***-*** OK
https://api.hubapi.com/engagements/v1/engagements?hapikey=***-***-*** OK
200 OK
u'{"engagement":{"id":1020811462,"portalId":3274272,"active":true,"createdAt":1528971012677,"lastUpdated":1528971012677,"type":"NOTE","timestamp":1528971012},"associations":{"contactIds":[456851],"companyIds":[],"dealIds":[],"ownerIds":[],"workflowIds":[],"ticketIds":[],"contentIds":[]},"metadata":{"body":"\\n GuestRate feedback submitted 2018-06-14 10:10:11.879795\\n\\n Link: https://bdrconline.com/cgi-bin/guestrate/report.py?e=1234567\\n "}}'
Additionally using real data we captured the following HTTP transaction indicating success. Unfortunately the capture was done via a utf8 terminal an the GZIP data is scrambled. (Green sent; Red received)