-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
148 lines (122 loc) · 6.6 KB
/
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import logging
import datetime
import config
import yaml
import gif
from telegram.ext import Updater, CommandHandler, MessageHandler
from telegram.ext import Filters, InlineQueryHandler, RegexHandler
from telegram.ext import CallbackQueryHandler
from beer import beer_search_menu, beer_info, dry_score_message, wet_score_message
from monologue import query_limit, set_limit, handle_counter, handle_gifs
from movies import movie_search_menu, movie_info, person_search_menu, person_info
# from twitter import start_twitter_stream, send_tweets
from weather import chuva, chuva2, scheduled_weather, send_weather
from corona import get_corona, get_covid, get_covidbr
from vaccine import get_vaccine
from telegram.error import (TelegramError, Unauthorized, BadRequest,
TimedOut, ChatMigrated, NetworkError)
import flex
cfg = config.cfg()
with open('gifs.yml') as f:
gifs = yaml.load(f, Loader=yaml.FullLoader)
logger = logging.getLogger(__name__)
counter = {}
msg_limit = {}
group_id = cfg.get('group_id')
my_chat_id = cfg.get('my_chat_id')
def start(update, context):
context.bot.send_message(chat_id=update.message.chat_id,
text="I'm The MonologNator. I'll be back")
def ping(update, context):
img = gif.get_random_giphy(keyword='pong')
context.bot.send_document(chat_id=update.message.chat_id,
document=img, timeout=100)
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
context.bot.send_message(chat_id=my_chat_id, text=context.error, timeout=100)
def error_callback(update, context):
try:
context.bot.send_message(chat_id=my_chat_id, text=context.error, timeout=100)
raise context.error
except Unauthorized as e:
# remove update.message.chat_id from conversation list
logger.error('Update "%s" caused error "%s"', update, e)
context.bot.send_message(chat_id=my_chat_id, text=context.error, timeout=100)
except BadRequest as e:
# handle malformed requests - read more below!
logger.error('Update "%s" caused error "%s"', update, e)
context.bot.send_message(chat_id=my_chat_id, text=context.error, timeout=100)
except TimedOut as e:
# handle slow connection problems
logger.error('Update "%s" caused error "%s"', update, e)
context.bot.send_message(chat_id=my_chat_id, text=context.error, timeout=100)
except NetworkError as e:
# handle other connection problems
logger.error('Update "%s" caused error "%s"', update, e)
context.bot.send_message(chat_id=my_chat_id, text=context.error, timeout=100)
except ChatMigrated as e:
# the chat_id of a group has changed, use e.new_chat_id instead
logger.error('Update "%s" caused error "%s"', update, e)
context.bot.send_message(chat_id=my_chat_id, text=context.error, timeout=100)
except TelegramError as e:
# handle all other telegram related errors
logger.error('Update "%s" caused error "%s"', update, e)
context.bot.send_message(chat_id=my_chat_id, text=context.error, timeout=100)
def main():
method = cfg.get('update-method') or 'polling'
token = cfg.get('telegram_token')
logging.basicConfig(level=cfg.get('loglevel', 'INFO'),
format='%(asctime)s - %(funcName)s - %(levelname)s - %(message)s')
updater = Updater(token, use_context=True)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('ping', ping))
updater.dispatcher.add_handler(CommandHandler('limit', query_limit))
updater.dispatcher.add_handler(CommandHandler('set_limit', set_limit))
updater.dispatcher.add_handler(CommandHandler('weather', send_weather))
updater.dispatcher.add_handler(CommandHandler('chuva', chuva))
updater.dispatcher.add_handler(CommandHandler('chuva2', chuva2))
updater.dispatcher.add_handler(CommandHandler('beer', beer_search_menu))
updater.dispatcher.add_handler(CommandHandler('homebrew', beer_search_menu))
updater.dispatcher.add_handler(CommandHandler('beer2', beer_search_menu))
updater.dispatcher.add_handler(CommandHandler('dry', dry_score_message))
updater.dispatcher.add_handler(CommandHandler('wet', wet_score_message))
updater.dispatcher.add_handler(CommandHandler('corona', get_corona))
updater.dispatcher.add_handler(CommandHandler('covid', get_covid))
updater.dispatcher.add_handler(CommandHandler('covidbr', get_covidbr))
updater.dispatcher.add_handler(CommandHandler('vac', get_vaccine))
updater.dispatcher.add_handler(CommandHandler('movie', movie_search_menu))
updater.dispatcher.add_handler(CommandHandler('person', person_search_menu))
updater.dispatcher.add_handler(CommandHandler('flex_pct', flex.send_percent_all))
updater.dispatcher.add_handler(CommandHandler('flex', flex.send_graph))
updater.dispatcher.add_handler(CommandHandler('prata', flex.send_prata))
updater.dispatcher.add_handler(CommandHandler('nanica', flex.send_nanica))
# GIF handlers
updater.dispatcher.add_handler(CommandHandler('list_alias', gif.get_aliases))
updater.dispatcher.add_handler(CommandHandler('list_aliases', gif.list_aliases))
updater.dispatcher.add_handler(InlineQueryHandler(gif.inlinequery))
updater.dispatcher.add_handler(MessageHandler(Filters.regex(gif.word_watcher_regex()), gif.word_watcher_gif))
updater.dispatcher.add_handler(CallbackQueryHandler(beer_info, pattern='beer'))
updater.dispatcher.add_handler(CallbackQueryHandler(movie_info, pattern='^movie'))
updater.dispatcher.add_handler(CallbackQueryHandler(person_info, pattern='^person'))
updater.dispatcher.add_error_handler(error_callback)
updater.dispatcher.add_handler(MessageHandler(Filters.text, handle_counter))
updater.dispatcher.add_handler(MessageHandler(Filters.document.gif, handle_gifs))
j = updater.job_queue
daily_job = j.run_daily(scheduled_weather, time=datetime.time(6))
# tweet_job = j.run_repeating(send_tweets, interval=60, first=20)
# start_twitter_stream()
if method == 'polling':
updater.start_polling(drop_pending_updates=True)
else:
webhook_url = cfg.get('webhook-url')
webhook_url = webhook_url + '/' + token
updater.start_webhook(listen='0.0.0.0',
port='8080',
url_path=token)
logger.info(f'Setting webhook url to: {webhook_url}')
updater.bot.set_webhook(url=webhook_url)
logger.info('Starting Monolognator...')
updater.idle()
if __name__ == '__main__':
main()