【发布时间】:2020-07-28 10:47:57
【问题描述】:
我正在编写一个新的电报机器人。我创建了一个循环,将变量i 插入到描述新按钮的字典dic 中。但是当我在 Json 字典中转换 dic 时,它给了我这个问题。
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()
输出:
TypeError: the JSON object must be str, bytes or bytearray, not dict
【问题讨论】:
-
只需使用
json.dumps(dict)。
标签: python json loops dictionary telegram