line bot - 如何开始
line bot - how to get started
我刚刚开始使用 line-bot
并按照此处的教程进行操作:https://developers.line.biz/en/docs/messaging-api/building-bot/
但是,我仍然不明白如何连接我的 line app account
,发送消息,并让这些消息返回 python。
下面是我从line
教程中复制的脚本。
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('foo', timeout=20)
handler = WebhookHandler('bar')
user_profile = 'far'
@app.route("/", methods=['GET'])
def home():
profile = line_bot_api.get_profile(user_profile)
print(profile.display_name)
print(profile.user_id)
print(profile.picture_url)
print(profile.status_message)
return '<div><h1>ok</h1></div>'
@app.route("/callback", 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)
app.logger.info("Request body: " + body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text='hello world'))
if __name__ == "__main__":
app.run(debug=True)
我错过了什么,或者如何连接line app收发消息?
我按照该教程成功创建了一个仅以大写形式回显消息的机器人:
您的问题是如何 "connect" 您的机器人代码与 LINE 应用程序。本教程最重要的三个部分可能是:
- 将机器人添加为好友,您可以通过使用 LINE 应用程序扫描其二维码来完成此操作
- 当您为您的机器人创建频道时,您需要启用 "Webhooks" 并提供一个 https 端点,LINE 将在该端点发送您的机器人收到的交互事件。为简化此答案,我创建了一个 AWS Lambda 函数并通过 API 网关将其公开为端点,如下所示:
https://asdfasdf.execute-api.us-east-1.amazonaws.com/default/MyLineBotFunction
。这是我为我的机器人输入的 Webhook URL。
- 一旦您成功接收到消息事件,只需使用消息随附的唯一
replyToken
发布到 LINE API 即可进行响应。
这是我的简单 yell-back-in-caps 机器人的 Lambda 函数代码:
import json
from botocore.vendored import requests
def lambda_handler(event, context):
if 'body' in event:
message_event = json.loads(event['body'])['events'][0]
reply_token = message_event['replyToken']
message_text = message_event['message']['text']
requests.post('https://api.line.me/v2/bot/message/reply',
data=json.dumps({
'replyToken': reply_token,
'messages': [{'type': 'text', 'text': message_text.upper()}]
}),
headers={
# TODO: Put your channel access token in the Authorization header
'Authorization': 'Bearer YOUR_CHANNEL_ACCESS_TOKEN_HERE',
'Content-Type': 'application/json'
}
)
return {
'statusCode': 200
}
我刚刚开始使用 line-bot
并按照此处的教程进行操作:https://developers.line.biz/en/docs/messaging-api/building-bot/
但是,我仍然不明白如何连接我的 line app account
,发送消息,并让这些消息返回 python。
下面是我从line
教程中复制的脚本。
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('foo', timeout=20)
handler = WebhookHandler('bar')
user_profile = 'far'
@app.route("/", methods=['GET'])
def home():
profile = line_bot_api.get_profile(user_profile)
print(profile.display_name)
print(profile.user_id)
print(profile.picture_url)
print(profile.status_message)
return '<div><h1>ok</h1></div>'
@app.route("/callback", 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)
app.logger.info("Request body: " + body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text='hello world'))
if __name__ == "__main__":
app.run(debug=True)
我错过了什么,或者如何连接line app收发消息?
我按照该教程成功创建了一个仅以大写形式回显消息的机器人:
您的问题是如何 "connect" 您的机器人代码与 LINE 应用程序。本教程最重要的三个部分可能是:
- 将机器人添加为好友,您可以通过使用 LINE 应用程序扫描其二维码来完成此操作
- 当您为您的机器人创建频道时,您需要启用 "Webhooks" 并提供一个 https 端点,LINE 将在该端点发送您的机器人收到的交互事件。为简化此答案,我创建了一个 AWS Lambda 函数并通过 API 网关将其公开为端点,如下所示:
https://asdfasdf.execute-api.us-east-1.amazonaws.com/default/MyLineBotFunction
。这是我为我的机器人输入的 Webhook URL。 - 一旦您成功接收到消息事件,只需使用消息随附的唯一
replyToken
发布到 LINE API 即可进行响应。
这是我的简单 yell-back-in-caps 机器人的 Lambda 函数代码:
import json
from botocore.vendored import requests
def lambda_handler(event, context):
if 'body' in event:
message_event = json.loads(event['body'])['events'][0]
reply_token = message_event['replyToken']
message_text = message_event['message']['text']
requests.post('https://api.line.me/v2/bot/message/reply',
data=json.dumps({
'replyToken': reply_token,
'messages': [{'type': 'text', 'text': message_text.upper()}]
}),
headers={
# TODO: Put your channel access token in the Authorization header
'Authorization': 'Bearer YOUR_CHANNEL_ACCESS_TOKEN_HERE',
'Content-Type': 'application/json'
}
)
return {
'statusCode': 200
}