Created
February 22, 2020 03:29
-
-
Save e96031413/04e9d741f177945b14d564ac9e0d0c13 to your computer and use it in GitHub Desktop.
紀錄linebot官方sdk的一些使用方式
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from linebot.models import ( | |
MessageEvent, TextMessage, TestSendMessage, StickerMessage, StickerSendMessage, ConfirmTemplate, TemplateSendMessage, MessageAction, URIAction, LocationMessage | |
) | |
### TextSendMessage | |
### StickerSendMessage:只有官方預設的四組貼圖包可以使用 | |
def handle_message(event): | |
txt=event.message.text #取得使用者輸入的文字 | |
reply_txt = TextSendMessage(text=txt) | |
reply_stk = StickerSendMessage(package_id=3,sticker_id=233) #貼圖包編號3、貼圖編號233(貼圖列表查詢網址:devdocs.line.me/files/sticker_list.pdf) | |
line_bot_api.reply_message(yourToken, [reply_txt, reply_stk]) #回復文字與指定貼圖 | |
### ConfirmTemplate:兩個按鈕 | |
queries = ConfirmTemplate( | |
text=f"{me['name']}您好,請問要回報查修地點嗎?", | |
actions=[ | |
URIAction( | |
label = '回報地點', | |
uri='line://nv/location' | |
), | |
MessageAction(label='不需要', text='不需要') | |
]) | |
temp_msg=TemplateSendMessage(alt_text='確認訊息',template=queries) | |
line_bot_api.reply_message(token, temp_msg) | |
### ButtonsTemplate:按鈕樣板 | |
queries = ButtonsTemplate( | |
text=f"{me['name']}您好,請問要回報查修地點嗎?", | |
actions=[ | |
URIAction( | |
label = '回報地點', | |
uri='line://nv/location' | |
), | |
MessageAction(label='不需要', text='不需要') | |
URIAction( | |
label='前往swf.com.tw網站', | |
url='https://google.com' #前往指定網頁 | |
) | |
]) | |
### ImageSendMessage | |
### LocationSendMessage | |
@handler.add(MessageEvent, message=LocationMessage) | |
def handle_location_message(event): | |
addr = event.message.address #地址 | |
lat = str(event.message.latitude) #緯度 | |
lon = str(event.message.longitude) #緯度 | |
line_bot_api.reply_message(yourToken, [reply_txt, reply_stk]) | |
if addr is None: | |
msg=f'收到GPS座標:({lat},{lon})\n謝謝!' | |
else: | |
msg=f'收到GPS座標:({lat},{lon})\n地址:{addr}!' | |
### 一些常用API函式 | |
_id = event.source.user_id #取得使用者的唯一識別碼 | |
line_bot_api.get_profile(_id) #取得個人檔案 | |
profile.picture_url #大頭貼網址 | |
profile.status_message #狀態消息 | |
profile.display_name #記錄使用者名稱 | |
line://nv/location #打開地點畫面,讓使用者點選地圖分享位置 | |
line://nv/camera #打開相機 | |
line://nv/cameraRoll/single #打開相簿,讓使用者選取一張照片 | |
line://nv/cameraRoll/multi #打開相簿,讓使用者選取多張照片 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment