This repository has been archived by the owner on Feb 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_poll_bot.py
217 lines (180 loc) · 8.25 KB
/
game_poll_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import datetime
import logging
import json
import pytz
from typing import Tuple, Optional
from telegram import (
Update,
Chat,
ChatMember,
ChatMemberUpdated
)
from telegram.ext import (
Updater,
Filters,
MessageHandler,
CommandHandler,
CallbackContext,
ChatMemberHandler,
)
CONFIG_FILE = 'config.json'
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)
# Определить статус добавления в чат
def extract_status_change(
chat_member_update: ChatMemberUpdated,
) -> Optional[Tuple[bool, bool]]:
status_change = chat_member_update.difference().get("status")
old_is_member, new_is_member = chat_member_update.difference().get("is_member",
(None, None))
if status_change is None:
return None
old_status, new_status = status_change
was_member = (
old_status
in [
ChatMember.MEMBER,
ChatMember.CREATOR,
ChatMember.ADMINISTRATOR,
]
or (old_status == ChatMember.RESTRICTED and old_is_member is True)
)
is_member = (
new_status
in [
ChatMember.MEMBER,
ChatMember.CREATOR,
ChatMember.ADMINISTRATOR,
]
or (new_status == ChatMember.RESTRICTED and new_is_member is True)
)
return was_member, is_member
class GamePollBot:
def __init__(self):
with open(CONFIG_FILE) as cfg_file:
self.config = json.load(cfg_file)
if len(self.config['poll_options']) < 2:
raise Exception("Poll options count can't be smaller than 2")
self.updater = Updater(self.config['token'])
dispatcher = self.updater.dispatcher
dispatcher.add_handler(MessageHandler(
Filters.text & ~Filters.command, self.help_cmd))
dispatcher.add_handler(CommandHandler('help', self.help_cmd))
dispatcher.add_handler(CommandHandler('start', self.help_cmd))
dispatcher.add_handler(CommandHandler(
'poll', self.poll_cmd, pass_chat_data=True))
dispatcher.add_handler(CommandHandler('add', self.add_cmd))
dispatcher.add_handler(CommandHandler('del', self.del_cmd))
dispatcher.add_handler(CommandHandler('list', self.list_cmd))
dispatcher.add_handler(CommandHandler('daily', self.daily_cmd))
dispatcher.add_handler(ChatMemberHandler(
self.chat_member_handler, ChatMemberHandler.MY_CHAT_MEMBER))
dispatcher.add_handler(MessageHandler(Filters.command, self.help_cmd))
# Запуск бота
def run(self):
self.updater.start_polling()
self.updater.idle()
# Помощь
def help_cmd(self, update: Update, context: CallbackContext):
update.message.reply_text(
'Команды бота:\n'
'/poll - создать голосование\n'
'/add <вариант> - добавить вариант в опрос\n'
'/del <вариант> - удалить вариант опроса\n'
'/list - получить список вариантов опроса\n'
'/daily - создать ежедневную задачу на запуск голосования\n'
)
def chat_member_handler(self, update: Update, context: CallbackContext):
result = extract_status_change(update.my_chat_member)
if result is None:
return
was_member, is_member = result
cause_name = update.effective_user.full_name
chat = update.effective_chat
if chat.type in [Chat.GROUP, Chat.SUPERGROUP]:
if not was_member and is_member:
logger.info("%s added the bot to the group %s",
cause_name, chat.title)
if not chat.id in self.config['chats']:
self.config['chats'].append(chat.id)
self.update_config_file()
elif was_member and not is_member:
logger.info("%s removed the bot from the group %s",
cause_name, chat.title)
if chat.id in self.config['chats']:
self.config['chats'].remove(chat.id)
self.update_config_file()
# Отправить боту запрос на создание опроса
def send_poll(self, context: CallbackContext):
for chat in self.config['chats']:
context.bot.send_poll(
chat_id=chat,
question='Во что играем?',
options=self.config['poll_options'],
is_anonymous=False
)
# Создать задачу для бота на запуск голосования в определенные дни
def daily_cmd(self, update: Update, context: CallbackContext):
for job in context.job_queue.get_jobs_by_name('daily'):
job.schedule_removal()
context.job_queue.run_daily(self.send_poll, datetime.time(hour=12, minute=30, tzinfo=pytz.timezone(
'Europe/Moscow')), days=(0, 1, 2, 3, 4), name='daily')
update.message.reply_text(
'Создана ежедневная задача на запуск голосования')
# Создать опрос
def poll_cmd(self, update: Update, context: CallbackContext):
self.send_poll(context)
update.message.reply_text('Опросы созданы')
# Добавить вариант опроса
def add_cmd(self, update: Update, context: CallbackContext):
option = ' '.join(context.args).strip()
if not option:
context.bot.send_message(
chat_id=update.effective_chat.id, text='Не указано название варианта опроса')
return
if option in self.config['poll_options']:
ans_text = f'Вариант {option} уже существует'
else:
self.config['poll_options'].append(option)
self.update_config_file()
ans_text = f'В список вариантов опроса добавлен {option}'
ans_text += ('\nТекущий список: {}'.format(
', '.join(self.config['poll_options'])))
context.bot.send_message(
chat_id=update.effective_chat.id, text=ans_text)
# Удалить вариант опроса
def del_cmd(self, update: Update, context: CallbackContext):
option = ' '.join(context.args).strip()
if not option:
context.bot.send_message(
chat_id=update.effective_chat.id, text='Не указано название варианта опроса')
return
if len(self.config['poll_options']) == 2:
context.bot.send_message(
chat_id=update.effective_chat.id, text='Число вариантов опроса не может быть меньше 2')
return
if option not in self.config['poll_options']:
ans_text = f'Вариант {option} не найден в списке вариантов опроса'
else:
self.config['poll_options'].remove(option)
self.update_config_file()
ans_text = f'Вариант {option} удален из списка вариантов опроса'
ans_text += ('\nТекущий список: {}'.format(
', '.join(self.config['poll_options'])))
context.bot.send_message(
chat_id=update.effective_chat.id, text=ans_text)
# Получить текущий список вариантов опроса
def list_cmd(self, update: Update, context: CallbackContext):
ans_text = 'Текущий список вариантов опроса: {}'.format(
', '.join(self.config['poll_options']))
context.bot.send_message(
chat_id=update.effective_chat.id, text=ans_text)
# Обновить конфигурационный файл
def update_config_file(self):
with open(CONFIG_FILE, 'w') as cfg_file:
json.dump(self.config, cfg_file, indent=4)
if __name__ == '__main__':
bot = GamePollBot()
bot.run()