Telegram integration with rasa

I made my bot connect to telegram and right now it is working. But i have different flows, if the bot is in a group or in a individual chat. I tried to retrieve some information and read the telegram API but only got more confused on how to do it. During a flow, i have an action to see where the bot is at.

  - story: Greet user
    steps:
      - intent: start
      - action: action_check_channel

If the bot is in telegram, i would like to now if it is in a group chat or not, so i did this request:

https://api.telegram.org/bot${TELEGRAM_TOKEN}/getChat

The Response i got:

{"ok":false,"error_code":400,"description":"Bad Request: chat_id is empty"}

Going to post the solution i found:

Implemented a custom input channel in a file called custom_telegram.py the same folder that the credentials.yml is, with the content:

from rasa.core.channels import TelegramInput
from typing import Text

class TelegramInputChannel(TelegramInput):
    def get_metadata(self, request):
        metadata=request.json
        return metadata

Then, edited my credentials.yml using:

custom_telegram.TelegramInputChannel:
  access_token: "token provided by father bot"
  verify: "bot_username"
  webhook_url: "https://address/webhooks/telegram/webhook"

After that, it was possible to access in actions file:

    telegram_username = tracker.latest_message["metadata"]["from"]["username"] #user that sent the message
    telegram_chat_type = tracker.latest_message["metadata"]["chat"]["type"]
    if telegram_chat_type == 'group':
        is_group = True
    return [
                SlotSet("is_in_telegram_group", is_group),
            ]
4 Likes

hello @fabiolamfleury , Can you help with an issue that i have, i am trying to connect rasa with telegram (am not using ngrok because i have an accessible ip ) but i have error 423 : too many requests retry after I am using docker rasa-x how can i do ? is it a port problem ? thanks @fabiolamfleury