Make a LINE BOT (chat)

Make a chat LINE BOT using A3RT

Improve the previously created Make LINE BOT and use the A3RT Talk API provided by Recruit to create a BOT for chatting. Had made.

(1) Get API KEY from A3RT

Issue API KEY on A3RT site.

(2) Check the structure of A3RT

Check the structure of A3RT before modifying main.py created last time. Execute the following code to check the structure of A3RT and check if the AI returns the answer to "Good morning" properly. First, install pya3rt in a virtual environment,

pip install pya3rt

Try running the following in python mode in your terminal.

import pya3rt

apikey = "*******************************"
client = pya3rt.TalkClient(apikey)
reply_message = client.talk("Good morning")

print(reply_message)
{'status': 0,'message': 'ok','results': [{'perplexity': 0.07743213382788067, 'reply': 'Good morning'}]}

Will be. I want to get "Good morning" in the reply, so I modified it as follows

import pya3rt

apikey = "****************************"
client = pya3rt.TalkClient(apikey)
reply_message = client.talk("Good morning")

print(reply_message['results'][0]['reply'])

Then

Good morning

Then, he properly returned "Good morning" → "Good morning". Incorporate this into the previously created main.py.

(3) Modify the previously created main.py

First, the above description of pya3rt is made into a function as follows.

def talk_ai(word):
    apikey = "****************************"
    client = pya3rt.TalkClient(apikey)
    reply_message = client.talk(word)
    return reply_message['results'][0]['reply']

Last time Modify the created main.py.

.py:main.py


from flask import Flask, request, abort
from linebot import (
    LineBotApi, WebhookHandler
)
from linebot.exceptions import (
    InvalidSignatureError
)
from linebot.models import (
    MessageEvent, TextMessage, TextSendMessage,
)
import os
#add to
import pya3rt

app = Flask(__name__)

YOUR_CHANNEL_ACCESS_TOKEN = os.environ["YOUR_CHANNEL_ACCESS_TOKEN"]
YOUR_CHANNEL_SECRET = os.environ["YOUR_CHANNEL_SECRET"]

line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN)
handler = WebhookHandler(YOUR_CHANNEL_SECRET)

@app.route("/")
def hello_world():
    return "hello world!"


@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:
        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):
    #(Addition) talk_Pass the argument to the ai method and get the return value ai_Assign to message
    ai_message = talk_ai(event.message.text)
    line_bot_api.reply_message(
        event.reply_token,
        #TextSendMessage(text=event.message.txt))
        #(Fixed) ai_Return message
        TextSendMessage(text=ai_message))

#(Addition) Reply ai conversation with pya3rt
def talk_ai(word):
    apikey = "****************************"
    client = pya3rt.TalkClient(apikey)
    reply_message = client.talk(word)
    return reply_message['results'][0]['reply']

if __name__ == "__main__":
    port = int(os.getenv("PORT", 5000))
    app.run(host="0.0.0.0", port=port)

If you can fix it, update requirements.txt and

pip freeze > requirements.txt

After that, deploy it to Heroku and you're done.

Recommended Posts

Make a LINE BOT (chat)
Make LINE BOT (Echolalia)
Make a LINE WORKS bot with Amazon Lex
Let's make a Discord Bot.
Make a morphological analysis bot loosely with LINE + Flask
[For play] Let's make Yubaba a LINE Bot (Python)
[Super easy] Let's make a LINE BOT with Python.
[Python] Make your own LINE bot
How to make a slack bot
[LINE bot] I'm a ranger! Part 2
Create a LINE Bot in Django
Make a LINE bot with GoogleAppEngine / py. Simple naked version
Let's make a LINE bot using various services [ngrok edition]
Make a rain notification bot for Hangouts Chat at explosive speed
Creating a LINE bot ~ Creating, deploying, and launching ~
[Python] [LINE Bot] Create a parrot return LINE Bot
Let's make a Twitter Bot with Python!
Python beginners decided to make a LINE bot with Flask (Flask rough commentary)
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
I made a stamp substitute bot with line
Make a Twitter trend bot with heroku + Python
Make a bot for Skype on EC2 (CentOS)
Create a LINE BOT with Minette for Python
Make a rock-paper-scissors game in one line (python)
I made a LINE Bot with Serverless Framework!
[Chat De Tornado] Make a chat using WebSocket with Tornado
Make a squash game
Make a function decorator
Make a distance matrix
LINE BOT if ~ stumbled
I'll make a password!
Make a Nyan button
Make a Tetris-style game!
Make a Base64 decoder
Create a slack bot
[AWS] I made a reminder BOT with LINE WORKS
I made a household account book bot with LINE Bot
Make a BOT that shortens the URL of Discord
I made a LINE BOT with Python and Heroku
Procedure for creating a Line Bot on AWS Lambda
A Python beginner made a chat bot, so I tried to summarize how to make it.
The world's most easy-to-understand explanation of how to make a LINE BOT (1) [Account preparation]
How to make a Cisco Webex Teams BOT with Flask
Make a Blueqat backend ~ Part 1
I made a LINE BOT that returns parrots with Go
Make a Blueqat backend ~ Part 2
Create a machine learning app with ABEJA Platform + LINE Bot
[Django] Make a pull-down menu
Until I return something with a line bot in Django!
Make a bookmarklet in Python
Make a fortune with Python
Make Responder a daemon (service)
I want to send a message from Python to LINE Bot
Parrot return LINE BOT creation
I made a discord bot
Let's make a rock-paper-scissors game
Safety confirmation LINE bot [Explanation]
Make a fire with kdeplot
How to make an interactive LINE BOT 004 (answer the closing date of a listed company)
Make a math drill print
[AWS] I made a reminder BOT with LINE WORKS (implementation)