Guides and Examples

Example: Chat transcript webhook

this example contains a python flask restful https //flask restful readthedocs io/ app that receives a webhooks reference docid\ ot0nvuq i76ze m5vrvuc from kindly upon receiving the webhook payload, the app sends a request to the kindly api to get the entire chat transcript for the relevant conversation up to that point import os import pprint import requests from flask import flask from flask restful import api, resource, reqparse app = flask( name ) api = api(app) parser = reqparse requestparser() parser add argument('message', type=str) parser add argument('chat transcript api url', type=str) \# get your app key on the "connect" page in the platform kindly api key = os environ get('kindly api key') class createticketapi(resource) def post(self) """ incoming webhook from kindly the request body is going to contain several fields, but the only one we're interested in right now is ` links` and the nested field `chat` """ args = parser parse args() chat transcript api url = args links chat api response = requests get( url=chat transcript api url, headers={ 'authorization' kindly api key, } ) if api response status code != 200 \# uh oh, did you set the right key? return { 'reply' "oops, something went wrong" } """ parse the data in the response """ chat transcript = api response json() """ chat transcript should look something like this { 'chat' { 'active' true, 'bot id' 4, 'organization id' 121, 'bot name' 'webhook', 'comments' \[], 'context' {'email' 'me\@example com'}, 'created' '2017 12 20t10 18 04 558000z', 'first name' none, 'full name' none, 'gender' none, 'id' '\<chat id>', 'labels' \['label1', 'label2'], 'language code' 'en', 'last name' none, 'recipient id' '\<user id>', 'source' 'chatbubble', 'state' 'chatting', 'taken over' none, 'updated' '2017 12 20t10 18 14 955000z', 'messages' \[ { 'buttons' \[], 'created' '2017 12 20t10 18 07 912000z', 'exchange id' none, 'exchange type' none, 'from bot' false, 'id' '5a3a38dfd923ba4238c70caa', 'message' 'create ticket', 'name' 'you' }, { 'buttons' \[], 'created' '2017 12 20t10 18 08 057000z', 'exchange id' '7f8601f0 b986 4acb 92f3 87b860f551d5', 'exchange type' 'usersays', 'from bot' true, 'id' '5a3a38e0d923ba4238c70cab', 'message' 'what is your email?', 'name' 'webhook' }, { 'buttons' \[], 'created' '2017 12 20t10 18 14 938000z', 'exchange id' none, 'exchange type' none, 'from bot' false, 'id' '5a3a38e6d923ba4238c70cac', 'message' 'me\@example com', 'name' 'you' }, { 'buttons' \[], 'created' '2017 12 20t10 18 14 949000z', 'exchange id' '9c7bae1f 7bb3 4bb3 9782 1a9f77e356dc', 'exchange type' 'usersays', 'from bot' true, 'id' '5a3a38e6d923ba4238c70cad', 'message' 'ok, creating ticket now ', 'name' 'webhook' } ] } } """ """ do what you want to do with the chat transcript data """ pprint pprint(chat transcript) """ this example uses synchronous python code, returning a response to the chat client at the very end if the task you want to perform takes a little time, it might be wise to do the task asynchronously and return a response to the chat client asap """ return { \# you can leave out the reply if you want, just make sure you send a 200 ok response 'reply' "ticket created check your inbox ({email}) for updates " format(email=chat transcript\['chat']\['context']\['email']), } api add resource(createticketapi, '/create ticket/') if name == ' main ' app run(debug=true, port=3333)