-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgdgpisausermanager.py
195 lines (149 loc) · 5.71 KB
/
gdgpisausermanager.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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import logging
import threading
from sys import maxsize
from telegram import InlineKeyboardButton
from telegram import InlineKeyboardMarkup
from telegram import ParseMode
from telegram.error import NetworkError
from telegram.ext import CallbackQueryHandler
from telegram.ext import CommandHandler
from telegram.ext import Filters
from telegram.ext import MessageHandler
from telegram.ext import Updater
from config import Config
logging.basicConfig(
format='%(asctime)s - %(levelname)s - %(message)s',
level=logging.INFO,
)
logger = logging.getLogger(__name__)
new_users = []
def welcome(bot, update):
"""
:type bot: telegram.Bot
:type update: telegram.Updater
"""
for new_user_obj in update.message.new_chat_members: # new_user_obj has telegram.user.User type
user_handle = new_user_obj.name
user_id = new_user_obj.id
chat_id = update.message.chat_id
if Config.DELETE_JOIN:
bot.delete_message(chat_id, message_id=update.message.message_id)
reply_text = "Benvenuto/a {}! Premi <i>Confermo</i> per dimostrare di non essere un bot.".format(
user_handle,
)
new_users.append(new_user_obj.id)
keyboard = [[InlineKeyboardButton("Confermo", callback_data=1)]]
reply_markup = InlineKeyboardMarkup(keyboard)
message = bot.send_message(
chat_id=update.message.chat_id,
text=reply_text,
reply_markup=reply_markup,
parse_mode=ParseMode.HTML,
)
threading.Timer(
interval=Config.WAITING_TIMEOUT,
function=timer,
args=[bot, user_id, chat_id, message.message_id],
).start()
logger.info("Nuovo utente: {}.".format(user_handle))
def goodbye(bot, update):
if Config.DELETE_JOIN:
bot.delete_message(update.message.chat_id, message_id=update.message.message_id)
def button_pressed(bot, update):
"""
:type bot: telegram.Bot
:type update: telegram.Update
"""
if update.callback_query.from_user.id in new_users:
user_handle = update.callback_query.from_user.name
chat_id = update.callback_query.message.chat_id
message_id = update.callback_query.message.message_id
new_users.remove(update.callback_query.from_user.id)
bot.delete_message(chat_id, message_id=message_id)
bot.send_message(
chat_id,
text="{}: grazie per aver completato la registrazione!".format(user_handle),
)
logger.info("Utente confermato: {}.".format(user_handle))
def check_activity(bot, update):
"""
:type bot: telegram.Bot
:type update: telegram.Update
"""
bot.send_message(
chat_id=update.message.chat_id,
text="pong",
reply_to_message_id=update.message.message_id,
)
def is_admin(bot, user_id, chat_id):
"""
:type bot: telegram.Bot
:type user_id: str or int
:type chat_id: str or int
"""
try:
return any(user_id == admin.user.id for admin in bot.get_chat_administrators(chat_id))
except NetworkError:
return False
def ban_user(bot, update):
"""
:type bot: telegram.Bot
:type update: telegram.Update
"""
if update.message.reply_to_message is None:
return
chat_id = update.message.chat.id
if is_admin(bot, user_id=update.message.from_user.id, chat_id=chat_id):
user_id = update.message.reply_to_message.from_user.id
user_handle = update.message.reply_to_message.from_user.name
if not is_admin(bot, user_id, chat_id):
bot.kick_chat_member(chat_id, user_id)
bot.send_message(
chat_id,
text="{} è stato bannato con successo.".format(user_handle),
)
logger.info("Utente {} bannato con successo.".format(user_handle))
def kick_user(bot, update):
"""
:type bot: telegram.Bot
:type update: telegram.Update
"""
if update.message.reply_to_message is None:
return
chat_id = update.message.chat.id
if is_admin(bot, user_id=update.message.from_user.id, chat_id=chat_id):
user_id = update.message.reply_to_message.from_user.id
user_handle = update.message.reply_to_message.from_user.name
if is_admin(bot, user_id, chat_id):
bot.kick_chat_member(chat_id, user_id)
bot.unban_chat_member(chat_id, user_id)
logger.info("The user {} was kicked with success.".format(user_handle))
def main():
updater = Updater(Config.TOKEN)
updater.dispatcher.add_handler(MessageHandler(Filters.status_update.new_chat_members, welcome))
updater.dispatcher.add_handler(MessageHandler(Filters.status_update.left_chat_member, goodbye))
updater.dispatcher.add_handler(MessageHandler(Filters.regex('^ping$'), check_activity))
updater.dispatcher.add_handler(CallbackQueryHandler(button_pressed))
updater.dispatcher.add_handler(CommandHandler('ban', ban_user))
updater.dispatcher.add_handler(CommandHandler('kick', kick_user))
updater.start_polling()
try:
updater.idle()
except KeyboardInterrupt:
print("Chiusura programma")
def timer(bot, user_id, chat_id, message_id):
"""
:type bot: telegram.Bot
:type user_id: str or int
:type chat_id: str or int
:type message_id: str or int
"""
if user_id in new_users:
logger.info("User with id: {} auto-removed with success".format(user_id))
new_users.remove(user_id)
bot.kick_chat_member(chat_id, user_id, until_date=maxsize) # Kicking a member for over 365 days is forever
bot.delete_message(chat_id, message_id=message_id)
if __name__ == '__main__':
exit(main())