[LINE Messaging API] Create parrot return BOT with Python

tutorial

image.png

[When receiving a message]

Set WebhookHandler as below @handler.add(MessageEvent, message=TextMessage)

lineapi.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_CHANNEL_ACCESS_TOKEN')
handler = WebhookHandler('YOUR_CHANNEL_SECRET')


@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:
        print("Invalid signature. Please check your channel access token/channel secret.")
        abort(400)

    return 'OK'


@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
    line_bot_api.reply_message(
        event.reply_token,
        TextSendMessage(text=event.message.text))


if __name__ == "__main__":
    app.run()

Quoted from ** Synopsis / Usage ** in ** LINE Messaging API SDK for Python **

[Reply message]

Call the reply message API (line_bot_api. ** reply_message **) By setting the argument to the following, the parrot is returned. ・ Event.reply_token ・ ** TextSendMessage ** (event.message.text)


event
handle_message method argument (** event **)

event


{
  "message": {
    "id": "10951288714213",
    "text": "Hello",
    "type": "text"
  },
  "replyToken": "73fb2d4ab910457443a96c3483f478dc",
  "source": {
    "type": "user",
    "userId": "U0d47ada1d7ca738641228d4599c9d347"
  },
  "timestamp": 1574264703712,
  "type": "message"
}

Quote

LINE Messaging API SDK for Python

Recommended Posts

[LINE Messaging API] Create parrot return BOT with Python
[Python] [LINE Bot] Create a parrot return LINE Bot
[LINE Messaging API] Create a BOT that connects with someone with Python
LINE BOT (Messaging API) development with API Gateway and Lambda (Python) [Part 2]
Create a LINE BOT with Minette for Python
LINE BOT with Python + AWS Lambda + API Gateway
Parrot return LINE BOT creation
[LINE Messaging API] Create a rich menu in Python
Create Awaitable with Python / C API
I made LINE-bot with Python + Flask + ngrok + LINE Messaging API
Made "Unofficial Apple Refurbished Product Introduction" BOT with LINE Messaging API (v2) + API Gateway + lambda (python)
How to make an artificial intelligence LINE bot with Flask + LINE Messaging API
Steps to create a Twitter bot with python
Make a parrot return LINE Bot on AWS Cloud9
[Python] Using Line API [1st Creation of Beauty Bot]
I made a LINE BOT with Python and Heroku
[Super easy] Let's make a LINE BOT with Python.
How to operate Discord API with Python (bot registration)
Various memorandums when using sdk of LINE Messaging API with Python (2.7.9) + Google App Engine
Use Trello API with python
Create an API with Django
Use Twitter API with Python
Create 3d gif with python3
Web API with Python + Falcon
[Python] A story about making a LINE Bot with a practical manned function on its own without using Salesforce [Messaging API]
Call the API with python3.
Use subsonic API with python3
Create a Mastodon bot with a function to automatically reply with Python
Until I return something with a line bot in Django!
Create a Twitter BOT with the GoogleAppEngine SDK for Python
[LINE Messaging API] Issue channel access token v2.1 in Python
Create API with Python, lambda, API Gateway quickly using AWS SAM
I made a Chatbot using LINE Messaging API and Python
A script that makes it easy to create rich menus with the LINE Messaging API
Easy-to-understand demo of Imagemap Message of LINE Messaging API [PHP] [Ruby] [Python]
Create another version of Python conda environment with one command line
[Python] Make your own LINE bot
I want to know the weather with LINE bot feat.Heroku + Python
[AWS] Create API with API Gateway + Lambda
Checkpoint when you get hooked on LINE parrot return bot on Heroku
Create folders from '01' to '12' with python
Create an automatic grade management app for Tenhou private room with LINE bot and Python Part 1
Print with python3 without line breaks
Quine Post with Qiita API (Python)
Create a virtual environment with Python!
Create an Excel file with Python3
Play with GCP free frame ① ~ Cloud Run, Datastore & LINE Messaging API ~
Create an automatic grade management app for Tenhou private room with LINE bot and Python Part 2
Create Gmail in Python without API
[python] Read information with Redmine API
Create an automatic grade management app for Tenhou private room with LINE bot and Python Part ③
Create API using hug with mod_wsgi
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
[Python] Create API to send Gmail
Create a LINE Bot in Django
Create a Twitter BOT service with GAE / P + Tweepy + RIOT API! (Part 1)
Create a Twitter BOT service with GAE / P + Tweepy + RIOT API! (Part 2)
Python beginners decided to make a LINE bot with Flask (Flask rough commentary)
Challenge to create time axis list report with Toggl API and Python
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
Collecting information from Twitter with Python (Twitter API)