Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python循环中的Json_Python_Json_Telegram_Telepot - Fatal编程技术网

python循环中的Json

python循环中的Json,python,json,telegram,telepot,Python,Json,Telegram,Telepot,我是一个完全的初学者,我有一个问题:我将使用僵尸程序创建一个电报机器人,我想通过python循环在JSON代码中插入我的列表元素。在本例中,我希望创建带有纽约、洛杉矶和华盛顿的按钮,{'text':I},但在电报中,最后一项(华盛顿)只显示一个按钮。我想创建3个按钮 import botogram import json bot = botogram.create("token") list = ['New York',"LA", "Washington DC"] @bot.command

我是一个完全的初学者,我有一个问题:我将使用僵尸程序创建一个电报机器人,我想通过python循环在JSON代码中插入我的列表元素。在本例中,我希望创建带有纽约、洛杉矶和华盛顿的按钮,
{'text':I}
,但在电报中,最后一项(华盛顿)只显示一个按钮。我想创建3个按钮

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()

您不是在
list
上循环创建三个按钮,而是在
list
上循环发送三条消息。创建按钮定义列表,然后将其添加到循环中,然后在循环外发送消息。

我从未使用过僵尸图,但在我看来,我建议您在循环中为创建一个变量(dictionary-dict),然后调用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)

我希望这对你有帮助,至少有一点

不要使用
list
作为变量名,它会覆盖
list()
函数。我该如何使用bot.api.call('sendMessage',dict)您的确切意思是什么?抱歉,但我有以下错误:来自电报的响应:“错误请求:无法解析应答键盘标记JSON对象”抱歉,但我有以下错误:来自电报的响应:“错误请求:无法解析回复键盘标记JSON对象”哦,你可以使用JSON.loads(dict)。也许这会有帮助!对不起,但我有一个错误:来自电报的响应:“错误请求:无法解析回复键盘标记JSON对象。”“@AbdulGandal那么你有了一个新问题,所以打开一个新问题,把你的新版本代码和新的错误消息一起放进去。