如何将函数传递到 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() 函数传递到 Twilio 主体和计划中。

我的 phone 收到来自 Twilio 的短信,但它的显示

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

这不是我所期望的

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

您的代码表示:

body=checkTemp

但应该是:

body=checkTemp()

您的 checkTemp 函数需要 return 一个字符串,以便它可以作为消息发送:

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)