繁体   English   中英

如何将 function 传递到 Twilio 的正文和时间表中

[英]How to pass a function into the Twilio's body and schedule

def checkTemp():
    temp = res["main"]["temp"]
    condition = res['weather'][0]['main']
    while 5 < temp < 10:
        print("today = ", condition, ",temperature is", temp, "cold please wear more")
    else:
        while -10 < temp < 5:
            print("today = ", condition, ",temperature is", temp, "very cold")
            break
        else:
            while 15 < temp < 20:
                print("today = ", condition, ",temperature is", temp, "good weather")
                break


checkTemp()


def send_message():
    client = Client(keys.account_sid, keys.auth_token)

    message = client.messages.create(
        body=checkTemp,
        from_=keys.twilio_number,
        to=keys.target_number)

    print(message.body)

schedule.every().day.at("22:12").do(send_message(), checkTemp())

while True:

    schedule.run_pending()
    time.sleep(2)

我想将 checkTemp() function 传递到 Twilio 正文和时间表中。

我的手机收到Twilio的短信,但是显示

sent from your Twilio trial account - <funciton checkTemp at 0x1b532340>

这不是我所期望的

这里的问题是您将 function object 传递给创建 SMS 消息的方法。 但是您想传递调用 function 的结果。您错过的只是括号。

你的代码说:

body=checkTemp

但它应该是:

body=checkTemp()

您的checkTemp function 将需要返回一个字符串,以便它可以作为消息发送:

def checkTemp():
    temp = res["main"]["temp"]
    condition = res['weather'][0]['main']
    if 5 < temp and temp < 10:
        return "today = {condition}, temperature is {temp}, cold please wear more".format(condition = condition, temp =temp)
    elsif -10 < temp and temp < 5:
        return "today = {condition}, temperature is {temp}, very cold".format(condition = condition, temp =temp)
    elif 15 < temp and temp < 20:
        return "today = {condition}, temperature is {temp}, good weather".format(condition = condition, temp =temp)
    else:
        return "today = {condition}, temperature is {temp}".format(condition = condition, temp =temp)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM