Refactor: v0.3 merge

This commit is contained in:
Christian Rendina 2020-03-09 01:05:49 +01:00
commit a50e046583
1 changed files with 111 additions and 25 deletions

136
inline.py
View File

@ -9,29 +9,95 @@ import uuid
import requests import requests
import password 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: class InlineBot:
def __init__(self): def __init__(self):
self.apiUrl = password.API_URL self.apiUrl = password.API_URL
self.errorText = "Sowwy mawster, an ewror occuwed. ;;w;;" self.errorText = "Sowwy mawster, an ewror occuwed. ;;w;;"
self.commands = []
self.commands = { #self.__loadConfig() ## todo v0.3.1
"Pony" : "yay, ponies!", self.__makeCommands()
"Clop" : "NSFW ponies",
"Furry" : "Furries"
"Loli" : "Cute lolis",
"Yiff" : "Yiff",
"Lewd" : "Say hi to the police",
"Rasoio" : "Dai facciamogli lo scherzo del rasoio!1!!"
}
def callRequest(self, command, senderid, text): 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, r = requests.get(self.apiUrl,
headers={ headers={
"botid":password.BOT_ID, "botid":password.BOT_ID,
"botpassword":password.BOT_PASSWORD, "botpassword":password.BOT_PASSWORD,
"command":command, "command":command.getName().lower(),
"replytouserid":"0", "replytouserid":"0",
"senderuserid":senderid, "senderuserid":senderid,
"receiverchatid":"0", "receiverchatid":"0",
@ -42,19 +108,19 @@ class InlineBot:
if not r: if not r:
return self.errorText 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: 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 return self.errorText
if r.headers["authorized"] != "true" or r.headers["success"] != "true" or r.headers["imageurl"] == "": if r.headers["authorized"] != "true" or r.headers["success"] != "true" or r.headers[requiredField] == "":
return self.errorText return self.errorText
return r.headers["imageurl"] return r.headers[command.getHeaderField()]
def start(self): def start(self):
self.bot = Bot(token=password.BOT_TOKEN) self.bot = Bot(token=password.BOT_TOKEN)
self.updater = Updater(bot=self.bot,use_context=True) self.updater = Updater(bot=self.bot,use_context=True)
print("h0nus bot\n"); print("Inline bot v. {}".format(BOT_VERSION))
self.dispatcher = self.updater.dispatcher self.dispatcher = self.updater.dispatcher
@ -65,7 +131,9 @@ class InlineBot:
inline_caps_handler = InlineQueryHandler(self.inlineQuery) inline_caps_handler = InlineQueryHandler(self.inlineQuery)
self.dispatcher.add_handler(inline_caps_handler) self.dispatcher.add_handler(inline_caps_handler)
print("Started!") print("Started!")
self.updater.start_polling() self.updater.start_polling()
self.updater.idle() self.updater.idle()
@ -75,14 +143,33 @@ class InlineBot:
def commandStart(self, update, context): 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") 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
)
def makeResult(self, qTitle, qDesc, picUrl): qValue = self.callRequest(command, senderid, querySearch)
contentText = ""
if command.getMode() == "imageurl":
contentText = InputTextMessageContent(""<a href=\"" + qValue + "\">HonusBot</a>", "HTML")
elif command.getMode() == "genericurl":
contentText = InputTextMessageContent(""<a href=\"" + qValue + "\">link</a>", "HTML")
else:
contentText = InputTextMessageContent("Error", "HTML")
return InlineQueryResultArticle( return InlineQueryResultArticle(
id=uuid.uuid4().hex, id=uuid.uuid4().hex,
type="article", type="article",
title=qTitle, title=command.getName(),
input_message_content=InputTextMessageContent("<a href=\"" + picUrl + "\">H0nusBot</a>", "HTML"), input_message_content=contentText
description=qDesc description=command.getDesc()
) )
def inlineQuery(self, update, context): def inlineQuery(self, update, context):
@ -90,11 +177,10 @@ class InlineBot:
query = update.inline_query.query query = update.inline_query.query
results = list() results = list()
if not query: if not query:
for key in self.commands: for command in self.commands:
desc = self.commands[key] results.append(self.makeResult(command, senderid)
results.append(self.makeResult(key, desc, self.callRequest(key.lower(), senderid, query)))
else: else:
results.append(self.makeResult("Cancer", "Search cancer about " + query, self.callRequest("cancer", senderid, query))) results.append(self.makeResult(self.cancerCommand, senderid, query))
context.bot.answer_inline_query(update.inline_query.id, results, 1, False) context.bot.answer_inline_query(update.inline_query.id, results, 1, False)
InlineBot().start() InlineBot().start()