forked from juliogomez/netdevops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
6_chatbot.py
46 lines (40 loc) · 1.55 KB
/
6_chatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from flask import Flask, request, json
import requests
app = Flask(__name__)
port = 5005
base_url = 'https://webexapis.com/v1/'
api_key = '<bot_token>'
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
@app.route('/', methods=['POST'])
def index():
data = request.get_json()
print(json.dumps(data,indent=4))
bot_id = requests.get(f'{base_url}/people/me', headers=headers).json().get('id')
if bot_id == data.get('data').get('personId'):
return 'Message from self ignored'
else:
message_id = data.get('data').get('id')
message_url = f'{base_url}/messages/{message_id}'
message_text = requests.get(message_url, headers=headers).json().get('text')
print(message_text)
room_id = data.get('data').get('roomId')
if message_text.startswith('/chuck'):
try:
category = message_text.split()[1]
web_server = f'https://api.chucknorris.io/jokes/random?category={category}'
except IndexError:
web_server = 'https://api.chucknorris.io/jokes/random'
reply = requests.get(web_server, headers=headers).json().get('value')
else:
reply = f'You said: "{message_text}"'
my_msg_data = {
"roomId": room_id,
"text": reply,
}
post_message_url = f'{base_url}/messages'
post_message_data = requests.post(post_message_url,headers=headers,data=json.dumps(my_msg_data))
return data
app.run(host="0.0.0.0", port=port, debug=True)