Skip to content

Instantly share code, notes, and snippets.

@Birdi7
Last active September 15, 2023 10:13
Show Gist options
  • Save Birdi7/d55e2d48dd085f6b959b89308d74494a to your computer and use it in GitHub Desktop.
Save Birdi7/d55e2d48dd085f6b959b89308d74494a to your computer and use it in GitHub Desktop.
aiogram to sync. author: @JrooTJunior
Save New Duplicate & Edit Just Text Twitter
"""
This example is under the same license as aiogram itself.
https://github.com/aiogram/aiogram/blob/dev-3.x/LICENSE
"""
from os import getenv
from typing import Any, Dict, Tuple
from requests import Session
from aiogram import Bot
from aiogram.methods import TelegramMethod, SendMessage
from aiogram.methods.base import TelegramType
from aiogram.types import InputFile
TELEGRAM_TOKEN = getenv("TELEGRAM_TOKEN")
CHAT_ID = getenv("CHAT_ID")
class RequestsWrapper:
def __init__(self, bot: Bot):
self.bot = bot
self.session = Session()
def _emit(self, method: TelegramMethod[TelegramType]) -> TelegramType:
data: Dict[str, Any] = {}
files: Dict[str, Tuple[str, InputFile]] = {}
for key, value in method.model_dump(warnings=False).items():
value = self.bot.session.prepare_value(value, bot=self.bot, files=files)
if not value:
continue
data[key] = value
for key, value in files.items():
raise NotImplementedError("Files are not supported yet")
# files[key] = (value.filename or key, value)
response = self.session.post(
self.bot.session.api.api_url(token=self.bot.token, method=method.__api_method__),
data=data,
files=files,
)
return self.bot.session.check_response(
bot=self.bot, method=method, status_code=response.status_code, content=response.text
)
def __call__(self, method: TelegramMethod[TelegramType]) -> TelegramType:
return self._emit(method)
def main():
bot = Bot(token=TELEGRAM_TOKEN)
client = RequestsWrapper(bot)
message = client(SendMessage(chat_id=CHAT_ID, text="Hello, world!"))
print(message)
if __name__ == "__main__":
main()
@Birdi7
Copy link
Author

Birdi7 commented Sep 15, 2023

автор @JrooTJunior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment