1

I'm a total beginner, I have a question: I'm going to create a telegram bot using botogram, I would like to insert my list's element in a JSON code by python loop. In this case I would like to create buttons with New York, LA, and Washington, {'text': i}, but on telegram appears just one button with the last item (Washington). I want to create 3 buttons.

import botogram
import json

bot = botogram.create("token")

list = ['New York',"LA", "Washington DC"]

@bot.command("start")
def start_command(chat, message):
    for i in list:
        bot.api.call('sendMessage', {
            'chat_id': chat.id,
            'text': 'Where are you?',
            'reply_markup': json.dumps({
            'keyboard': [
                [
                    {'text': i},
                    {'text': 'Action B'},
                ],
                [
                    {
                        'text': 'Use geolocation',
                        'request_location': True,
                    },
                ],
            ],
            'resize_keyboard': True,
            'one_time_keyboard': True,
            'selective': True,
        }),
    })

if __name__ == "__main__":
     bot.run()
Barmar
  • 741,623
  • 53
  • 500
  • 612
Abdul Gandal
  • 97
  • 1
  • 7

2 Answers2

1

You aren't looping over list to create three buttons, you're looping over list to send three messages. Create your button definition list and then add to it within your loop, then send the message outside of the loop.

Jim
  • 72,985
  • 14
  • 101
  • 108
  • Sorry, but i've this error: Response from Telegram: "Bad Request: can't parse reply keyboard markup JSON object" – Abdul Gandal Apr 14 '20 at 20:15
  • @AbdulGandal then you have a new problem, so open a new question and put your new version of your code into it along with the new error message. – Jim Apr 15 '20 at 04:51
0

I have never used botogram, but as I see it, I suggest you create a variable in the for loop (a dictionary - dict) and then call bot.api.call

@bot.command("start")
def start_command(chat, message):
    for i in list:
        dict = {
            'chat_id': chat.id,
            'text': 'Where are you?',
            'reply_markup': {
            'keyboard': [[
                {'text': i},
                {'text': 'Action B'},
            ],
            [
                {
                    'text': 'Use geolocation',
                    'request_location': True,
                },
            ],
        ],
        'resize_keyboard': True,
        'one_time_keyboard': True,
        'selective': True, 
        }
    }
    bot.api.call('sendMessage', dict)

I hope that helps you, at least a little bit!

georgekrax
  • 1,065
  • 1
  • 11
  • 22