初步的先做一個可以收發訊息的bot
使用的python檔案為app.py
( 程式碼參考: https://github.com/LukeHong/line_bot_echo )
有稍微修改了一下
1.打開app.py,完整的程式碼如下
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
)
app = Flask(__name__)
line_bot_api = LineBotApi('YOUR_LineBot_Channel access token')
handler = WebhookHandler('YOUR_LineBot_Channel secret')
@app.route("/", methods=['GET'])
def hello():
return "Hello World!"
@app.route("/", methods=['POST'])
def callback():
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']
# get request body as text
body = request.get_data(as_text=True)
print("Request body: " + body, "Signature: " + signature)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
msg = event.message.text
print(msg)
msg = msg.encode('utf-8')
line_bot_api.reply_message(event.reply_token,TextSendMessage(text=event.message.text))
if __name__ == "__main__":
app.run(debug=True,port=80)
以下兩步驟為說明
2.說明一下收發訊息的部分
這是收訊息的部分
收到的訊息也可以在cmd中很清楚的看到整個訊息事件
3.而發出的訊息,目前是收到什麼內容就回覆什麼
4.測試一下