skip to Main Content

im programming a new telegram bot. I’ve created a loop which inserts the variable i in the dictionari dic that describes a new button. But when i convert dic in Json dictionary, it gives me this problem.

import botogram
import json

bot = botogram.create("1210935912:AAG4X8vHlXLM3jQWnxFKDB2NsZ6pqTQM7lQ")
list = ["La","Alaska","New Delhi"]
bot.about = "Benvenuti"
@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', json.loads(dict))

if __name__ == "__main__":
     bot.run()

Output:

TypeError: the JSON object must be str, bytes or bytearray, not dict

2

Answers


  1. json is a text representation of a data structure.

    json.loads converts a json string into a dictionary. Since you already have a dictionary, and want a json string, you should instead use the json.dumpsmethod.

    Login or Signup to reply.
  2. You need to use json.dumps to convert object to json

    bot.api.call('sendMessage', json.dumps(dict))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search