[Python] Calling an inline bot via telethon

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to solve a problem for the second day. I'm trying to call an inline bot in Telegram using the Telethon library in Python. I need the script to run under a user account and send messages as if it were a real person.

    The problem is that Telethon doesn't allow to perfectly mimic the behaviour of a user when calling an inline bot. The results of the request are successfully received, but I can't get Telegram to display them with ‘via @bot_username’ at the top of the message, as it happens with a normal inline bot. Perhaps someone knows a workaround for this or some other ways to implement the inline bot call?

    Here's a snippet of my code:

    try:
    # Получаем результаты инлайн-запроса
    results = await client(GetInlineBotResultsRequest(
    bot=await client.get_entity(bot_username),
    peer=await event.get_input_chat(),
    query=symbol,
    offset=''
    ))

    if results.results:
    # Отправляем первый результат
    await client.send_message(
    entity=event.chat_id,
    message=results.results[0].id,
    reply_to=event.id
    )
    print(f"Успешно отправлен инлайн-результат для {symbol}")
    else:
    print("Бот не вернул результатов")

    except Exception as e:
    print(f"Ошибка вызова бота: {str(e)[:100]}")

    @client.on(events.NewMessage(incoming=True))
    async def handler(event):
    if event.out or not event.text:
    return

    # Проверка ключевых слов
    if any(kw in event.raw_text.lower() for kw in keywords):
    try:
    # Искусственная задержка
    await asyncio.sleep(1.5)

    # Определяем криптовалюту
    symbol = await detect_crypto(event.raw_text)
    if not symbol:
    return

    # Вызываем инлайн-бота
    await call_inline_bot(event, symbol)

    except Exception as e:
    print(f"Ошибка обработки: {e}")



    Problem Statement:
    The script fails to properly trigger the inline bot (@Cryptocurrency_rate_robot) to display its formatted response (price, charts, etc.) when a user asks about cryptocurrencies. Instead of showing the bot’s rich inline result, it sends the raw text @Cryptocurrency_rate_robot SYMBOL, which does not activate the bot’s expected behavior.

    What We Tried


    1. Inline Bot Activation Logic:

      results = await client(GetInlineBotResultsRequest(
      bot=bot_entity,
      peer=event.chat_id,
      query=symbol,
      offset=''
      ))
      await client.send_message(event.chat_id, message=results.results[0].id, reply_to=event.id)
      • Goal: Fetch the inline bot’s response for SYMBOL (e.g., SOL) and send it automatically.
      • Expectation: The bot’s formatted message (price, 24h change, etc.) would appear as if the user manually triggered it.

    2. Gemini Integration:
      • Used Gemini to detect cryptocurrency symbols (e.g., mapping "солан" → SOL).
      • Expectation: Correctly identify the symbol to pass to the inline bot.
    What Happened

    • Observed Behavior:
      • The script sends the literal text @Cryptocurrency_rate_robot SYMBOL (e.g., @Cryptocurrency_rate_robot SOL).
      • The inline bot’s formatted response (price card, charts) does not appear.
      • Errors like 'BotInlineResult' object is not iterable or silent failures occur.

    Continue reading...

Compartilhe esta Página