-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
190 lines (153 loc) · 7.18 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This program is dedicated to the public domain under the CC0 license.
#
# THIS EXAMPLE HAS BEEN UPDATED TO WORK WITH THE BETA VERSION 12 OF PYTHON-TELEGRAM-BOT.
# If you're still using version 11.1.0, please see the examples at
# https://github.com/python-telegram-bot/python-telegram-bot/tree/v11.1.0/examples
"""
First, a few handler functions are defined. Then, those functions are passed to
the Dispatcher and registered at their respective places.
Then, the bot is started and runs until we press Ctrl-C on the command line.
Usage:
Basic inline bot example. Applies different text transformations.
Press Ctrl-C on the command line or send a signal to the process to stop the
bot.
"""
import os
from loguru import logger
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ChatPermissions
from telegram.ext import Filters, Updater, MessageHandler, CommandHandler, CallbackQueryHandler
from telegram.error import BadRequest
from random import shuffle
# Enable logging
logger.add("logs/gatekeeper-bot_{time}.log",
format="{time} - {name} - {level} - {message}", level="INFO", rotation="5 MB", compression="zip")
# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
def start(update, context):
"""Send a message when the command /start is issued."""
update.message.reply_text("Hello, I am the Swiss Mechanical Keyboard Enthusiasts Gatekeeper. "
"As soon as a new user joins, they will be restricted to keep them "
"from posting until they prove they are not a robot.")
def help(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text('Available commands:\n\n/id')
def id(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text(update.effective_chat.id)
def remove_job_if_exists(name, context):
"""Remove job with given name. Returns whether job was removed."""
current_jobs = context.job_queue.get_jobs_by_name(str(name))
if not current_jobs:
return False
for job in current_jobs:
job.schedule_removal()
return True
def welcome_timeout(context):
"""Remove welcome message after predifined timeout in os.environ['TIMEOUT'] """
job_context = context.job.context
if 'message_id' in job_context:
try:
context.bot.delete_message(
chat_id=os.environ['CHAT_ID'],
message_id=job_context['message_id']
)
logger.info('Welcome message for "{first_name}" timed out after {seconds} seconds.',
first_name=job_context['first_name'], seconds=os.environ['TIMEOUT'])
except BadRequest:
logger.info('Welcome message with id "{message_id}" no longer exists.',
message_id=job_context['message_id'])
def hodor(update, context):
try:
for new_member in update.message.new_chat_members:
callback_id = str(new_member.id)
context.bot.restrict_chat_member(
int(os.environ['CHAT_ID']),
new_member.id,
ChatPermissions(
can_send_messages=False,
can_send_media_messages=False,
can_send_other_messages=False,
can_add_web_page_previews=False)
)
keyboard_items = [
InlineKeyboardButton(
'Mechanical', callback_data=callback_id + ',mech'),
InlineKeyboardButton(
'Rubberdome', callback_data=callback_id + ',rubber'),
]
shuffle(keyboard_items)
keyboard = []
for i in range(2):
keyboard.append([keyboard_items[i]])
reply_markup = InlineKeyboardMarkup(keyboard)
timeout_seconds = int(os.environ['TIMEOUT'])
timeout_minutes = str(int(timeout_seconds / 60))
message = update.message.reply_text(
'Hello, ' +
new_member.first_name +
' and welcome to the Swiss Mech Chat. Just a small formality before we allow you to post: ' +
'Please prove that you are not a robot by choosing "Mechanical" below. You have ' +
timeout_minutes +
' minutes to answer.',
reply_markup=reply_markup,
disable_notification=True
)
remove_job_if_exists(callback_id, context)
context.job_queue.run_once(welcome_timeout, timeout_seconds, context={
"message_id": message.message_id, "first_name": new_member.first_name}, name=callback_id)
except AttributeError:
pass
def button(update, context):
query = update.callback_query
person_who_pushed_the_button = int(query.data.split(",")[0])
if query.from_user.id == person_who_pushed_the_button:
logger.info('User "{user}" sent back "{reply}"',
user=str(query.from_user), reply=str(query.data))
remove_job_if_exists(person_who_pushed_the_button, context)
if 'mech' in query.data:
context.bot.delete_message(
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id
)
context.bot.restrict_chat_member(
int(os.environ['CHAT_ID']),
person_who_pushed_the_button,
ChatPermissions(
can_send_messages=True,
can_send_media_messages=True,
can_send_other_messages=True,
can_add_web_page_previews=True)
)
else:
query.edit_message_text(text="🚨 Looks like we have another bot. 🚨")
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "{update}" caused error "{error}"',
update=update, error=context.error)
def main():
# Create the Updater and pass it your bot's token.
# Make sure to set use_context=True to use the new context based callbacks
# Post version 12 this will no longer be necessary
updater = Updater(str(os.environ['TOKEN']), use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("id", id))
dp.add_handler(MessageHandler(
Filters.chat(int(os.environ['CHAT_ID'])), hodor))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
# on noncommand i.e message - echo the message on Telegram
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Block until the user presses 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()