Line Bot 教學
Line Bot 是什麼?
LINE Bot Designer是什麼?
LINE Bot Designer使您無需任何程式基礎,更輕鬆地製作LINE機器人原型。您可以在所需的場景中自由設計聊天機器人。
簡單來說就是可以在Line聊天視窗自動回覆客人
或是設計一些實用功能讓使用者透過Line就可以使用
建立Line Bot
1. 創建一個聊天頻道
2. 輸入發行者名稱
3. 點擊 Create a Messaging API channel
4. 把剩下的頻道基本資料填一填
5. 填寫完成點擊創建
6. 點擊 Messaging API
7. 點擊 Auto-reply messages
8. 把 “加入好友歡迎訊息” 與 “自動回應訊息” 停用
Line Bot Python 程式範例
1. 創建一個 config.ini
[line-bot]
channel_access_token = 你的token
channel_secret = 你的金鑰
2. 取得Channel access token
Channel access token 在哪?
點擊 Messaging API
下面有Channel access token
點選issue後 會顯示你的token
3. 取得Channel secret
Channel secret 在哪?
點擊 Basic settings
下面有 Channel secret
4. 建立app.py
建立一個python檔案
這邊以app.py為例
from __future__ import unicode_literals
import os
from flask import Flask, request, abort
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import MessageEvent, TextMessage, TextSendMessage
import configparser
import random
app = Flask(__name__)
# LINE 聊天機器人的基本資料
config = configparser.ConfigParser()
config.read('config.ini')
line_bot_api = LineBotApi(config.get('line-bot', 'channel_access_token'))
handler = WebhookHandler(config.get('line-bot', 'channel_secret'))
# 接收 LINE 的資訊
@app.route("/callback", methods=['POST'])
def callback():
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
try:
print(body, signature)
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def prettyEcho(event):
sendString = ""
if "擲筊" in event.message.text:
sendString = divinationBlocks()
elif "抽簽" in event.message.text or "抽" in event.message.text:
sendString = drawStraws()
else:
sendString = event.message.text
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=sendString)
)
def divinationBlocks():
divinationBlocksList = ["笑杯", "正杯", "正杯", "笑杯"]
return divinationBlocksList[random.randint(0, len(divinationBlocksList) - 1)]
def drawStraws():
drawStrawsList = ["大吉", "中吉", "小吉", "吉", "凶", "小凶", "中凶", "大凶"]
return drawStrawsList[random.randint(0, len(drawStrawsList) - 1)]
if __name__ == "__main__":
app.run()
如果有缺少一些SDK
可以參考這裡
$ pip3 install django
$ pip3 install line-bot-sdk
$ pip3 install beautifulsoup4
$ pip3 install requests
開啟終端機/CMD
輸入 python3 app.py
Ngrok怎麼設置
Ngrok
就像是 No-IP
與其他轉址軟體一樣
可以本地端地址轉至他生成的一個網址裡
這樣你在本地端也可以建立伺服器
不用固定ip 不用自己網域
懶人架設網域
1. Ngrok安裝步驟
2. 解壓縮Ngrok.zip
unzip ngrok.zip
3. 設置Ngrok token
上面再下載時官網已經有跟你說token
了
ngrok authtoken 你的token
4. 設置Ngrok port
5000是port
自己可以更換
但前面app.py
架設伺服器是使用 5000 port
故設置 5000
ngrok http 5000
Forwarding 有給一組網域名稱
https://bd318e3935d9.ngrok.io
這就是ngrok
生成的
5. 更換Webhook URL
點擊 Edit
更換URL
記得後面要換我的 /callback
才呼叫得到func
好的以後點擊 update
點擊 Verify
測試伺服器是否開啟
失敗請回前幾個步驟重新建置
出現 error 200
也是
需要多建置幾次方可成功
Line 測試
6. 開啟加入群組權限
Messaging API -> Allow bot to join group chats
開啟加入群組或多人聊天室
要玩的可以自行加機器人好友
參考
[Python+LINE Bot教學]6步驟快速上手LINE Bot機器人
[Python Linebot]教你如何使用Python成功串接Linebot(2020版)
line-bot-sdk SDK範例
[教學] Line Bot 機器人不求人 – 範例 (Python)
Python – Selenium 繞過”這個瀏覽器或應用程式可能有安全疑慮”
python 各種均線 10MA/20MA均線 | pandas安裝
python sleep 暫停幾秒執行 – 2021年
PyAutoGUI Python控制電腦 | 按鍵精靈 | 鍵盤控制
python binance币安利用ccxt完成虛擬貨幣自動化交易
python write dictionary to text file | dict 寫入讀取
rtpkantorbola
Kantorbola adalah situs slot gacor terbaik di indonesia , kunjungi situs RTP kantor bola untuk mendapatkan informasi akurat slot dengan rtp diatas 95% . Kunjungi juga link alternatif kami di kantorbola77 dan kantorbola99
Jago Slot
Jagoslot adalah situs slot gacor terlengkap, terbesar & terpercaya yang menjadi situs slot online paling gacor di indonesia. Jago slot menyediakan semua permaina slot gacor dan judi online mudah menang seperti slot online, live casino, judi bola, togel online, tembak ikan, sabung ayam, arcade dll.
What’s up everyone, it’s my first pay a quick visit at this site, and paragraph is in fact fruitful in favor of me,
keep up posting these types of articles.
Normally I don’t read post on blogs, but I would like
to say that this write-up very compelled me to try and do it!
Your writing taste has been amazed me. Thank you, very great article.
It was a gift I liked. These ended up my Christmas gift selections… At
the commencing of the e-book he identifies a set of “people who do not read through introductions” amongst whom I would have included myself,
but I fortunately basked in him crafting about the publications contained
listed here, and I immediately subscribed to practically all of them, most of
which ended up fully unfamiliar to me. Here, protesters march at
a rally to show their distaste of the monthly bill.
Pretty compulsive examining, and should really be obligatory actually
to understand the mess that 1 ignorant, vain, narcissistic, legal can impose on America inside of times
of commencing using workplace. Images at first intended to be shared amongst partners can now be unfold about the earth.
You can give it away for free of charge, but you can’t provide it.
Daisy chain: Group of contributors execute cunnilingus
or fellatio on just about every other in a round development, permitting each and every participant to equally give and obtain oral
sexual intercourse concurrently.
Look at my web-site; Webcams girls nude
Thank you for any other great post. The place else could anybody get that kind
of information in such an ideal means of writing?
I’ve a presentation next week, and I’m at the search for such info.
Also visit my web blog: annual rates
留言區 0.0