我有一个运行中的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很好地处理电报输出。我在其他命令中也有同样的问题。我该怎么做?我是否必须在函数中返回值,然后使用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
。