Kindly
Search…
Example: Chatlog webhook
This example contains a Python Flask-RESTful app that receives a webhook from Kindly. Upon receiving the webhook payload, the app sends a request to the Kindly API to get the entire chat log for the relevant conversation up to that point.
1
import os
2
import pprint
3
​
4
import requests
5
​
6
from flask import Flask
7
from flask_restful import Api, Resource, reqparse
8
​
9
app = Flask(__name__)
10
api = Api(app)
11
​
12
parser = reqparse.RequestParser()
13
parser.add_argument('message', type=str)
14
parser.add_argument('chatlog_api_url', type=str)
15
​
16
​
17
# Get your app key on the "Connect" page in the platform
18
KINDLY_API_KEY = os.environ.get('KINDLY_API_KEY')
19
​
20
​
21
class 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
chatlog_api_url = args._links.chat
31
​
32
api_response = requests.get(
33
url=chatlog_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
chatlog = api_response.json()
49
​
50
"""
51
chat log should look something like this:
52
{
53
'chat': {
54
'active': True,
55
'bot_id': 4,
56
'bot_name': 'webhook',
57
'comments': [],
58
'context': {'email': '[email protected]'},
59
'created': '2017-12-20T10:18:04.558000Z',
60
'first_name': None,
61
'full_name': None,
62
'gender': None,
63
'id': '<CHAT ID>',
64
'language_code': 'en',
65
'last_name': None,
66
'recipient_id': '<USER ID>',
67
'source': 'chatbubble',
68
'state': 'chatting',
69
'taken_over': None,
70
'updated': '2017-12-20T10:18:14.955000Z',
71
'messages': [
72
{
73
'buttons': [],
74
'created': '2017-12-20T10:18:07.912000Z',
75
'exchange_id': None,
76
'exchange_type': None,
77
'from_bot': False,
78
'id': '5a3a38dfd923ba4238c70caa',
79
'message': 'create ticket',
80
'name': 'You'
81
},
82
{
83
'buttons': [],
84
'created': '2017-12-20T10:18:08.057000Z',
85
'exchange_id': '7f8601f0-b986-4acb-92f3-87b860f551d5',
86
'exchange_type': 'usersays',
87
'from_bot': True,
88
'id': '5a3a38e0d923ba4238c70cab',
89
'message': 'What is your email?',
90
'name': 'webhook'
91
},
92
{
93
'buttons': [],
94
'created': '2017-12-20T10:18:14.938000Z',
95
'exchange_id': None,
96
'exchange_type': None,
97
'from_bot': False,
98
'id': '5a3a38e6d923ba4238c70cac',
99
'message': '[email protected]',
100
'name': 'You'
101
},
102
{
103
'buttons': [],
104
'created': '2017-12-20T10:18:14.949000Z',
105
'exchange_id': '9c7bae1f-7bb3-4bb3-9782-1a9f77e356dc',
106
'exchange_type': 'usersays',
107
'from_bot': True,
108
'id': '5a3a38e6d923ba4238c70cad',
109
'message': 'OK, creating ticket now.',
110
'name': 'webhook'
111
}
112
]
113
}
114
}
115
"""
116
​
117
"""
118
Do what you want to do with the chatlog data
119
"""
120
pprint.pprint(chatlog)
121
​
122
"""
123
This example uses synchronous Python code, returning a response to the chat client at the very end.
124
If the task you want to perform takes a little time, it might be wise to do the task asynchronously
125
and return a response to the chat client ASAP.
126
"""
127
​
128
return {
129
# You can leave out the reply if you want, just make sure you send a 200 OK response
130
'reply': "Ticket created. Check your inbox ({email}) for updates.".format(email=chatlog['chat']['context']['email']),
131
}
132
​
133
​
134
api.add_resource(CreateTicketAPI, '/create_ticket/')
135
​
136
if __name__ == '__main__':
137
app.run(debug=True, port=3333)
Copied!
Copy link