-
Notifications
You must be signed in to change notification settings - Fork 2
/
repeater.py
51 lines (37 loc) · 1.44 KB
/
repeater.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import os
from telegram import Bot, Update, Message
from telegram.ext.callbackcontext import CallbackContext
from telegram.ext import (Updater, MessageHandler, Filters, CommandHandler,
PicklePersistence)
from repeat import repeat, clean_repeat
token = os.getenv('TELEGRAM_APITOKEN')
bot = Bot(token=token)
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def error(update: Update, context: CallbackContext):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
print(context.error)
def main():
updater = Updater(token, use_context=True)
dp = updater.dispatcher
# log all errors
dp.add_error_handler(error)
repeat_filters = Filters.text & ~Filters.command
# add repeat handler
dp.add_handler(MessageHandler(repeat_filters, repeat))
# add repeat clear handler
dp.add_handler(MessageHandler(Filters.all & ~repeat_filters, repeat))
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()