-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
88 lines (74 loc) · 2.38 KB
/
main.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
# 3rd party imports
from telegram.ext import PicklePersistence, Updater, CallbackQueryHandler
# Local imports
from cinnabot.base import Start, About, Help
from cinnabot.claims import Claims
from cinnabot.feedback import Feedback
from cinnabot.resources import Resources
from cinnabot.spaces import Spaces
from cinnabot.travel import NUSMap
from cinnabot.supper import Supper
from google.cloud.firestore import Client
from google.auth.credentials import AnonymousCredentials
# Initialize a backend with a firestore client
firestore = Client(
project='usc-website-206715',
credentials=AnonymousCredentials(),
)
# Initialize to check that all requirements defined in utils.py have been met.
FEATURES = [
Start(),
About(),
Spaces(database=firestore),
Claims(),
Resources(),
Feedback(),
Supper(),
NUSMap(),
Help(),
]
def make_cinnabot(token):
"""Helps initialize an updater with our features"""
# The updater primarily gets telegram updates from telegram servers
updater = Updater(token)
# The dispatcher routes updates to the first matching handler
for feature in FEATURES:
updater.dispatcher.add_handler(feature.handler)
# Store data for /help and /help <feature> in the bot
updater.dispatcher.bot_data['help_text'] = dict()
updater.dispatcher.bot_data['help_full'] = dict()
for feature in FEATURES:
updater.dispatcher.bot_data['help_text'][feature.command] = feature.help_text
updater.dispatcher.bot_data['help_full'][feature.command] = feature.help_full
return updater
if __name__ == '__main__':
import logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO,
)
logger = logging.getLogger(__name__)
# Deploy using webhooks if on server
try:
import os
TOKEN = os.environ['TOKEN']
HOST = os.environ['HOST']
PORT = os.environ.get('PORT', 5000)
logger.info(f'Deploying on webhook...')
cinnabot = make_cinnabot(TOKEN)
cinnabot.bot.set_webhook(f'{HOST}/{TOKEN}')
cinnabot.start_webhook('0.0.0.0', PORT, url_path=TOKEN)
cinnabot.idle()
# Fallback to polling (likely to be local development)
except KeyError:
import json
with open('config.json', 'r') as f:
config = json.load(f)
TOKEN = config.get('telegram_bot_token')
logger.warning(f'Deploying locally...')
cinnabot = make_cinnabot(TOKEN)
cinnabot.start_polling()
cinnabot.idle()
# Deployment failed?
except Exception as e:
logger.error(e)