SlackAPI-无法返回带有多个变量的消息

时间:2019-09-23 12:08:36

标签: python slack-api openweathermap

我的Slack机器人具有以下代码:

    if command.startswith(COMMAND_FIVE):
        owm = pyowm.OWM()

        owm.set_API_key('')
        observation = owm.weather_at_id(4744326)
        w = observation.get_weather()
        jsondata= w.get_temperature('fahrenheit')
        RESPOND = ("The current temperature (Fahrenheit) is:",(jsondata["temp"]),"\nThe high is:",(jsondata["temp_max"]),"\nThe low is:",(jsondata["temp_min"]),"\n*Note: This can change*")
        response = (RESPOND)
        print("Someone got the weather.")

    # Sends the response back to the channel
    slack_client.api_call(
        "chat.postMessage",
        channel=channel,
        text=response or default_response
    )

当我调用此命令时,我得到以下信息:

What the slack bot returns. 我如何格式化消息,使其返回所有内容,而不仅仅是底线?

1 个答案:

答案 0 :(得分:1)

您的代码未产生预期结果的原因是,您正在将文本作为字符串的元组传递给API方法。显然,API方法在内部将其减少为一个元素(最后一个)。

要完成这项工作,您需要将文本作为一个完整的字符串传递给API方法的text属性。

这是您的代码的新版本,应该可以更好地工作:

RESPOND = [
    "The current temperature (Fahrenheit) is: {0:.2f}".format(jsondata["temp"]),
    "The high is: {0:.2f}".format(jsondata["temp_max"]),
    "The low is: {0:.2f}".format(jsondata["temp_min"]),
    "*Note: This can change*"]
response = "\n".join(RESPOND)