jsonupdate

This commit is contained in:
Christian Rendina 2020-05-05 12:02:41 +02:00
parent 44b27accac
commit 576b6adf28
2 changed files with 71 additions and 26 deletions

View File

@ -2,6 +2,7 @@
Url=
Id=
Password=
UserAgent=
[Bot]
Token=

View File

@ -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
@ -83,6 +115,9 @@ class Config:
def getBotConnectionPool(self):
return self.botConnectionPool
def getUserAgent(self):
return self.userAgent
class InlineBot:
def __init__(self):
self.errorText = "Sowwy mawster, an ewror occuwed. ;;w;;"
@ -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
}
)
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:
#if not r:
return self.errorText
if not "authorized" in r.headers or not "success" in r.headers or not command.getHeaderField() in r.headers:
if len(r.text) < 1:
return self.errorText
if r.headers["authorized"] != "true" or r.headers["success"] != "true" or r.headers[command.getHeaderField()] == "":
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
return r.headers[command.getHeaderField()]
txt = 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()))