-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
116 lines (93 loc) · 4.12 KB
/
main.py
File metadata and controls
116 lines (93 loc) · 4.12 KB
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
import logging
import os
from argparse import ArgumentParser
from datetime import date, datetime
from telegram import *
from telegram.ext import *
import settings
from utils import History, convert_date
logger = logging.getLogger(__name__)
class Stop(Exception):
pass
# get date from message text if any
def get_custom_date(text: str, default: date):
if not text:
return default
try:
return datetime.strptime(text[:10], '%Y-%m-%d').date()
except ValueError:
return default
def check_chat(update: Update, context: CallbackContext):
chat = update.effective_chat # type:Chat
bot = context.bot # type:Bot
if chat.type != Chat.SUPERGROUP:
bot.send_message(chat.id, 'Добавь меня в групповой чат, я буду записывать и потом напоминать о том, '
'что обсуждалось в чате некоторое время назад.')
raise Stop
# if chat type is supergroup, try to add message id to history file. Any other chat type - answer with a default text
def answer(update: Update, context: CallbackContext):
check_chat(update, context)
message = update.message # type:Message
text = message.text or message.caption
custom_date = get_custom_date(text, convert_date(message.date).date())
history = History(message.chat_id)
if history.has(custom_date):
# дата уже есть
history.close()
else:
history.add(custom_date, message.message_id)
history.save()
# for command 'save': add (replace - if date already exists) message id to history file
def save_command(update: Update, context: CallbackContext):
check_chat(update, context)
message = update.message # type:Message
reply_to_message = update.message.reply_to_message # type:Message
if not reply_to_message:
message.reply_text('Сделай реплай на сообщение')
return
message_id = str(reply_to_message.message_id)
text = reply_to_message.text or reply_to_message.caption
custom_date = get_custom_date(text, convert_date(reply_to_message.date).date())
history = History(message.chat_id)
history.add(custom_date, message_id)
history.save()
if history.has(custom_date):
reply = 'Обновлено для {}'
else:
reply = 'Сохранено для {}'
reply = reply.format(custom_date)
message.reply_text(reply)
def help_command(update: Update, context: CallbackContext):
chat = update.effective_chat # type:Chat
context.bot.send_message(
chat.id,
'Подробное <a href="{}">описание</a>, инструкция по использованию со скриншотами и disclaimer.\n\n'
'<a href="{}">Репозиторий</a>'.format(settings.HELP_URL, settings.REPOSITORY_URL),
parse_mode='HTML',
)
def error(update: Update, context: CallbackContext):
if isinstance(context.error, Stop):
# остановка
return
logger.exception(context.error)
if update:
message = update.effective_message # type:Message
message.reply_text('Произошла ошибка')
def main():
parser = ArgumentParser()
parser.add_argument('-v', '--verbose', action='store_true')
args = parser.parse_args()
os.makedirs('data', exist_ok=True)
updater = Updater(token=settings.TELEGRAM_BOT_API, use_context=True)
level = logging.DEBUG if args.verbose else logging.INFO
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=level)
dispatcher = updater.dispatcher # type:Dispatcher
dispatcher.add_handler(CommandHandler('start', answer))
dispatcher.add_handler(CommandHandler('save', save_command))
dispatcher.add_handler(CommandHandler('help', help_command))
dispatcher.add_handler(MessageHandler(Filters.update.message & (Filters.text | Filters.caption), answer))
dispatcher.add_error_handler(error)
updater.start_polling(allowed_updates=['message'])
# TODO: try-except KeyboardInterrupt
if __name__ == '__main__':
main()