簡體   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