lxxr / bitrixogram

simple async bitrix chat bot framework in "aiogram"-style
https://pypi.org/project/bitrixogram/
MIT License
2 stars 0 forks source link

Message handler exist #1

Closed TheHeadman closed 1 month ago

TheHeadman commented 2 months ago

Hello brother! I try to use bitrixogram, but get some issues: at first, commands registration func does not works, so I improve like that:

for command in reg_commands.commands: await bx.rest_command('imbot.command.register', { 'BOT_ID': config.short_bot_id, 'COMMAND': command['COMMAND'], 'COMMON': 'Y', 'HIDDEN': 'Y', 'EXTRANET_SUPPORT': 'N', 'CLIENT_ID': config.bitrix_bot_id, 'LANG': [{'LANGUAGE_ID': 'en', 'TITLE': command['TITLE'], 'PARAMS': command['PARAMS']}], 'EVENT_COMMAND_ADD': command['EVENT_COMMAND_ADD'] })

where config.short_bot_id is short bot id like "122", get from bitrix imbot.bot.list after registration

at second, I found its work correctly, when I use this:

bx = BitrixBot(config.bitrix_bot_endpoint, config.bitrix_bot_id, config.bitrix_bot_auth, session) instead of this: bx = BitrixBot(config.bitrix_bot_endpoint, config.bitrix_bot_auth, config.bitrix_bot_id, session) So client_id and client_secret need to be changed by place.

At third I have to ask: Do you have a sample for message_handler? Cause there no examples, and I cant manage income messages (only commands) by default any_handler.

Thanks!

lxxr commented 1 month ago

Dear TheHeadman,

Thank you for your detailed feedback. I appreciate the time you've taken to address these issues.

1.Command Registration Functionality:

Could you please provide the debug log? The command registration is a core function, and linking it to the local configuration may require further refinement. I believe enhancing this section to simplify the command format and using the base webhook address, such as:

'EVENT_COMMAND_ADD': self.base_url

This would allow the commands to be structured as follows:

commands = [
    {'COMMAND': 'inc', 'TITLE': '+', 'PARAMS': 'text'}
]
  1. Variable Placement in BitrixBot Initialization:

Could you send over the debug log for this as well? I encountered the following error when switching the variable positions:

{'error': 'BOT_ID_ERROR', 'error_description': 'Bot not found'}
  1. Message Handler Example:

There is a sample message handler with FSM States available in the README section. If you need to add a router before the any_router, you might want to review the following code snippet:

dp.add_router(messages_handler.message_router(bx))  # first router
dp.add_router(any_handler.any_router(bx))            # last router

Here’s an example of a message handler implementation:

from bitrixogram.core import Router, FSMContext, MagicFilter, Message, State, StatesGroup

F = MagicFilter()
router = Router()

class TestState(StatesGroup):
    test1_state = State()
    test2_state = State()

def some_router(bx):
    @router.message(TestState.test1_state, ((F.text() == "test2") | (F.text() == "test3")))
    async def handle_message_add_event_test_state1(message: Message, fsm: FSMContext):
        chat_id = message.get_chat_id()
        state = await fsm.get_state()
        print(f"get state test1: {state}")
        await bx.send_message(
            chat_id=chat_id,
            text="any router test - state1"
        )
        await fsm.set_state(TestState.test2_state)

    @router.message(TestState.test2_state, (F.text()))
    async def handle_message_add_event_test_state2(message: Message, fsm: FSMContext):
        chat_id = message.get_chat_id()
        state = await fsm.get_state()
        print(f"get state test2: {state}")
        await bx.send_message(
            chat_id=chat_id,
            text="any router test - state2"
        )
        await fsm.clear_state()

    @router.message(F.text() == "test")
    async def handle_message_add_event_test(message: Message, fsm: FSMContext):
        chat_id = message.get_chat_id()
        await fsm.set_state(TestState.test1_state)
        state = await fsm.get_state()
        print(f"set state: {state}")
        await bx.send_message(
            chat_id=chat_id,
            text="any router test"
        )

    @router.message(F.text()) # this is "any text" handler after all other handlers
    async def handle_message_add_event_other_text(message: Message, fsm: FSMContext):
        chat_id = message.get_chat_id()
        await bx.send_message(chat_id, "Any text handler")

    return router

I hope these suggestions help in resolving the issues you’re facing. Please let me know if you need further assistance or clarification.

Best regards, Aleksey Rublev

TheHeadman commented 1 month ago

Thank you brother! I found one issue in my server settings, which one drop messages before. I improve this, so finally it works well.