skip to Main Content

I’m building a bot that has to receive a group as an input from a user, so that it knows what group the user wants to do some X action. How to do that?

I thought about deep linking with some callback data just like poll-bot does, but the thing is that I do not want the user to write anything on the group (yes, both the user and the bot are in the group).

I also thought about feeding a database with all the users from a group (where the bot is), and then ask the user which group he wants, but there is still no way to do that with telegram bot API.

Thanks!

2

Answers


  1. Every group chat as well as single chat has a chat_id. It won’t change as long as the chat exists. To learn the chat_id you can use a bot like this one. It will always answer with the chat id of the chat in which he received a message:

    from telegram.ext import Updater
    
    def echo(bot, update):
        bot.sendMessage(chat_id=update.message.chat_id, text="Your chat ID is " + str(update.message.chat_id))
    
    updater = Updater(token='BOT_TOKEN')
    dispatcher = updater.dispatcher
    dispatcher.add_handler(MessageHandler(Filters.text, echo))
    

    To do some X action you can use this chat_id. It won’t change, so you just can store them as a variable in your script.

    If you want to let the user choose in what group he wants to do some X action, you first need to know all groups the user is a member in. As far as I know there is no way to receive this information with telegram. It makes sense, otherwise the bot owner could know all the users groups. I doubt this is what any user wants.

    In my view the best way is to let the user specify the group ID.

    def some_X_action(bot, update, args):
        if (len(args) <= 0):
            bot.sendMessage(chat_id=update.message.chat_id, text="usage: /Xaction groupID")
            return
        bot.sendMessage(chat_id=args[0], text="sending message in group with ID " + args[0])
    
    dispatcher.add_handler(CommandHandler('Xaction', some_X_action, pass_args=True))
    

    Maybe you could allow to create an alias for a chat_id, like “news-chat” or something that is easy to remember.

    Login or Signup to reply.
  2. You could use deep link to ask the user to add your bot to the group; when the user chooses a group, you link that group’s chat_id to the user’s chat_id on yout database;
    Next time your user wants to perform a task, you just show all the linked groups.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search