async def test(): telegram = Telegram(token="token", session=aiohttp.ClientSession()) telegram.api_messages_lock = asyncio.Lock() async def req(method, kwargs): requests.append((method, kwargs)) telegram._request = req attachment = Attachment.new(b"file") await telegram.execute_send(1, "", attachment, {}) attachment = Attachment.new(b"file", type="doc") await telegram.execute_send(1, "", attachment, {}) attachment = Attachment.new(b"file", type="voice") await telegram.execute_send(1, "", attachment, {}) assert len(requests) == 3 assert requests[0] == ("sendPhoto", {"chat_id": "1", "photo": b"file"}) assert requests[1] == ("sendDocument", { "chat_id": '1', "document": b'file' }) assert requests[2] == ("sendVoice", {"chat_id": '1', "voice": b'file'})
async def test(): telegram = Telegram(token="token", session=aiohttp.ClientSession()) telegram.api_messages_lock = asyncio.Lock() attachment = Attachment.new(b"bruh", type="location") with pytest.raises(ValueError): await telegram.execute_send(1, "", attachment, {})
async def test(): telegram = Telegram(token="token", session=aiohttp.ClientSession()) async def req(method, kwargs={}): if method == "getFile" and kwargs["file_id"] == "file_id": return {"file_path": "123"} telegram._request = req assert await telegram._request_file("file_id") == b"content" assert await telegram._make_getter("file_id")() == b"content"
def test_execute_request(): telegram = Telegram(token="token") async def req(method, kwargs): assert method == "method" assert kwargs["arg"] == "val" return 1 telegram._request = req result = asyncio.get_event_loop().run_until_complete( telegram.execute_request("method", {"arg": "val"})) assert result == 1
async def test(): telegram = Telegram(token="token", session=aiohttp.ClientSession()) async def _req_f(file_id): return b"content" telegram._request_file = _req_f for k, v in ATTACHMENTS.items(): attachment = telegram._make_attachment(*v) assert attachment.type == k assert attachment.file is None if k == "location": continue assert await attachment.get_file() == b"content"
async def test(): telegram = Telegram(token="token") assert await telegram._request("method1", {"arg": "val1"}) == 1 with pytest.raises(RequestException): await telegram._request("method2", {"arg": "val2"}) await telegram.session.close()
async def test(): telegram = Telegram(token="token", session=aiohttp.ClientSession()) await telegram.acquire_updates(None) with pytest.raises(asyncio.CancelledError): await telegram.acquire_updates(None) await telegram.acquire_updates(None) await telegram.session.close()
async def test(): mock.return_value = "OK" tg = Telegram("token") tg.api_messages_lock = asyncio.Lock() resp = await tg.request("method", arg1="val1") assert resp == "OK" mock.assert_awaited_with("method", {"arg1": "val1"}) resp = await tg.send_message("user1", "msg", arg1="val1") assert resp == ["OK"] mock.assert_awaited_with( "sendMessage", { "chat_id": "user1", "text": "msg", "arg1": "val1" }, )
def add_backends(app, backends): for backend in backends or []: kwargs = {k: v for k, v in backend.items() if k != "kind"} name = kwargs.get("name") if name and app.get_backend(name): logger.logger.warning(f"Duplicated backend name: {name}") if backend["kind"] == "vk" and "address" in backend: app.add_backend(VkontakteCallback(**kwargs)) elif backend["kind"] == "vk": app.add_backend(Vkontakte(**kwargs)) elif backend["kind"] == "tg": app.add_backend(Telegram(**kwargs)) else: logger.logger.warning(f"Unknown backend kind: {backend['kind']}")
def test_no_token(): with pytest.raises(ValueError): Telegram("")
from ai_dungeon import config from ai_dungeon.storage.redis import RedisStorage from kutana import Kutana, load_plugins from kutana.backends import Vkontakte, Telegram import logging logging.basicConfig(level=logging.DEBUG) # Create application if config.REDIS_URL is not None: app = Kutana(storage=RedisStorage(config.REDIS_URL)) else: app = Kutana() # Add manager to application if config.TELEGRAM_TOKEN: app.add_backend(Telegram(token=config.TELEGRAM_TOKEN)) if config.VK_TOKEN: app.add_backend(Vkontakte(token=config.VK_TOKEN)) # Load and register plugins app.add_plugins(load_plugins("plugins/")) if __name__ == "__main__": # Run application app.run()
import os from pathlib import Path from kutana import Kutana, load_plugins from kutana.backends import Telegram import dotenv env = dotenv.dotenv_values() if Path(".env").exists() else os.environ # Create application app = Kutana() # Add manager to application app.add_backend(Telegram(env.get("TELEGRAM_TOKEN"), proxy=env.get("PROXY"))) # Load and register plugins app.add_plugins(load_plugins("plugins/")) if __name__ == "__main__": # Run application app.run()
import json from kutana import Kutana, load_plugins from kutana.backends import Vkontakte, Telegram # Import configuration with open("configuration.json") as fh: config = json.load(fh) # Create application app = Kutana() # Set config app.config.update({ "vk_chat_id": config["vk_chat_id"], "tg_chat_id": config["tg_chat_id"] }) # Add manager to application app.add_backend(Telegram(token=config["tg_token"])) app.add_backend(Vkontakte(token=config["vk_token"])) # Load and register plugins app.add_plugins(load_plugins("plugins/")) if __name__ == "__main__": # Run application app.run()