[+] Encapsulate sendRequest()
This commit is contained in:
+17
-6
@@ -1,11 +1,13 @@
|
|||||||
|
import difflib
|
||||||
|
import json
|
||||||
import re
|
import re
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
from telegram import Bot, Update
|
from telegram import Bot, Update
|
||||||
from telegram.ext import Updater, CallbackContext
|
from telegram.ext import Updater, CallbackContext, Job
|
||||||
|
|
||||||
from src.database import Database
|
from src.database import Database
|
||||||
from src.utils import toJson, create
|
from src.utils import toJson, create, dictToString
|
||||||
|
|
||||||
helpMsg = """
|
helpMsg = """
|
||||||
Welcome! This bot monitors http changes!
|
Welcome! This bot monitors http changes!
|
||||||
@@ -41,6 +43,15 @@ cache: {str: {str: str}} = {}
|
|||||||
updater: Updater = {}
|
updater: Updater = {}
|
||||||
|
|
||||||
|
|
||||||
|
def sendRequest(req: str):
|
||||||
|
r = create(req)
|
||||||
|
text = r.text
|
||||||
|
if r.headers['Content-Type'] == 'application/json':
|
||||||
|
text = dictToString(json.loads(text))
|
||||||
|
r.close()
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
# Initialize bot
|
# Initialize bot
|
||||||
def init(bot: Bot, u: Updater):
|
def init(bot: Bot, u: Updater):
|
||||||
global updater
|
global updater
|
||||||
@@ -133,12 +144,12 @@ def test(update: Update, context: CallbackContext):
|
|||||||
return "*Error:* %s doesn't exist." % name
|
return "*Error:* %s doesn't exist." % name
|
||||||
|
|
||||||
# Run
|
# Run
|
||||||
r = create(database.userRequests[user][name])
|
text = sendRequest(database.userRequests[user][name])
|
||||||
body = r.text
|
|
||||||
if len(body) > 60000:
|
if len(text) > 60000:
|
||||||
return "File too large (>60kb)."
|
return "File too large (>60kb)."
|
||||||
|
|
||||||
context.bot.send_document(chat_id=chat.id, document=BytesIO(bytes(r.text, 'utf-8')), filename=name + '.txt')
|
context.bot.send_document(chat_id=chat.id, document=BytesIO(bytes(text, 'utf-8')), filename=name + '.txt')
|
||||||
return 'Done!'
|
return 'Done!'
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user