-
Notifications
You must be signed in to change notification settings - Fork 61
/
3_chatbot.py
32 lines (28 loc) · 961 Bytes
/
3_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
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))
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')
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)