Created
August 6, 2019 14:39
-
-
Save gschanuel/0ed72305795eee22bd6a022fb9763100 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 | |
data = [] | |
def logger(bot, update): | |
msg=str(update.message.chat_id)+":"+str(update.message.from_user.id)+":"+str(update.message.message_id) | |
print ("[!][logger] " + msg) | |
data.append(msg) | |
noflood(bot, update) | |
Timer(msg_interval, noflood, [bot, update]).start() | |
def deleteMsgs(bot, update, msgIDs): | |
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() | |
query = "SELECT text FROM xingamento ORDER BY RAND() LIMIT 1" | |
cursor.execute(query) | |
row = cursor.fetchone() | |
quote = "__{}__".format(row[0]) | |
msg="[!] Cala a boca " + str(update.message.from_user.first_name) + ", " + str(quote) + "!" | |
bot.send_message(chat_id,msg) | |
def deleteMsg(bot, update, msgId): | |
chat_id=update.message.chat_id | |
print ("[!][deleteMsg] Deleting => " + str(msgId)) | |
bot.delete_message(chat_id=chat_id, message_id=msgId) | |
def noflood(bot, update): | |
chat_id=str(update.message.chat_id) | |
user_id=str(update.message.from_user.id) | |
counter = 0 | |
msgIds = [] | |
print ("[!] NoFlood check") | |
for i, item in enumerate(data): | |
print(item) | |
if (chat_id + ":" + user_id) in item: | |
msgIds.append((data[i].split(":")[2])) | |
counter += 1 | |
if counter >= msg_flood: | |
print ("[!] NoFlood action") | |
data.clear() | |
deleteMsgs(bot, update,msgIds) | |
def on_new_animation(bot, update): | |
user_id=str(update.message.from_user.id) | |
chat_id=str(update.message.chat_id) | |
msg_id=str(update.message.message_id) | |
msg=chat_id+":"+user_id+":"+msg_id | |
print ("[!][new_msg] " + msg) | |
data.append(msg) | |
check_flood(bot, update) | |
def check_flood(bot, update): | |
try: | |
chat_id=str(update.message.chat_id) | |
user_id=str(update.message.from_user.id) | |
msg_id=str(update.message.message_id) | |
counter = 0 | |
msgIds = [] | |
for i, item in enumerate(data): | |
if (chat_id + ":" + user_id) in item: | |
msgIds.append((data[i].split(":")[2])) | |
counter += 1 | |
if counter >= msg_flood: | |
deleteMsgs(bot, update, msgIds[msg_flood:]) | |
msg="[!] Cala a boca " + str(update.message.from_user.first_name) + ", " + str(random.choice(xinga)) + "!" | |
bot.send_message(chat_id,msg) | |
for msg, msg_item in enumerate(msgIds): | |
for i, item in enumerate(data): | |
if (chat_id + ":" + user_id + ":" + str(msg)) in item: | |
break | |
except Exception as e: | |
print (str(e)) | |
def logging(bot, update): | |
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 ) | |
print(query) | |
cursor.execute(query) | |
con.commit() | |
except Exception as e: | |
print (str(e)) | |
def put_quote(bot, update): | |
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) | |
try: | |
cursor.execute(query) | |
con.commit() | |
print ("OK") | |
except Exception as e: | |
print (str(e)) | |
def get_quote(bot, update): | |
print(str(update)) | |
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) | |
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') | |
updater = Updater(token=TOKEN) | |
dp = updater.dispatcher | |
log_handler = MessageHandler(Filters.all, logging) | |
flood_handler = MessageHandler(Filters.animation | Filters.photo | Filters.video, logger) | |
dp.add_handler(CommandHandler("save", put_quote)) | |
dp.add_handler(CommandHandler("quote", get_quote)) | |
dp.add_handler(CommandHandler("lauters", get_quote)) | |
dp.add_handler(flood_handler) | |
dp.add_handler(log_handler) | |
updater.start_polling() | |
updater.idle() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment