Python Telegram bot return 变量列表
Python Telegram bot return list of variables
我有一个 Python 电报机器人和 运行。现在我已经实现了一个新命令,它从 API 中检索值并通过 bot.send.message 发送它们。这里有上述命令的代码片段,
def my_command(bot, update):
request = requests.get("https://my_site.com/api").json()
value1 = request[0]
value2 = request[1]
value3 = request[2]
array_of_values = (value1, value2, value3)
bot.send_message(chat_id=update.message.chat_id, text = array_of_values)
这会在电报中打印以下内容
["value1", "value2", "value3"]
但是我想要的是下面的
值 1
值 2
值 3
我显然无法使用 bot.send_message 很好地管理电报输出。我在其他命令中也有同样的问题。我该怎么做?我必须 return 函数中的值然后使用 bot.send_message 还是格式漂亮的问题?
谢谢!
埃里克
它打印 ["value1", "value2", "value3"] 因为 bot.send_message(text="")
接受字符串,而不是数组,所以它通常只打印你在那里写的内容.
您可以通过两种方式完成,
手动点赞下面的代码。
bot.send_message(chat_id=update.message.chat_id, text = value1 + "\n" + value2 + "\n" + value3)
或
array_of_values = (value1, value2, value3)
bot.send_message(chat_id=update.message.chat_id, text = "\n".join(array_of_values))
请注意,"\n"
是为换行添加的。如果换行符对您不起作用,您可以将其替换为 %0A
。
我有一个 Python 电报机器人和 运行。现在我已经实现了一个新命令,它从 API 中检索值并通过 bot.send.message 发送它们。这里有上述命令的代码片段,
def my_command(bot, update):
request = requests.get("https://my_site.com/api").json()
value1 = request[0]
value2 = request[1]
value3 = request[2]
array_of_values = (value1, value2, value3)
bot.send_message(chat_id=update.message.chat_id, text = array_of_values)
这会在电报中打印以下内容
["value1", "value2", "value3"]
但是我想要的是下面的
值 1
值 2
值 3
我显然无法使用 bot.send_message 很好地管理电报输出。我在其他命令中也有同样的问题。我该怎么做?我必须 return 函数中的值然后使用 bot.send_message 还是格式漂亮的问题?
谢谢!
埃里克
它打印 ["value1", "value2", "value3"] 因为 bot.send_message(text="")
接受字符串,而不是数组,所以它通常只打印你在那里写的内容.
您可以通过两种方式完成,
手动点赞下面的代码。
bot.send_message(chat_id=update.message.chat_id, text = value1 + "\n" + value2 + "\n" + value3)
或
array_of_values = (value1, value2, value3)
bot.send_message(chat_id=update.message.chat_id, text = "\n".join(array_of_values))
请注意,"\n"
是为换行添加的。如果换行符对您不起作用,您可以将其替换为 %0A
。