Ads 468x60px

##EasyReadMore##

23 5月, 2018

第一章. 使用 Line bot 發布訊息

申請 Line bot 帳號:

參考下列網址: https://goo.gl/ML3Tyg

安裝 line-bot-sdk library:

 pip install line-bot-sdk
ubuntu 安裝 line-bot-sdk 出現錯誤
Cannot uninstall 'chardet'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall
sudo rm /usr/lib/python2.7/dist-packages/chardet-2.0.1.egg-info
sudo rm -r /usr/lib/python2.7/dist-packages/chardet/

取得 Channel access token 及 Your user ID :

到 line Messaging API 取得 Channel access token 及 Your user ID 讓 sample code 使用

image

image

line_bot.py sample code: 

from linebot import LineBotApi
from linebot.models import TextSendMessage
line_bot_api = LineBotApi('Channel access token (long-lived) ')
#push message to one user , 使用用 bot 的 user ID ,或你要推到群組就使用 gourp ID ,gourp ID 可用  print("Group ID:"+event.source.group_id) 取得
line_bot_api.push_message('Your user ID', 
     TextSendMessage(text='Hello World!'))
 #push message to multiple users
 line_bot_api.multicast(['user_id1', 'user_id2'], 
     TextSendMessage(text='Hello World!'))

執行 python line_bot.py 結果如下:

image

錯誤處理:

[Exception: PushMessage API ERROR: {"message":"Access to this API is not available for your account"}]

如果訊息還是發不出去,要檢查一下當初申請的 plan 是不是 Developer 而不是 free 帳號

image

pip - Cannot uninstall chardet - Stack Overflow - https://goo.gl/J7Qayf

0 意見:

張貼留言

 
Blogger Templates