-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb_bot.py
94 lines (67 loc) · 2.64 KB
/
fb_bot.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from datetime import datetime
import os
from dotenv import load_dotenv
from flask import Flask, request
from flask_apscheduler import APScheduler
from redis import Redis
from helpers.fb_action_handlers import handle_menu_caching, handle_user_input
from helpers.token_handers import AuthToken
class Config:
SCHEDULER_API_ENABLED = True
SCHEDULER_TIMEZONE = "Europe/Paris"
class FlaskApp(Flask):
load_dotenv()
client_id = os.getenv('CLIENT_ID')
client_secret = os.getenv('CLIENT_SECRET')
auth = AuthToken(client_id, client_secret)
redis_password = os.getenv('REDIS_PASSWORD', '')
redis_host = os.getenv('REDIS_HOST', 'localhost')
redis_port = os.getenv('REDIS_PORT', 6379)
db = Redis(host=redis_host, port=redis_port, password=redis_password)
fb_token = os.getenv('PAGE_ACCESS_TOKEN')
verify_token = os.getenv('VERIFY_TOKEN')
app = FlaskApp(__name__)
app.config.from_object(Config())
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
@scheduler.task('interval', minutes=1, next_run_time=datetime.now())
def cache_menu():
handle_menu_caching(app.auth, app.db)
@app.route('/', methods=['GET'])
def verify():
"""Verifies webhook for FB."""
args = request.args
if args.get('hub.mode') == 'subscribe' and args.get('hub.challenge'):
if not request.args.get('hub.verify_token') == app.verify_token:
return 'Verification token mismatch', 403
return request.args['hub.challenge'], 200
return 'Hello world', 200
@app.route('/', methods=['POST'])
def webhook():
"""Webhook to handle facebook messages."""
data = request.get_json()
if data['object'] == 'page':
for entry in data['entry']:
for event in entry['messaging']:
user_id = event['sender']['id']
if state_in_db := app.db.get(f'STATE_{user_id}'):
state = state_in_db.decode()
else:
state = 'MENU'
message = event.get('message')
postback = event.get('postback')
if message or postback:
message = message['text'] if message else None
payload = postback['payload'] if postback else None
state = handle_user_input(
db=app.db,
state=state,
user_id=user_id,
auth=app.auth,
fb_token=app.fb_token,
message=message,
payload=payload,
)
app.db.set(f'STATE_{user_id}', state)
return "ok", 200