-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEpicScraperV2.py
329 lines (236 loc) · 13.9 KB
/
EpicScraperV2.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
from telegram import ReplyKeyboardRemove, InlineKeyboardButton, InlineKeyboardMarkup, InlineQueryResultArticle, InputTextMessageContent, ParseMode
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, ConversationHandler, MessageHandler, Filters, InlineQueryHandler
from telegram.utils.helpers import escape_markdown
import DBStuffs
import Log
import Common
import Telegram
#############################################################################################################
# Global Variables
# Stages - Used to direct various method callbacks to the next action
state_FirstMenu, state_SilentSend, state_ChangelogSend, state_Password = range(4)
# Callback data - Passed as parameters and used to determine correct method to execute / action to take place
cbd_FALSE, cbd_TRUE, cbd_SETTINGSMENU, cbd_SILENTSEND, cbd_CHANGELOG, cbd_EXIT = range(6)
#############################################################################################################
# Displays the options in within the settings menu
def Display_SettingsMenu(update, context):
keyboard = [
[InlineKeyboardButton("Send Silently", callback_data=str(cbd_SILENTSEND)),
InlineKeyboardButton("Send Updates", callback_data=str(cbd_CHANGELOG))]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text(
"Which Setting would you like to change?",
reply_markup=reply_markup
)
return state_FirstMenu
#############################################################################################################
#region Changelog sending display / set methods
#############################################################################################################
# Display menu for changelog sending
def Display_Changelog(update, context):
query = update.callback_query
query.answer()
keyboard = [
[InlineKeyboardButton("Yes", callback_data=str(cbd_TRUE)),
InlineKeyboardButton("No", callback_data=str(cbd_FALSE))]
]
reply_markup = InlineKeyboardMarkup(keyboard)
query.edit_message_text(
text="Would you like to receive notifications when updates are available?",
reply_markup=reply_markup
)
return state_ChangelogSend
#############################################################################################################
# enable / disable changelog sending
def Set_ChangelogSending(update, context):
query = update.callback_query
query.answer()
NewSetting = update.callback_query.data
ChatID = str(update.callback_query.message.chat.id)
if (IsChatRegistered(ChatID)):
if NewSetting == str(cbd_TRUE):
DBStuffs.DB_Post("UPDATE Telegram SET SendChangelog = \"{}\" WHERE ChatID == {}".format(True, ChatID))
context.bot.send_message(chat_id=update.effective_chat.id, text="Update messages enabled")
Log.Information("Enabled update messages for chat: {}".format(ChatID))
else:
DBStuffs.DB_Post("UPDATE Telegram SET SendChangelog = \"{}\" WHERE ChatID == {}".format(False, ChatID))
context.bot.send_message(chat_id=update.effective_chat.id, text="Update messages disabled")
Log.Information("Disabled update messages for chat: {}".format(ChatID))
else:
context.bot.send_message(chat_id=update.effective_chat.id, text="Chat needs to be registered first")
Log.Warning("{} tried to set Silent Sending but is not registered".format(ChatID))
return ConversationHandler.END
#############################################################################################################
#endregion
#############################################################################################################
#region SilentSend Display / Set Methods
#############################################################################################################
# Display menu for silent sending
def Display_SilentSend(update, context):
query = update.callback_query
query.answer()
keyboard = [
[InlineKeyboardButton("Yes", callback_data=str(cbd_TRUE)),
InlineKeyboardButton("No", callback_data=str(cbd_FALSE))]
]
reply_markup = InlineKeyboardMarkup(keyboard)
query.edit_message_text(
text="Would you like the messages to be sent without notification?",
reply_markup=reply_markup
)
return state_SilentSend
#############################################################################################################
# enables / disables silent messages
def Set_SilentSend(update, context):
query = update.callback_query
query.answer()
NewSetting = update.callback_query.data
ChatID = str(update.callback_query.message.chat.id)
if (IsChatRegistered(ChatID)):
if NewSetting == str(cbd_TRUE):
DBStuffs.DB_Post("UPDATE Telegram SET SendMuted = \"{}\" WHERE ChatID == {}".format(True, ChatID))
context.bot.send_message(chat_id=update.effective_chat.id, text="Silent sending enabled")
Log.Information("Enabled silent sending for chat: {}".format(ChatID))
else:
DBStuffs.DB_Post("UPDATE Telegram SET SendMuted = \"{}\" WHERE ChatID == {}".format(False, ChatID))
context.bot.send_message(chat_id=update.effective_chat.id, text="Silent sending disabled")
Log.Information("Disabled silent sending for chat: {}".format(ChatID))
else:
context.bot.send_message(chat_id=update.effective_chat.id, text="Chat needs to be registered first")
Log.Warning("{} tried to set Silent Sending but is not registered".format(ChatID))
return ConversationHandler.END
#############################################################################################################
#endregion
#############################################################################################################
#region /Join support methods
#############################################################################################################
# Extract correct Chat name basedo on chat type from the update
def Getchatname(update):
if (update.message.chat.type == "private"):
ChatName = update.message.chat.first_name + " " + update.message.chat.last_name
else:
ChatName = update.message.chat.title
return ChatName
#############################################################################################################
# Checks if the provided ChatID is in Chat Database, Returns Boolean
def IsChatRegistered(ChatID):
ChatList = DBStuffs.DB_Fetch("SELECT * FROM Telegram;")
result = False
for chat in ChatList:
if (chat[0] == ChatID):
result = True
return result
#############################################################################################################
# Saves the chat details to the database, enrolling the chat with the server
def RegisterChat(update, context):
ChatName = Getchatname(update)
ChatID = update.effective_chat.id
DBStuffs.DB_Post("INSERT INTO Telegram (ChatID, ChatName, SendMuted, SendChangelog) VALUES (\"{}\", \"{}\", \"{}\", \"{}\");".format(ChatID, ChatName, False, False))
context.bot.send_message(chat_id=ChatID, text="\"{}\" ({}) is now registered with the server".format(ChatName, ChatID))
Telegram.SendCurrentGames(ChatID)
Log.Information("\"{}\" ({}) is now registered with the server".format(ChatName, ChatID))
#############################################################################################################
#endregion
#############################################################################################################
# Registers chat with server
def start(update, context):
ChatName = Getchatname(update)
ChatID = str(update.message.chat.id)
if not IsChatRegistered(ChatID):
if (Common.IsServerPasswordProtected()):
if "/start" in update.message.text:
update.message.reply_text("This server is password protected.\nPlease enter the password now or send /cancel to exit")
context.user_data["IncorrectCount"] = 0
return state_Password
else:
RegisterChat(update, context)
return ConversationHandler.END
else:
context.bot.send_message(chat_id=update.effective_chat.id, text="\"{}\" ({}) has already been registered with the server".format(ChatName, ChatID))
Log.Warning("\"{}\" ({}) has already been registered with the server".format(ChatName, ChatID))
return ConversationHandler.END
#############################################################################################################
# De-register chat from server
def stop(update, context):
ChatID = str(update.message.chat.id)
ChatName = Getchatname(update)
if IsChatRegistered(ChatID):
DBStuffs.DB_Post("DELETE FROM Telegram WHERE ChatID == \"{}\"".format(ChatID))
context.bot.send_message(chat_id=update.effective_chat.id, text="\"{}\" ({}) has been de-registered from the server".format(ChatName, ChatID))
Log.Information("\"{}\" ({}) has been de-registered".format(ChatName, ChatID))
else:
context.bot.send_message(chat_id=update.effective_chat.id, text="This chat was never registered")
Log.Warning("{} tried to de-register {}, but was never registered with the server".format(ChatID, ChatName))
return ConversationHandler.END
#############################################################################################################
# Method to tell user that the command was not recognised
def UnknownCommand(update, context):
if ("group" in update.message.chat.type):
if ("@EpicScraperBot" in update.message.text):
context.bot.send_message(chat_id=update.effective_chat.id, text="U WOT M8?")
else:
context.bot.send_message(chat_id=update.effective_chat.id, text="U WOT M8?")
return ConversationHandler.END
#############################################################################################################
# Re-Send the Active games to the current chat
def resend(update, context):
ChatID = str(update.message.chat.id)
Telegram.SendCurrentGames(ChatID)
return ConversationHandler.END
#############################################################################################################
# returns to the previous menu's handle
def end(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="Exited")
return ConversationHandler.END
#############################################################################################################
# Verifies that the submitted password matches the server record, Registers chat if successful
def ValidatePassword(update, context):
context.bot.delete_message(chat_id=update.effective_chat.id, message_id=update.message.message_id)
SendPass = update.message.text
ServerPass = DBStuffs.DB_FetchSingle("SERVER_PASSWORD", "Instance")
context.user_data["IncorrectCount"] += 1
if context.user_data["IncorrectCount"] <= 3:
if SendPass == ServerPass:
context.bot.send_message(chat_id=update.effective_chat.id, text="Password correct. Registering...")
RegisterChat(update, context)
return ConversationHandler.END
else:
context.bot.edit_message_text("Password Incorrect. (Attempt: {})\nYou can try again, or stop by sending /cancel".format(context.user_data["IncorrectCount"]), chat_id=update.effective_chat.id, message_id=update.message.message_id - context.user_data["IncorrectCount"])
return state_Password
else:
context.bot.edit_message_text("Password Incorrect.\nToo many incorrect attempts. Good Bye", chat_id=update.effective_chat.id, message_id=update.message.message_id - context.user_data["IncorrectCount"])
Log.Warning("\"{}\" tried to join the server but used an incorrect password 3 times".format(Getchatname(update)))
return ConversationHandler.END
#############################################################################################################
# Creates listeners for the Telegram bot's menu
def main():
TG_Bot_ID = DBStuffs.DB_FetchSingle("TG_Bot_ID", "Instance")
updater = Updater(TG_Bot_ID, use_context=True)
dp = updater.dispatcher
ch_settingsmenu = ConversationHandler(
entry_points=[CommandHandler('start', start),
CommandHandler('settings', Display_SettingsMenu),
CommandHandler('resend', resend),
CommandHandler('stop', stop),
MessageHandler(Filters.command, UnknownCommand)],
states={
state_FirstMenu: [CallbackQueryHandler(Display_SilentSend, pattern='^' + str(cbd_SILENTSEND) + '$'),
CallbackQueryHandler(Display_Changelog, pattern='^' + str(cbd_CHANGELOG) + '$')],
state_SilentSend: [CallbackQueryHandler(Set_SilentSend, pattern='^{}|{}$'.format(str(cbd_TRUE), str(cbd_FALSE)))],
state_ChangelogSend: [CallbackQueryHandler(Set_ChangelogSending, pattern='^{}|{}$'.format(str(cbd_TRUE), str(cbd_FALSE)))],
state_Password: [MessageHandler(Filters.text, ValidatePassword),
CommandHandler('cancel', end)]
},
fallbacks=[MessageHandler(Filters.command, UnknownCommand)]
)
# Add ConversationHandler to dispatcher that will be used for handling updates
dp.add_handler(ch_settingsmenu)
# 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()
#############################################################################################################
main()