From 576b6adf282513e138048eaf4e7e1198b8affb19 Mon Sep 17 00:00:00 2001 From: Arves100 Date: Tue, 5 May 2020 12:02:41 +0200 Subject: [PATCH] jsonupdate --- config.ini.example | 1 + inline.py | 96 +++++++++++++++++++++++++++++++++------------- 2 files changed, 71 insertions(+), 26 deletions(-) diff --git a/config.ini.example b/config.ini.example index 842b493..1bed88a 100644 --- a/config.ini.example +++ b/config.ini.example @@ -2,6 +2,7 @@ Url= Id= Password= +UserAgent= [Bot] Token= diff --git a/inline.py b/inline.py index f254584..d256fde 100644 --- a/inline.py +++ b/inline.py @@ -1,15 +1,16 @@ # File: inline.py # Description: Inline wrapper # Author: Arves100 -# Date: 25-12-2019 ~ 28-03-2020 +# Date: 25-12-2019 ~ 28-03-2020 . 29-04-2020 from telegram.ext import Updater, InlineQueryHandler, CommandHandler import logging from telegram import InlineQueryResultArticle, InputTextMessageContent, Bot import uuid import requests from telegram.utils.request import Request +import json -BOT_VERSION = "0.3" +BOT_VERSION = "0.4" class Command: def __init__(self): @@ -51,6 +52,36 @@ class Command: def getHeaderField(self): return self.headerField +class JSONRequest(object): + def __init__(config, command, senderId, text): + self.botName = config.getApiId() + self.botPassword = config.getApiPassword() + self.command = command + self.replyToUserId = 0 + self.senderUserId = senderId + self.chatId = 0 + self.queryText = text + self.rawSingleArgument = False + +class JSONResponse(object): + def __init__(self, auth, success, img, dsc): + self.auth = auth + self.success = success + self.imgUrl = img + self.htmlDesc = dsc + + def isAuthorized(): + return self.auth + + def succeeded(): + return self.success + + def getHtmlDesc(): + return self.htmlDesc + + def getImageUrl(): + return self.imgUrl + class Config: def __init__(self): self.apiUrl = "" @@ -67,6 +98,7 @@ class Config: self.apiPassword = cfg["Api"]["Password"] self.botToken = cfg["Bot"]["Token"] self.botConnectionPool = cfg["Bot"]["ConnectionPool"] + self.userAgent = cfg["Api"]["UserAgent"] def getApiUrl(self): return self.apiUrl @@ -82,6 +114,9 @@ class Config: def getBotConnectionPool(self): return self.botConnectionPool + + def getUserAgent(self): + return self.userAgent class InlineBot: def __init__(self): @@ -97,41 +132,50 @@ class InlineBot: self.config.read("config.ini") def __makeCommands(self): - self.commands.append(Command("Pony", "yay, ponies!", "imageurl", "imageurl")) - self.commands.append(Command("Clop", "NSFW ponies", "imageurl", "imageurl")) - self.commands.append(Command("Furry", "Furries", "imageurl", "imageurl")) - self.commands.append(Command("Loli", "Cute lolis", "imageurl", "imageurl")) - self.commands.append(Command("Yiff", "Yiff", "imageurl", "imageurl")) - self.commands.append(Command("Lewd", "Say hi to the police", "imageurl", "imageurl")) - self.commands.append(Command("Rasoio", "Dai facciamogli lo scherzo del rasoio!1!!", "genericurl", "htmldescription")) + self.commands.append(Command("Pony", "yay, ponies!", "imageurl", "imageUrl")) + self.commands.append(Command("Clop", "NSFW ponies", "imageurl", "imageUrl")) + self.commands.append(Command("Furry", "Furries", "imageurl", "imageUrl")) + self.commands.append(Command("Loli", "Cute lolis", "imageurl", "imageUrl")) + self.commands.append(Command("Yiff", "Yiff", "imageurl", "imageUrl")) + self.commands.append(Command("Lewd", "Say hi to the police", "imageurl", "imageUrl")) + self.commands.append(Command("Rasoio", "Dai facciamogli lo scherzo del rasoio!1!!", "genericurl", "htmlDescription")) self.cancerCommand = Command("Cancer", "Search cancer", "imageurl", "imageurl") + def asResponse(dct): + if "imageUrl" in dct: + return JSONResponse(dct["authorized"], dct["success"], dct["imageUrl"], "") + else if "htmlDescription" in dct: + return JSONResponse(dct["authorized"], dct["success"], "", dct["htmlDescription"]) + else: + raise Exception("Invalid JSON") + def callRequest(self, command, senderid, text = ""): if not command: return self.errorText - - r = requests.get(self.config.getApiUrl(), - headers={ - "botid":self.config.getApiId(), - "botpassword":self.config.getApiPassword(), - "command":command.getName().lower(), - "replytouserid":"0", - "senderuserid":senderid, - "receiverchatid":"0", - "querytext":text - } - ) - if not r: + r = requests.post(self.config.getApiUrl(), headers = { "Content-Type" : "application/json", "User-Agent" : self.config.getUserAgent() }, data = json.JSONEncoder().encode(JSONRequest(self.config, command, senderid, text).__dict__)) + + #if not r: + return self.errorText + + if len(r.text) < 1: return self.errorText - if not "authorized" in r.headers or not "success" in r.headers or not command.getHeaderField() in r.headers: + resp = None + try: + resp = json.loads(r.text, object_hook = asResponse) + except: + return self.errorText ## Invalid json + + if not resp.isAuthorized() or not resp.succeeded(): return self.errorText - if r.headers["authorized"] != "true" or r.headers["success"] != "true" or r.headers[command.getHeaderField()] == "": - return self.errorText + txt = r.headers[command.getHeaderField()] - return r.headers[command.getHeaderField()] + if len(txt) < 1: + return self.errorText ## Wrong api? + + return txt def start(self): self.prequest = Request(con_pool_size=int(self.config.getBotConnectionPool()))