Created
August 6, 2019 22:20
-
-
Save gschanuel/6664958af02a0beb6a594146a2b4e446 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
References: | |
https://core.telegram.org/bots/api | |
https://python-telegram-bot.readthedocs.io/en/latest | |
""" | |
import datetime, time, telegram | |
from threading import Thread, Timer | |
from settings import TOKEN, msg_flood, msg_interval, con, cursor | |
from telegram.ext import Updater, MessageHandler, Filters, CommandHandler | |
import random | |
flood_counter = [] | |
data = [] | |
def on_new_animation(bot, update): | |
print ("[!][on_new_animation]") | |
try: | |
item="{}:{}:{}".format(update.message.chat_id,update.message.from_user.id,update.message.message_id) | |
print ("[!][New Animation] {}".format(item)) | |
data.append(item) | |
flood_counter.append(item) | |
check_flood(bot, update) | |
Timer(msg_interval, timeout, [bot, update]).start() | |
except Exception as e: | |
print (str(e)) | |
def deleteMsgs(bot, update, msgIDs): | |
print ("[!][deleteMsgs]") | |
try: | |
chat_id=str(update.message.chat_id) | |
user_id=str(update.message.from_user.id) | |
for msg in msgIDs[msg_flood:]: | |
Thread(target=deleteMsg, args=(bot, update, int(msg),),).start() | |
item="{}:{}:{}".format(update.message.chat_id,update.message.from_user.id,msg) | |
data.remove(item) | |
xinga(bot, update) | |
except Exception as e: | |
print (str(e)) | |
def xinga(bot, update): | |
print ("[!][xinga]") | |
try: | |
query = "SELECT text FROM xingamento ORDER BY RAND() LIMIT 1" | |
cursor.execute(query) | |
row = cursor.fetchone() | |
quote = "__{}__".format(row[0]) | |
msg="[!] Cala a boca {}, {}!".format(update.message.from_user.first_name, quote) | |
bot.send_message(update.message.chat_id, msg, parse_mode='Markdown') | |
except Exception as e: | |
print (str(e)) | |
def deleteMsg(bot, update, msgId): | |
print ("[!][deleteMsg]") | |
try: | |
chat_id=update.message.chat_id | |
print ("[!][deleteMsg] Deleting => {}".format(msgId)) | |
bot.delete_message(chat_id=chat_id, message_id=msgId) | |
item="{}:{}:{}".format(update.message.chat_id,update.message.from_user.id,msgId) | |
data.remove(item) | |
except Exception as e: | |
print (str(e)) | |
def check_flood(bot, update): | |
print ("[!][check_flood]") | |
try: | |
chat_id=str(update.message.chat_id) | |
user_id=str(update.message.from_user.id) | |
counter = 0 | |
#msgIds = [] | |
print ("[!] Checking flood") | |
for i, item in enumerate(flood_counter): | |
if (chat_id + ":" + user_id) in item: | |
#msgIds.append((flood_counter[i].split(":")[2])) | |
counter += 1 | |
if counter > msg_flood: | |
print ("[!] Flood in action: {} ({})".format(update.message.user_id, counter)) | |
#Thread(target=deleteMsg, args=(bot, update, update.message.message_id),).start() | |
deleteMsg(bot, update, update.message.message_id) | |
#deleteMsgs(bot, update, msgIds) | |
except Exception as e: | |
print (str(e)) | |
def timeout(bot, update): | |
print ("[!][timeout]") | |
try: | |
print ("[!] timeout - flood_counter: {}".format(flood_counter)) | |
print ("[!] timeout - data: {}".format(data)) | |
item="{}:{}:{}".format(update.message.chat_id,update.message.from_user.id,update.message.message_id) | |
flood_counter.remove(item) | |
#data.remove(item) | |
except Exception as e: | |
print (str(e)) | |
def logging(bot, update): | |
print ("[!][logging]") | |
print ("##############################") | |
print (str(update)) | |
print (" ") | |
try: | |
photo = "" | |
query =("INSERT INTO log (message_id, date, chat_id, text, photo, from_id, first_name, last_name, username) VALUES ('{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}')").format(update.message.message_id, str(update.message.date), update.message.chat.id, str(update.message.text), photo, update.message.from_user.id, update.message.from_user.first_name, update.message.from_user.last_name, update.message.from_user.username ) | |
# else: | |
# photo = update.message.photo.file_id | |
# print(photo) | |
# print(query) | |
cursor.execute(query) | |
con.commit() | |
except Exception as e: | |
print (str(e)) | |
def put_quote(bot, update): | |
print ("[!][put_quote]") | |
try: | |
message_id=update.message.reply_to_message.message_id | |
print (message_id) | |
query=("INSERT INTO quote (message_id, chat_id) VALUES ({}, {})").format(message_id, update.message.chat_id) | |
cursor.execute(query) | |
con.commit() | |
print ("OK") | |
except Exception as e: | |
print (str(e)) | |
def get_quote(bot, update): | |
print ("[!][get_quote]") | |
try: | |
if update.message.text == "/lauters": | |
query = "SELECT text FROM lauters ORDER BY RAND() LIMIT 1" | |
else: | |
quote_id=(update.message.text).split(" ") | |
if len(quote_id) > 1 : | |
query = "SELECT message_ID FROM quote WHERE chat_ID = {} AND id = {} ORDER BY RAND() LIMIT 1".format(update.message.chat_id,int(quote_id[1])) | |
else: | |
query = "SELECT message_ID FROM quote WHERE chat_ID = {} ORDER BY RAND() LIMIT 1".format(update.message.chat_id) | |
cursor.execute(query) | |
message_id=cursor.fetchone()[0] | |
query = "SELECT text FROM log WHERE message_id = {}".format(message_id) | |
# print (query) | |
cursor.execute(query) | |
row = cursor.fetchone() | |
quote = "__{}__".format(row[0]) | |
print (quote) | |
except Exception as e: | |
print (str(e)) | |
print(row) | |
bot.send_message(update.message.chat_id, quote, parse_mode='Markdown') | |
try: | |
updater = Updater(token=TOKEN) | |
dp = updater.dispatcher | |
log_handler = MessageHandler(Filters.all, logging) | |
flood_handler = MessageHandler(Filters.animation | Filters.photo | Filters.video, on_new_animation) | |
save_handler = CommandHandler("save", put_quote) | |
quote_handler = CommandHandler("quote", get_quote) | |
lauters_handler = CommandHandler("lauters", get_quote) | |
dp.add_handler(save_handler) | |
dp.add_handler(quote_handler) | |
dp.add_handler(lauters_handler) | |
dp.add_handler(flood_handler) | |
dp.add_handler(log_handler) | |
updater.start_polling() | |
updater.idle() | |
except Exception as e: | |
print (str(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment