简体   繁体   中英

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)

I want to pass the checkTemp() function into the Twilio body and schedule.

my phone receive the SMS from Twilio, but its display

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

which is not what I expect

The issue here is that you are passing the function object to the method that creates the SMS message. But you want to pass the result of calling the function. All you've missed for that are the brackets.

Your code says:

body=checkTemp

but it should be:

body=checkTemp()

Your checkTemp function will need to return a string so that it can be sent as a message:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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