Send event to intercom.ie – example python code
So as mentioned in a previous post I have been doing some python software work to integrate a client’s could application with intercom.io. The intercom REST interface is nicely designed and quite straightforward to use.
Applications send events to their intercom account (with optional event data) when their users do interesting things, intercom can be configured with rules so that messages are automatically sent in response to a particular combination of events.
It is a nice idea that shows great potential, however the intercom rules engine is quite limited and probably needs quite a bit of work before it is really useful, for example, at the time of writing, it is not possible to include any of the event data as part of the decision process of a rule.
Anyway to post an event to intercom.io you do an HTTP POST to /events passing some data, including: the event name, the user’s email address a time-stamp. You can also send optional data that will be stored with the event (but which can’t bu used as part of a messaging rule).
The following code defines a function called raise_event() which can be used like this:
raise_event('project_created', email_address, {'project_id': id, 'project_name':name})
This raises an event named ‘project_created’ with some extra data: project id and project name.
import httplib import json APP_ID = 'Place your App Id here' API_KEY = 'Place your API key here' API_URL_STUB = 'api.intercom.io' def raise_event(name, user_email, extra_data = None): return _api_post('events', {'event_name': name, 'created_at':_timestamp(), 'user_id': user_email, 'email': user_email, 'metadata': extra_data, } ) def _api_post(endpoint, data): userAndPass = b64encode(b'%s:%s' % (APP_ID, API_KEY)).decode("ascii") headers = {"content-type": 'application/json', 'Authorization' : 'Basic %s' % userAndPass} h = httplib.HTTPSConnection(API_URL_STUB) url = '/%s' % endpoint h.request('POST', url, body=json.dumps(data), headers=headers) response = h.getresponse() def _timestamp(): now = datetime.now() return long(time.mktime(now.timetuple()))
Of course this is all very interesting but for a real python API wrapper you should probably checkout python-intercom here: