|
1 | | -# [Botanalytics](https://botanalytics.co) - Conversational analytics & engagement tool for chatbots |
| 1 | +# Python SDK for [Botanalytics](https://botanalytics.co) |
2 | 2 |
|
3 | | -Python SDK currently supports |
| 3 | +> This version of our SDK is only compatible with the newest version of Botanalytics, currently on private beta. |
| 4 | +> If you are using an older version, please use the SDK version 2.x. |
4 | 5 |
|
5 | 6 |
|
6 | | -* [Google Assistant](https://botanalytics.co/docs#google-assistant) |
7 | | -* [Amazon Alexa](https://botanalytics.co/docs#amazon-alexa) |
8 | | -* [Facebook](https://botanalytics.co/docs#facebook-messenger) |
9 | | -* [Slack](https://botanalytics.co/docs#slack) |
10 | | -* [Generic](https://botanalytics.co/docs#generic) |
11 | | -* [Rasa](https://botanalytics.co/docs#rasa) |
| 7 | +## Documentation |
12 | 8 |
|
13 | | -If you want to use nodejs instead, checkout [Botanalytics Node.js SDK](https://github.com/Botanalyticsco/botanalytics) |
14 | | - |
15 | | -If you want to use ruby instead, checkout [Botanalytics Ruby SDK](https://github.com/Botanalyticsco/botanalytics-ruby) |
16 | | - |
17 | | -## Setup |
18 | | - |
19 | | -Create a free account at [https://www.botanalytics.co](https://www.botanalytics.co) and get a Token. |
20 | | - |
21 | | -Botanalytics is available via pip. |
22 | | - |
23 | | -```bash |
24 | | -pip install botanalytics |
25 | | -``` |
26 | | - |
27 | | -##### Google Assistant |
28 | | -```python |
29 | | -from botanalytics.google import GoogleAssistant |
30 | | -from botanalytics.google import GoogleAssistantCloudFunctions |
31 | | -import os |
32 | | - |
33 | | -# Optional callback function, if you specify it, you can handle failed |
34 | | -# attemps the way you want |
35 | | -def err_callback(err, reason, payload): |
36 | | - pass |
37 | | - |
38 | | -# Debug(optional)->bool, token(required)->str, |
39 | | -# callback(optional)->function, is_async(optional)-> bool |
40 | | -botanalytics = GoogleAssistant( |
41 | | - debug=True, |
42 | | - token=os.environ['BOTANALYTICS_API_TOKEN'], |
43 | | - callback=err_callback, |
44 | | - is_async=True |
45 | | - ) |
46 | | -# request_payload -> dict, response_payload -> dict |
47 | | -botanalytics.log(request_payload, response_payload) |
48 | | - |
49 | | -# For Google Cloud Functions |
50 | | -# Debug(optional)->bool, token(required)->str, is_async(optional)->bool |
51 | | -botanalytics_gc = GoogleAssistantCloudFunctions( |
52 | | - debug=True, |
53 | | - token=os.environ['BOTANALYTICS_API_TOKEN'], |
54 | | - is_async=True) |
55 | | -# request_payload->dict, response_payload->dict |
56 | | -botanalytics_gc.log(request_payload, response_payload) |
57 | | - |
58 | | -``` |
59 | | - |
60 | | -##### Amazon Alexa |
61 | | -```python |
62 | | -from botanalytics.amazon import AmazonAlexa, AmazonAlexaLambda |
63 | | -import os |
64 | | - |
65 | | -# Optional callback function, if you specify it, you can handle failed |
66 | | -# attemps the way you want |
67 | | -def err_callback(err, reason, payload): |
68 | | - pass |
69 | | - |
70 | | -# Debug(optional)->bool, token(required)->str, |
71 | | -# callback(optional)->function, is_async(optional)-> bool |
72 | | -botanalytics = AmazonAlexa( |
73 | | - debug=True, |
74 | | - token=os.environ['BOTANALYTICS_API_TOKEN'], |
75 | | - callback=err_callback, |
76 | | - is_async=True) |
77 | | -# request_payload -> dict, response_payload -> dict |
78 | | -botanalytics.log(request_payload, response_payload) |
79 | | - |
80 | | -# For lambda |
81 | | -# Debug(optional)->bool, token(required)->str, is_async(optional)->bool |
82 | | -botanalytics_aal = AmazonAlexaLambda( |
83 | | - debug=True, |
84 | | - token=os.environ['BOTANALYTICS_API_TOKEN'], |
85 | | - is_async=True) |
86 | | -# request_payload->dict, response_payload->dict |
87 | | -botanalytics_aal.log(request_payload, response_payload) |
88 | | - |
89 | | -``` |
90 | | - |
91 | | -##### Facebook |
92 | | -```python |
93 | | -from botanalytics.facebook import FacebookMessenger |
94 | | -from http.server import BaseHTTPRequestHandler, HTTPServer |
95 | | -from urllib.parse import parse_qs |
96 | | -import requests |
97 | | -import json |
98 | | -import os |
99 | | - |
100 | | -# Optional callback function, if you specify it, you can handle failed |
101 | | -# attemps the way you want |
102 | | -def err_callback(err, reason, payload): |
103 | | - pass |
104 | | - |
105 | | -botanalytics_token = os.environ['BOTANALYTICS_API_TOKEN'] |
106 | | -fb_page_token = os.environ["FACEBOOK_PAGE_TOKEN"] |
107 | | -# Debug(optional)->bool, token(required)->str, fb_token(optional)-> str, |
108 | | -# callback(optional)->function, is_async(optional)-> bool |
109 | | -botanalytics = FacebookMessenger( |
110 | | - debug=True, |
111 | | - token=botanalytics_token, |
112 | | - fb_token=fb_page_token, |
113 | | - callback=err_callback, |
114 | | - is_async=True |
115 | | - ) |
116 | | - |
117 | | -class BasicRequestHandler(BaseHTTPRequestHandler): |
118 | | - def __init__(self, *args, **kwargs): |
119 | | - super(ServerHandler, self).__init__(*args, **kwargs) |
120 | | - |
121 | | - def do_POST(self): |
122 | | - if self.path == '/webhook/facebook': |
123 | | - print('Incoming facebook message') |
124 | | - self.handle_facebook_message() |
125 | | - else: |
126 | | - # Handle your paths |
127 | | - |
128 | | - def handle_facebook_message(self): |
129 | | - request_body = str( |
130 | | - self.rfile.read( |
131 | | - int(self.headers['content-length']) |
132 | | - ), |
133 | | - 'utf-8') |
134 | | - message_payload = json.loads(request_body) |
135 | | - #log incoming message |
136 | | - botanalytics.log_incoming(message_payload) |
137 | | - |
138 | | - for entry in message_payload['entry']: |
139 | | - page_id = entry['id'] |
140 | | - message_time = entry['time'] |
141 | | - for event in entry['messaging']: |
142 | | - sender_id = event['sender']['id'] |
143 | | - message_to_build= {} |
144 | | - |
145 | | - # |
146 | | - # Handle your event and build your message |
147 | | - # |
148 | | - |
149 | | - # log outgoing message |
150 | | - botanalytics.log_outgoing(message_to_build, sender_id) |
151 | | - |
152 | | - # send your message |
153 | | - response_message = { |
154 | | - "recipient": {"id":sender_id}, |
155 | | - "message": message_to_build, |
156 | | - } |
157 | | - requests.post( |
158 | | - 'https://graph.facebook.com/v2.6/me/messages?access_token='+fb_page_token, |
159 | | - json = response_message) |
160 | | - |
161 | | - self.send_response(200) |
162 | | - |
163 | | -def run(server_class=HTTPServer, handler_class=BasicRequestHandler, |
164 | | - port=8000): |
165 | | - server_address = ('', port) |
166 | | - httpd = server_class(server_address, handler_class) |
167 | | - print('Starting httpd...') |
168 | | - try: |
169 | | - httpd.serve_forever() |
170 | | - except BaseException as ki: |
171 | | - print(str(ki)) |
172 | | - |
173 | | - |
174 | | -if __name__ == "__main__": |
175 | | - run() |
176 | | - |
177 | | -``` |
178 | | - |
179 | | - |
180 | | -##### Generic |
181 | | -```python |
182 | | -from botanalytics.generic import Generic |
183 | | -import os |
184 | | - |
185 | | -# Optional callback function, if you specify it, you can handle failed |
186 | | -# attemps the way you want |
187 | | -def err_callback(err, reason, payload): |
188 | | - pass |
189 | | - |
190 | | -# Debug(optional)->bool, token(required)->str, |
191 | | -# callback(optional)->function, is_async(optional)-> bool |
192 | | -botanalytics = Generic( |
193 | | - debug=True, |
194 | | - token=os.environ['BOTANALYTICS_API_TOKEN'], |
195 | | - callback=err_callback, |
196 | | - is_async=True) |
197 | | -# message -> dict |
198 | | -botanalytics.log(message) |
199 | | - |
200 | | -``` |
201 | | - |
202 | | -##### Rasa |
203 | | - |
204 | | -There are two options to integrate Rasa with Botanalytics |
205 | | - |
206 | | -**OPTION 1: Endpoints.yml** |
207 | | - |
208 | | -**Add Botanalytics to your endpoints.yml** |
209 | | - |
210 | | -Add a line to your endpoints.yml so that rasa-core is configured to send events to Botanalytics: |
211 | | - |
212 | | -```yaml |
213 | | -event_broker: |
214 | | - type: botanalytics.rasa.Rasa |
215 | | - api_token: BOTANALYTICS_TOKEN |
216 | | - debug: true |
217 | | -``` |
218 | | -**OPTION 2: Event Broker** |
219 | | -**Include Botanalytics** |
220 | | -```python |
221 | | -from botanalytics.rasa import Rasa |
222 | | -``` |
223 | | -**Consume events with Botanalytics** |
224 | | -```python |
225 | | - |
226 | | -def _callback(ch, method, properties, body): |
227 | | - event = json.loads(body) |
228 | | - db = Rasa(token=os.environ['BOTANALYTICS_TOKEN']) |
229 | | - db.publish(event) |
230 | | - |
231 | | -``` |
232 | | - |
233 | | -##### SlackRTMApi |
234 | | -```python |
235 | | -from botanalytics.slack import SlackRTMApi |
236 | | -from slackclient import SlackClient |
237 | | -import os |
238 | | - |
239 | | -sc = SlackClient(os.environ["SLACK_API_TOKEN"]) |
240 | | - |
241 | | -# Optional callback function, if you specify it, you can handle failed |
242 | | -# attemps the way you want |
243 | | -def err_callback(err, reason, payload): |
244 | | - pass |
245 | | - |
246 | | -# Debug(optional)->bool, token(required)->str, |
247 | | -# slack_client_instance(required)-> SlackClient, |
248 | | -# callback(optional)->function, is_async(optional)-> bool |
249 | | -botanalytics = SlackRTMApi( |
250 | | - debug=True, |
251 | | - token=os.environ['BOTANALYTICS_API_TOKEN'], |
252 | | - slack_client_instance=sc, callback=err_callback, |
253 | | - is_async=True |
254 | | - ) |
255 | | -if sc.rtm_connect(): |
256 | | - while sc.server.connected is True: |
257 | | - for message in sc.rtm_read(): |
258 | | - # message -> dict |
259 | | - botanalytics.log_incoming(message) |
260 | | - # Handle incoming message |
261 | | - # . |
262 | | - # . |
263 | | - sc.rtm_send_message("welcome-test", "test") |
264 | | - botanalytics.log_outgoing("welcome-test", "test") |
265 | | - time.sleep(1) |
266 | | -else: |
267 | | - print "Connection Failed" |
268 | | - |
269 | | - |
270 | | -``` |
271 | | - |
272 | | -##### SlackEventApi |
273 | | -```python |
274 | | -from botanalytics.slack import SlackEventApi |
275 | | -from http.server import BaseHTTPRequestHandler, HTTPServer |
276 | | -from urllib.parse import parse_qs |
277 | | -import json |
278 | | - |
279 | | -# Optional callback function, if you specify it, you can handle failed |
280 | | -# attemps the way you want |
281 | | -def err_callback(err, reason, payload): |
282 | | - pass |
283 | | - |
284 | | -# Debug(optional)->bool, token(required)->str, |
285 | | -# slack_token(required)-> str, callback(optional)->function |
286 | | -# is_async(optional)-> bool |
287 | | -botanalytics = SlackEventApi( |
288 | | - debug=True, |
289 | | - token=os.environ['BOTANALYTICS_API_TOKEN'], |
290 | | - slack_token=os.environ["SLACK_API_TOKEN"], |
291 | | - callback=err_callback, |
292 | | - is_async=True |
293 | | - ) |
294 | | - |
295 | | -class BasicRequestHandler(BaseHTTPRequestHandler): |
296 | | - def __init__(self, *args, **kwargs): |
297 | | - super(ServerHandler, self).__init__(*args, **kwargs) |
298 | | - |
299 | | - def do_POST(self): |
300 | | - if self.path == '/slack/events': |
301 | | - print('Incoming event') |
302 | | - self.handle_slack_event() |
303 | | - |
304 | | - elif self.path == '/slack/interactive': |
305 | | - print('Incoming interactive event') |
306 | | - self.handle_slack_interactive() |
307 | | - else: |
308 | | - # Handle your paths |
309 | | - |
310 | | - def handle_slack_interactive(self): |
311 | | - byte_dictionary = parse_qs( |
312 | | - self.rfile.read( |
313 | | - int(self.headers['content-length'])), |
314 | | - keep_blank_values=1 |
315 | | - ) |
316 | | - interactive_payload = json.loads( |
317 | | - byte_dictionary[b'payload'][0].decode() |
318 | | - ) |
319 | | - botanalytics.log(interactive_payload) |
320 | | - # |
321 | | - # Handle interactive payload |
322 | | - # |
323 | | - self.send_response(200) |
324 | | - |
325 | | - def handle_slack_event(self): |
326 | | - request_body = str( |
327 | | - self.rfile.read( |
328 | | - int(self.headers['content-length']) |
329 | | - ), |
330 | | - 'utf-8' |
331 | | - ) |
332 | | - event_payload = json.loads(request_body) |
333 | | - botanalytics.log(event_payload) |
334 | | - # |
335 | | - #Handle event message |
336 | | - # |
337 | | - self.send_response(200) |
338 | | - |
339 | | -def run(server_class=HTTPServer, handler_class=BasicRequestHandler, |
340 | | - port=8000): |
341 | | - server_address = ('', port) |
342 | | - httpd = server_class(server_address, handler_class) |
343 | | - print('Starting httpd...') |
344 | | - try: |
345 | | - httpd.serve_forever() |
346 | | - except BaseException as ki: |
347 | | - print(str(ki)) |
348 | | - |
349 | | - |
350 | | -if __name__ == "__main__": |
351 | | - run() |
352 | | - |
353 | | -``` |
354 | | -Follow the instructions at [https://botanalytics.co/docs](https://botanalytics.co/docs) |
| 9 | +Please follow up the steps in the [Getting Started](https://beta.botanalytics.co/docs/integration/sdks/python/getting-started) section of Python SDK documentation. |
0 commit comments