iT邦幫忙

0

LINE BOT handler 問題,無法呼叫函式?{己解決}

@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
    input_text = event.message.text
    if input_text == "查匯率":
        resp = requests.get('https://tw.rter.info/capi.php')
        currency_data = resp.json()
        usd_to_twd = currency_data['USDTWD']['Exrate']
        line_bot_api.reply_message(
            event.reply_token,
            TextSendMessage(text=f'美元 USD 對台幣 TWD:1:{usd_to_twd}'))
    elif input_text == '現在時間':
        now = datetime.now()
        current_time = now.strftime("%H:%M:%S")
        line_bot_api.reply_message(
            event.reply_token, TextSendMessage(text=f"台北時間:{current_time}"))
    elif input_text == "現在幾度" or "溫度":
        Rdata = requests.get(
            'https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0003-001?Authorization=ABC-#@12345324-KEY-1233-1237485723&locationName=%E8%87%BA%E5%8C%97,&elementName=TEMP&parameterName=CITY').json()
        Temp = Rdata["records"]["location"][0]["weatherElement"]
        Mdict = dict(enumerate(Temp))
        o1 = Mdict[0]['elementValue']
        line_bot_api.reply_message(
            event.reply_token,
            TextSendMessage(text=f'現在「台北」的目前溫度是:{o1}'))
    elif input_text == "股票":

        stock(x)
        line_bot_api.reply_message(
            event.reply_token,
            TextSendMessage(text=f'現在「股票號碼{stock} 」的目前股價是:{getstock}'))

試過修改許多次if elif的功能,一直無法正常運作,在使用者貼上訊息的時候一直會回應溫度>"<,除非使用特定文字才可以實作回報時間和匯率。另外股票的功能也沒辦法啟動
試著用函式也不能呼叫?

or "溫度" -> 永遠回傳 true

修改為
elif input_text == "現在幾度" or input_text == "溫度":
KYCPG iT邦新手 4 級 ‧ 2020-07-21 23:06:29 檢舉
@小碼農米爾Mir 謝謝大大 問題解決了 :D
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答