Guides and Examples
Example: Chat transcript webhook
This example contains a Python Flask-RESTful app that receives a Webhooks reference 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.
Python
1import os
2import pprint
3
4import requests
5
6from flask import Flask
7from flask_restful import Api, Resource, reqparse
8
9app = Flask(__name__)
10api = Api(app)
11
12parser = reqparse.RequestParser()
13parser.add_argument('message', type=str)
14parser.add_argument('chat_transcript_api_url', type=str)
15
16
17# Get your app key on the "Connect" page in the platform
18KINDLY_API_KEY = os.environ.get('KINDLY_API_KEY')
19
20
21class CreateTicketAPI(Resource):
22 def post(self):
23 """
24 Incoming webhook from Kindly
25 The request body is going to contain several fields,
26 but the only one we're interested in right now is `_links` and the nested field `chat`.
27 """
28
29 args = parser.parse_args()
30 chat_transcript_api_url = args._links.chat
31
32 api_response = requests.get(
33 url=chat_transcript_api_url,
34 headers={
35 'Authorization': KINDLY_API_KEY,
36 }
37 )
38
39 if api_response.status_code != 200:
40 # Uh-oh, did you set the right key?
41 return {
42 'reply': "Oops, something went wrong"
43 }
44
45 """
46 parse the data in the response
47 """
48 chat_transcript = api_response.json()
49
50 """
51 chat transcript should look something like this:
52 {
53 'chat': {
54 'active': True,
55 'bot_id': 4,
56 'organization_id': 121,
57 'bot_name': 'webhook',
58 'comments': [],
59 'context': {'email': '[email protected]'},
60 'created': '2017-12-20T10:18:04.558000Z',
61 'first_name': None,
62 'full_name': None,
63 'gender': None,
64 'id': '<CHAT ID>',
65 'labels': ['label1', 'label2'],
66 'language_code': 'en',
67 'last_name': None,
68 'recipient_id': '<USER ID>',
69 'source': 'chatbubble',
70 'state': 'chatting',
71 'taken_over': None,
72 'updated': '2017-12-20T10:18:14.955000Z',
73 'messages': [
74 {
75 'buttons': [],
76 'created': '2017-12-20T10:18:07.912000Z',
77 'exchange_id': None,
78 'exchange_type': None,
79 'from_bot': False,
80 'id': '5a3a38dfd923ba4238c70caa',
81 'message': 'create ticket',
82 'name': 'You'
83 },
84 {
85 'buttons': [],
86 'created': '2017-12-20T10:18:08.057000Z',
87 'exchange_id': '7f8601f0-b986-4acb-92f3-87b860f551d5',
88 'exchange_type': 'usersays',
89 'from_bot': True,
90 'id': '5a3a38e0d923ba4238c70cab',
91 'message': 'What is your email?',
92 'name': 'webhook'
93 },
94 {
95 'buttons': [],
96 'created': '2017-12-20T10:18:14.938000Z',
97 'exchange_id': None,
98 'exchange_type': None,
99 'from_bot': False,
100 'id': '5a3a38e6d923ba4238c70cac',
101 'message': '[email protected]',
102 'name': 'You'
103 },
104 {
105 'buttons': [],
106 'created': '2017-12-20T10:18:14.949000Z',
107 'exchange_id': '9c7bae1f-7bb3-4bb3-9782-1a9f77e356dc',
108 'exchange_type': 'usersays',
109 'from_bot': True,
110 'id': '5a3a38e6d923ba4238c70cad',
111 'message': 'OK, creating ticket now.',
112 'name': 'webhook'
113 }
114 ]
115 }
116 }
117 """
118
119 """
120 Do what you want to do with the chat_transcript data
121 """
122 pprint.pprint(chat_transcript)
123
124 """
125 This example uses synchronous Python code, returning a response to the chat client at the very end.
126 If the task you want to perform takes a little time, it might be wise to do the task asynchronously
127 and return a response to the chat client ASAP.
128 """
129
130 return {
131 # You can leave out the reply if you want, just make sure you send a 200 OK response
132 'reply': "Ticket created. Check your inbox ({email}) for updates.".format(email=chat_transcript['chat']['context']['email']),
133 }
134
135
136api.add_resource(CreateTicketAPI, '/create_ticket/')
137
138if __name__ == '__main__':
139 app.run(debug=True, port=3333)
Updated 06 Oct 2023
Did this page help you?