0

My telegram bot answers in personal chats but don't in group chats. I could not find the problem. A snippet from my code:

@app.route('/{}'.format(TOKEN), methods=['POST'])
def respond():
    # retrieve the message in JSON and then transform it to Telegram object
    update = telegram.Update.de_json(request.get_json(force=True), bot)

    # get the chat_id to be able to respond to the same user
    chat_id = update.message.chat.id

    # Telegram understands UTF-8, so encode text for unicode compatibility
    text = update.message.text

    if text == '/start':
        response = send_welcome()
        bot.sendMessage(chat_id=chat_id, text=response)
    elif 'discord' in text.split():
        response = send_discord()
        bot.sendMessage(chat_id=chat_id, text=response)
    elif 'edeka' in text.split():
        response = send_edeka()
        bot.sendMessage(chat_id=chat_id, text=response)
    elif 'random' in text.split():
        response = send_random()
        bot.sendMessage(chat_id=chat_id, text=response)             
    elif 'metro' in text.split():
        response = send_metro()
        bot.sendMessage(chat_id=chat_id, text=response)
    elif text == '/help':
        response = send_help()
        bot.sendMessage(chat_id=chat_id, text=response) 
    elif text == 'Good bot':
        response = 'Önemli degil kanka'    
        bot.sendMessage(chat_id=chat_id, text=response)    

    return 'ok'  

I get an error message in heroku for text.split(), but only from group chats.

1 Answers1

1
# get the chat_id to be able to respond to the same user
chat_id = update.message.chat.id

This is from the person sending the message. Therefore, if a person targets your bot in a group chat, you'll reply to his personal id.

Consider using

chat_id = update.message.from.id

This is the chat the message was send in, in this case it could be the group chat you want to reply to.

To get a better understanding about the different id's take a look at for example, this answer

0stone0
  • 34,288
  • 4
  • 39
  • 64