owned this note
owned this note
Published
Linked with GitHub
# Line Bot Lesson 3 - 專題實作-旅遊購物外幣換算
:house: [回筆記首頁](https://www.mirai.tw)
###### tags: `python` `Line` `Bot` `api`
[toc]
試試看這個 line-bot吧! <a href="https://lin.ee/VbbJPCq"><img src="https://scdn.line-apps.com/n/line_add_friends/btn/zh-Hant.png" alt="加入好友" height="36" border="0"></a>

這是這個程式的原始碼
```
# 使用fastapi
LINE_CHANNEL_ACCESS_TOKEN='xxxxx'
LINE_CHANNEL_SECRET='xxx'
# Deta_Project_Token='xxx'
AUTHOR='林奇賢'
curd={"..":{"美金":"..USD","日圓":"..JPY","韓元":"..KRW","歐元":"..EUR","人民幣":"..CNY","更多":"..,"},
"..,":{"港幣":"..HKD","英鎊":"..GPB","澳幣":"..AUD","泰幣":"..THB","馬來幣":"..MYR","更多":"..,,"},
"..,,":{"菲比索":"..PHP","印尼幣":"..IDR","越南盾":"..VND","加幣":"..CAD","紐元":"..NZD","更多":"..,,,"},
"..,,,":{"瑞士法郎":"..CHF","南非幣":"..ZAR","瑞典幣":"..SEK","新加坡幣":"..SGD","更多":".."},
}
import re
import twder
from deta import Deta
from fastapi import FastAPI, Request
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import MessageEvent, TextMessage, TextSendMessage, QuickReply, QuickReplyButton, MessageAction
app= FastAPI(docs_url=None, redoc_url=None, openapi_url = None)
deta = Deta(Deta_Project_Token)
curr_users = deta.Base("currency_bot")
line_bot_api = LineBotApi(LINE_CHANNEL_ACCESS_TOKEN)
handler = WebhookHandler(LINE_CHANNEL_SECRET)
#profile = line_bot_api.get_profile('<user_id>')
# a POST route for our webhook events
@app.post("/")
async def webhook_handler(request: Request):
# verify signature if needed
# add logic to handle the request
signature = request.headers['X-Line-Signature']
body = await request.body()
try:
handler.handle(body.decode(),signature)
except InvalidSignatureError:
raise HTTPException(status_code=400, detail="SignatureError")
return "ok"
@handler.add(MessageEvent, message=TextMessage)
def handling_message(event):
mtext=event.message.text
user_id=event.source.user_id #get userID in Line
currency_dict=curr_users.get(user_id) # get data-line userID in deta.base
if currency_dict is None: # if there is no data in deta.base
currency_dict = curr_users.put("JPY",user_id)
currency=currency_dict['value']
try:
regex = r'[^0-9.+-/()*]'
prog = re.compile(regex)
if not prog.search(mtext):
money=eval(mtext)
else:
money=-1
curr=float(twder.now(currency)[2] if twder.now(currency)[4]=='-' else twder.now(currency)[4])
#2:現鈔賣出 4:即期賣出 ,若無即期報價則回傳現鈔報價
except:
curr=0
money=-1
if mtext.upper()=="AUTHOR":
message=TextSendMessage("作者:"+AUTHOR)
elif mtext in curd: # mtext='..' or '...' or '....' and so on
items=[]
for item in curd[mtext]:
items.append(QuickReplyButton(action=MessageAction(item,curd[mtext][item])))
try:
message=TextSendMessage(
'選擇要查詢的外幣',
quick_reply=QuickReply(items)
)
except:
message=TextSendMessage("Error at QuickReply")
elif (mtext[:2]=='..')and(len(mtext)>2):
changecurrency=mtext[2:5].upper()
if changecurrency in twder.currencies():
curr_users.put(changecurrency,user_id)
message=TextSendMessage(f'設定成{twder.currency_name_dict()[changecurrency]}換台幣\n請直接輸入數值或運算式')
elif (money>=0):
message=TextSendMessage(f'''{money} {twder.currency_name_dict()[currency]}\n台幣約為 {str(round(money*curr))}元\n輸入 .. 變換幣別''')
else:
message=TextSendMessage(f'現在查詢的是{twder.currency_name_dict()[currency]}\n輸入..變換幣別\n或是輸入數值或運算式\n例如:\n1500\n50+45*0.7')
line_bot_api.reply_message(event.reply_token,message)
```