# File: inline.py # Description: Inline wrapper # Author: Arvs100 # Date: 25-12-2019 ~ 28-12-2019 from telegram.ext import Updater, InlineQueryHandler, CommandHandler import logging from telegram import InlineQueryResultArticle, InputTextMessageContent, Bot import uuid import requests import password BOT_VERSION = "0.3" class Command: def __init__(self): self.desc = "" self.name = "" self.headerField = "" self.mode = "" def setMode(self, mode): self.mode = mode def getMode(self): return self.mode def setDesc(self, desc): self.desc = desc def getDesc(self): return self.desc def getName(self): return self.name def setName(self, name): self.name = name def getCommand(self): return self.command def setHeaderField(self, param): self.headerField = param def getHeaderField(self): return self.headerField class InlineBot: def __init__(self): self.apiUrl = password.API_URL self.errorText = "Sowwy mawster, an ewror occuwed. ;;w;;" self.commands = [] #self.__loadConfig() ## todo v0.3.1 self.__makeCommands() def __makeCommands(self): cmd = Command() cmd.setName("Pony") cmd.setDesc("yay, ponies!") cmd.setMode("imageurl") cmd.setHeaderField("imageurl") self.commands.append(cmd) cmd.setName("Clop") cmd.setDesc("NSFW ponies") self.commands.append(cmd) cmd.setName("Furry") cmd.setDesc("Furries") self.commands.append(cmd) cmd.setName("Loli") cmd.setDesc("Cute lolis") self.commands.append(cmd) cmd.setName("Yiff") cmd.setDesc("Yiff") self.commands.append(cmd) cmd.setName("Lewd") cmd.setDesc("Say hi to the police") self.commands.append(cmd) cmd.setName("Rasoio") cmd.setDesc("Dai facciamogli lo scherzo del rasoio!1!!") cmd.setMode("genericurl") cmd.setHeaderField("htmldescription") self.commands.append(cmd) def callRequest(self, command, senderid, text = ""): if not command: return self.errorText r = requests.get(self.apiUrl, headers={ "botid":password.BOT_ID, "botpassword":password.BOT_PASSWORD, "command":command.getName().lower(), "replytouserid":"0", "senderuserid":senderid, "receiverchatid":"0", "querytext":text } ) if not r: return self.errorText if not "authorized" in r.headers or not "success" in r.headers or not "htmldescription" in r.headers or not "imageurl" in r.headers or not requiredField in r.headers: return self.errorText if r.headers["authorized"] != "true" or r.headers["success"] != "true" or r.headers[requiredField] == "": return self.errorText return r.headers[command.getHeaderField()] def start(self): self.bot = Bot(token=password.BOT_TOKEN) self.updater = Updater(bot=self.bot,use_context=True) print("Inline bot v. {}".format(BOT_VERSION)) self.dispatcher = self.updater.dispatcher logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) self.logger = logging.getLogger(__name__) self.dispatcher.add_error_handler(self.errorHandler) inline_caps_handler = InlineQueryHandler(self.inlineQuery) self.dispatcher.add_handler(inline_caps_handler) print("Started!") self.updater.start_polling() self.updater.idle() def errorHandler(self, update, context): """Log Errors caused by Updates.""" self.logger.warning('Update "%s" caused error "%s"', update, context.error) def commandStart(self, update, context): context.bot.send_message(chat_id=update.effective_chat.id, text="Inline cancer, clop, furry spammer (h0nus)",parse_mode="HTML") def makeResult(self, command, senderid, querySearch = ""): if commnand == None: return InlineQueryResultArticle( id=uuid.uuid4().hex, type="article", title="Error", input_message_content=InputTextMessageContent("Error", "HTML"), description=self.errorText ) qValue = self.callRequest(command, senderid, querySearch) contentText = "" if command.getMode() == "imageurl": contentText = InputTextMessageContent(""HonusBot", "HTML") elif command.getMode() == "genericurl": contentText = InputTextMessageContent(""link", "HTML") else: contentText = InputTextMessageContent("Error", "HTML") return InlineQueryResultArticle( id=uuid.uuid4().hex, type="article", title=command.getName(), input_message_content=contentText description=command.getDesc() ) def inlineQuery(self, update, context): senderid = str(update.inline_query.from_user.id) query = update.inline_query.query results = list() if not query: for command in self.commands: results.append(self.makeResult(command, senderid) else: results.append(self.makeResult(self.cancerCommand, senderid, query)) context.bot.answer_inline_query(update.inline_query.id, results, 1, False) InlineBot().start()