def test_sync_bot_file_request(command_with_text_and_file, hostname, bot_id, sync_requests): bot = Bot() bot.register_cts(CTS(host=hostname, secret_key="secret")) m = Message(**command_with_text_and_file) assert len(bot.send_file(m.file.file, m.sync_id, m.bot_id, m.host)) == 2
def test_sync_bot_notification_request(command_with_text_and_file, hostname, bot_id, sync_requests): bot = Bot() bot.register_cts(CTS(host=hostname, secret_key="secret")) m = Message(**command_with_text_and_file) assert (len( bot._send_notification_result(m.body, [m.group_chat_id], m.bot_id, m.host, m.file, "all", [], [], [])) == 2)
def test_sync_bot_status_parsing(custom_router, custom_handler, custom_default_handler): custom_router.add_handler(custom_handler) custom_router.add_handler(custom_default_handler) bot = Bot() bot.add_commands(custom_router) assert bot.parse_status() == Status(result=StatusResult( commands=[custom_handler.to_status_command()]))
def test_sync_bot_work_with_disabled_credentials(sync_requests, command_with_text_and_file): bot = Bot(disable_credentials=True) def token_obtaining_mock(**data): raise Exception() bot._obtain_token = token_obtaining_mock m = Message(**command_with_text_and_file) bot.send_message(m.body, m.sync_id, m.bot_id, m.host)
def test_sync_answer_message(command_with_text_and_file, sync_requests): bot = Bot(disable_credentials=True) bot.start() message = Message(**command_with_text_and_file) bot.answer_message(message.body, message) bot.stop()
def test_setting_ui_flag_property_for_common_message() -> None: msg = Message.from_dict( { "sync_id": "a465f0f3-1354-491c-8f11-f400164295cb", "source_sync_id": "ff934be3-a03f-45d8-b315-738ba1ddec45", "command": { "body": "/cmd", "command_type": "user", "data": { "ui": True }, "metadata": { "account_id": 94 }, }, "file": None, "from": { "user_huid": None, "group_chat_id": "8dada2c8-67a6-4434-9dec-570d244e78ee", "ad_login": None, "ad_domain": None, "username": None, "chat_type": "group_chat", "host": "cts.ccteam.ru", "is_admin": False, "is_creator": False, }, "bot_id": "dcfa5a7c-7cc4-4c89-b6c0-80325604f9f4", }, Bot(), ) assert msg.source_sync_id == uuid.UUID( "ff934be3-a03f-45d8-b315-738ba1ddec45")
def bot(host, secret_key, bot_id, token): accounts = BotXCredentials( host=host, secret_key=secret_key, bot_id=bot_id, token=token, ) return Bot(bot_accounts=[accounts])
def test_sync_dispatcher_throws_bot_to_command(command_with_text_and_file): bot = Bot() bot.start() @bot.command def cmd(m, b): assert b == bot bot.parse_command(command_with_text_and_file) bot.stop()
def test_building_from_message(self, sending_message: SendingMessage) -> None: builder = MessageBuilder() msg = Message(message=builder.message, bot=Bot()) sending_msg = SendingMessage.from_message( text=sending_message.text, message=msg, ) assert sending_msg.host == msg.host assert sending_msg.sync_id == msg.sync_id assert sending_msg.bot_id == msg.bot_id
def test_sync_bot_token_obtaining(hostname, bot_id, sync_requests): bot = Bot() with pytest.raises(BotXException): bot._obtain_token(hostname, bot_id) cts = CTS(host=hostname, secret_key="secret") bot.register_cts(cts) bot._obtain_token(hostname, bot_id) assert bot._credentials.known_cts[cts.host] == ( cts, CTSCredentials(bot_id=bot_id, result="token_for_operations"), )
def test_sync_bot_start_and_shutdown(): bot = Bot() bot.start() bot.stop() with pytest.raises(RuntimeError): bot._dispatcher._pool.submit(print, 1)
def test_sync_bot_token_obtaining_with_errored_request(hostname, bot_id, sync_error_requests): bot = Bot() cts = CTS(host=hostname, secret_key="secret") bot.register_cts(cts) bot._obtain_token(hostname, bot_id) assert bot._credentials.known_cts[cts.host][1] is None
def test_sync_bot_error_requests(command_with_text_and_file, hostname, bot_id, sync_error_requests): bot = Bot() bot.register_cts(CTS(host=hostname, secret_key="secret")) m = Message(**command_with_text_and_file) assert bot.send_message(m.body, m.sync_id, m.bot_id, m.host) != 200 assert bot.send_file(m.file.file, m.sync_id, m.bot_id, m.host)[1] != 200
def test_sync_bot_message_as_command_sending(hostname, bot_id, command_with_text_and_file, sync_requests): command_array = [] notification_array = [] def custom_command_sending(text, chat_id, bot_id, host, file, recipients, mentions, bubble, keyboard): command_array.append((text, chat_id, bot_id, host, file, recipients, mentions, bubble, keyboard)) def custom_notification_sending(text, group_chat_ids, bot_id, host, file, recipients, mentions, bubble, keyboard): notification_array.append(( text, group_chat_ids, bot_id, host, file, recipients, mentions, bubble, keyboard, )) bot = Bot() bot.register_cts(CTS(host=hostname, secret_key="secret")) bot._send_command_result = custom_command_sending bot._send_notification_result = custom_notification_sending m = Message(**command_with_text_and_file) bot.send_message(m.body, m.sync_id, m.bot_id, m.host, file=m.file.file) assert len(command_array) == 1 assert command_array[0] == ( m.body, m.sync_id, m.bot_id, m.host, m.file, "all", [], [], [], )
def test_message_is_proxy_to_incoming_message(incoming_message) -> None: msg = Message.from_dict(incoming_message.dict(), Bot()) assert msg.sync_id == incoming_message.sync_id assert msg.command == incoming_message.command assert msg.file == incoming_message.file assert msg.user == incoming_message.user assert msg.bot_id == incoming_message.bot_id assert msg.body == incoming_message.command.body assert msg.data == {**msg.metadata, **incoming_message.command.data_dict} assert msg.metadata == incoming_message.command.metadata assert msg.user_huid == incoming_message.user.user_huid assert msg.ad_login == incoming_message.user.ad_login assert msg.group_chat_id == incoming_message.user.group_chat_id assert msg.chat_type == incoming_message.user.chat_type assert msg.host == incoming_message.user.host assert msg.credentials.sync_id == incoming_message.sync_id assert msg.credentials.bot_id == incoming_message.bot_id assert msg.credentials.host == incoming_message.user.host assert msg.entities == incoming_message.entities assert msg.incoming_message == incoming_message
from uuid import UUID from fastapi import FastAPI from starlette.status import HTTP_202_ACCEPTED from botx import Bot, BotXCredentials, IncomingMessage, Message, Status users_data = {} bot = Bot(bot_accounts=[ BotXCredentials( host="cts.example.com", secret_key="secret", bot_id=UUID("bot_id")) ]) @bot.default(include_in_status=False) async def echo_handler(message: Message) -> None: await bot.answer_message(message.body, message) @bot.handler async def fill_info(message: Message) -> None: if message.user_huid not in users_data: text = ("Hi! I'm a bot that will ask some questions about you.\n" "First of all: what is your name?") else: text = ( "You've already filled out information about yourself.\n" "You can view it by typing `/my-info` command.\n" "You can also view the processed information by typing `/info` command." )
from botx import Bot from .a_commands import collector bot = Bot() bot.include_collector(collector) @bot.default(include_in_status=False) async def default_handler() -> None: print("default handler")
def test_sync_bot_command_executing(command_with_text_and_file, custom_handler): bot = Bot() bot.add_handler(custom_handler) assert bot.parse_command(command_with_text_and_file)
def test_setting_ui_flag_property_for_system_message(incoming_message) -> None: msg = Message.from_dict(incoming_message.dict(), Bot()) assert not msg.source_sync_id
def test_sync_bot_init(hostname): bot = Bot() assert bot._dispatcher is not None assert bot._credentials == BotCredentials()
from typing import Any from uuid import UUID from bot.config import BOT_SECRET, CTS_HOST from bot.handlers import FSMStates, fsm from bot.middleware import FlowError, FSMMiddleware, change_state from botx import Bot, BotXCredentials, Message bot = Bot(bot_accounts=[ BotXCredentials( host=CTS_HOST, secret_key=str(BOT_SECRET), bot_id=UUID("bot_id")) ]) bot.add_middleware(FSMMiddleware, bot=bot, fsm=fsm) @bot.default(include_in_status=False) async def default_handler(message: Message) -> None: if message.body == "start": change_state(message, FSMStates.get_first_name) await message.bot.answer_message("enter first name", message) return await message.bot.answer_message("default handler", message) @bot.exception_handler(FlowError) async def flow_error_handler(*_: Any) -> None: pass
from uuid import UUID from botx import Bot, Message, SendingMessage bot = Bot() CHAT_FOR_MENTION = UUID("369b49fd-b5eb-4d5b-8e4d-83b020ff2b14") USER_FOR_MENTION = UUID("cbf4b952-77d5-4484-aea0-f05fb622e089") @bot.handler async def my_handler_with_user_mention(message: Message) -> None: reply = SendingMessage.from_message( text="Hi! There is a notification with mention for you", message=message, ) reply.mention_user(message.user_huid) await bot.send(reply) @bot.handler async def my_handler_with_chat_mention(message: Message) -> None: reply = SendingMessage.from_message(text="Check this chat", message=message) reply.mention_chat(CHAT_FOR_MENTION, name="Interesting chat") await bot.send(reply) @bot.handler async def my_handler_with_contact_mention(message: Message) -> None: reply = SendingMessage.from_message(
from uuid import UUID from fastapi import FastAPI from starlette.status import HTTP_202_ACCEPTED from botx import Bot, BotXCredentials, IncomingMessage, Message, Status from botx.middlewares.ns import NextStepMiddleware, register_next_step_handler bot = Bot(bot_accounts=[BotXCredentials(host="cts.example.com", secret_key="secret", bot_id=UUID("bot_id"))]) users_data = {} async def get_name(message: Message) -> None: users_data[message.user_huid]["name"] = message.body await bot.answer_message("Good! Move next: how old are you?", message) register_next_step_handler(message, get_age) async def get_age(message: Message) -> None: try: age = int(message.body) if age <= 2: await bot.answer_message( "Sorry, but it's not true. Say your real age, please!", message, ) register_next_step_handler(message, get_age) else: users_data[message.user_huid]["age"] = age await bot.answer_message("Got it! Final question: your gender?", message) register_next_step_handler(message, get_gender)