[For play] Let's make Yubaba a LINE Bot (Python)

Introduction

After seeing @ Nemesis's implementation of Yubaba in Java, you might want to enter your name and play with Yubaba. ?? I thought it was a bot. You can add bot friends from the ** QR code below **. Please play with it.

I would like to inform the world that such a thing is popular in the engineer area, so please take a look at LGMT.

Screen Shot 2020-11-11 at 22.17.43.png

About implementation

The language used was Python. It's a "" contract. Write your name there. It was a little annoying that a request from the user was required to activate "", but it was annoying, so I tried to respond by saying "** work **".

Basically, please play only in the part where the characters are cut.

code

main.py


from flask import Flask, request, abort
import random

from linebot import (
    LineBotApi, WebhookHandler
)
from linebot.exceptions import (
    InvalidSignatureError
)

from linebot.models import (
    MessageEvent, TextMessage, TextSendMessage)
import os

app = Flask(__name__)

YOUR_CHANNEL_ACCESS_TOKEN = 'XXXXXXXXXXX'
YOUR_CHANNEL_SECRET = 'XXXXXXXXXXX'

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


@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:
        abort(400)
    return 'OK'


@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
    word = event.message.text
    if 'Let me work' in word:
        reply_word = "It's a contract. Write your name there."
    else:
        newName = random.choice(word)
        reply_word = f"Hung.{word}I mean. It's a luxurious name. From now on your name is{newName}It is. Mind you,{newName}That's right. I'll reply when I understand{newName}!!"

    line_bot_api.reply_message(
        event.reply_token,
        TextSendMessage(text=reply_word))


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

Execution result

It cuts out nicely.

Screen Shot 2020-11-11 at 22.34.10.png

Impressions

** Please forgive me for not being able to throw the usual error because I can't send an empty string. ** **

It is a mystery why one-character cutting is popular. I want to randomly cut out the number of characters.

LINE bot is too convenient, so I want to know more features.

that's all. Screen Shot 2020-11-11 at 22.17.43.png

Recommended Posts

[For play] Let's make Yubaba a LINE Bot (Python)
[Super easy] Let's make a LINE BOT with Python.
Let's make a Twitter Bot with Python!
Create a LINE BOT with Minette for Python
Let's make a module for Python using SWIG
Let's make a Discord Bot.
Make a LINE BOT (chat)
[Python] Make your own LINE bot
Let's make a GUI with python.
Let's make a graph with python! !!
Let's make a LINE bot using various services [ngrok edition]
Let's make a shiritori game with Python
Let's make a voice slowly with Python
[Python] [LINE Bot] Create a parrot return LINE Bot
Make Qt for Python app a desktop app
Let's make a web framework with Python! (1)
Let's make a combination calculation in Python
Let's make a web framework with Python! (2)
Let's make a Backend plugin for Errbot
Python beginners decided to make a LINE bot with Flask (Flask rough commentary)
Make a bot for Skype on EC2 (CentOS)
Make a rock-paper-scissors game in one line (python)
Let's replace UWSC with Python (5) Let's make a Robot
Make a LINE WORKS bot with Amazon Lex
Make LINE BOT (Echolalia)
If you want to make a discord bot with python, let's use a framework
Make a morphological analysis bot loosely with LINE + Flask
Let's make a simple game with Python 3 and iPhone
Make a parrot return LINE Bot on AWS Cloud9
Procedure for creating a Line Bot on AWS Lambda
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
Make a bookmarklet in Python
Make a fortune with Python
Let's make a rock-paper-scissors game
Let's make a websocket client with Python. (Access token authentication)
Experiment to make a self-catering PDF for Kindle with Python
Let's make a spot sale service 4 (in Python mini Hack-a-thon)
How to make a Python package (written for an intern)
Create a Twitter BOT with the GoogleAppEngine SDK for Python
I want to send a message from Python to LINE Bot
Let's make a remote rumba [Hardware]
[Python] I made a Line bot that randomly asks English words.
Make python segfault in one line
Let's make a WEB application for phone book with flask Part 1
Let's make a remote rumba [Software]
Make a Tweet box for Pepper
Let's make a spot sale service 2
How to make a slack bot
Let's make a breakout with wxPython
Let's make a spot sale service 1
Let's play with Excel with Python [Beginner]
[Python] Make the function a lambda function
Make a recommender system with python
[Let's play with Python] Aiming for automatic sentence generation ~ Completion of automatic sentence generation ~
[LINE Messaging API] Create a BOT that connects with someone with Python
Let's make a positive / negative judgment tool into a band graph (Python)
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
Let's make a WEB application for phone book with flask Part 3
[LINE bot] I'm a ranger! Part 2
Let's make a WEB application for phone book with flask Part 4
Let's make a supercomputer with xCAT