I made a LINE bot that tells me the type and strength of Pokemon in the Galar region with Heroku + Flask + PostgreSQL (Heroku Postgres)

Introduction

As the title says, I made a LINE bot that will tell you information about Pokemon that appears in the Galar region (Sword Shield), so I would like to summarize it briefly. Below is a screen image.

image.png

system.png

Please refer to [GitHub] for the file structure.

Local environment

Install the required libraries from the following Pipfile using pipenv. For some reason psycopg2 cannot be installed normally, so I have included psycopg2-binary. (The cause of this area is unknown.)

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
flask = "==1.1.2"
line-bot-sdk = "==1.16.0"
sqlalchemy = "==1.3.17"
psycopg2-binary = "==2.8.5"

[requires]
python_version = "3.8"

Settings on the LINE API side

-(I think most people have one) As a prerequisite, you need a LINE account. If you already have an account, please register to use the API from [LINE Developers]. --Create a new channel after registration. --After adding a channel, go to [Channel Basic Settings]-> [LINE Official Account Manager]-> [Response Settings Tab], and then go to Response Message OFF (it can be ON, but it is a little subtle if the default message is inserted one by one). Set to Webhook ON. --Check the channel secret in the channel preferences and the channel access token in the Messaging API settings. This will be needed later when setting environment variables on Heroku.

(This is needed after working on Heroku) --Go to Webhook Settings from Messaging API Settings and enter https: // {app name} .herokuapp.com / callback in Webhook URL.

Heroku side settings

-Register at [Heroku]. --Install the required commands with brew. --Login with the following command. --Set environment variables with the following command.

$ brew install heroku
$ brew install postgresql
$ heroku login
$ heroku config:set LINE_CHANNEL_ACCESS_TOKEN="***********" --app {app name}
$ heroku config:set LINE_CHANNEL_SECRET="***********" --app {app name}

Preparing PostgreSQL

--Create an empty DB while logged in to heroku. --Connect to the DB from the terminal. --Change DB timezone ――I will download a nice CSV data from Kaggle. ――It's Kaggle, but the data of Pokemon is quite substantial. (As expected, global content) --Create a table. (The SQL statement below is an example.) --Copy CSV data to DB. This is because the with csv header below ignores the CSV header and imports it. Please change it if necessary.

$ heroku addons:create heroku-postgresql:hobby-dev -a {app name}
$ heroku pg:psql -a {app name}

#Start connection, create table
app-name::DATABASE=> alter database {Database name} set timezone = 'Asia/Tokyo';
app-name::DATABASE=> create table pokemon_status(
app-name::DATABASE=> id integer not null,
...
...
app-name::DATABASE=> type2 text,
app-name::DATABASE=> primary key (id));

#Copy local CSV to DB
\copy pokemon_status from '{file name}' with csv header;

I think it is registered like this. スクリーンショット 2020-06-13 12.44.55.png

Various files

runtime.txt List the Python version.

python-3.8.1

requirements.txt Describes the libraries required on the server side.

Flask==1.1.2
line-bot-sdk==1.16.0
psycopg2==2.8.5

Procfile A file required by Heroku that describes how to run the program.

web: python main.py

The main file will be a bit long, so see [GitHub].

in conclusion

PS5 fun! !!

Recommended Posts

I made a LINE bot that tells me the type and strength of Pokemon in the Galar region with Heroku + Flask + PostgreSQL (Heroku Postgres)
I made a LINE BOT with Python and Heroku
[Discode Bot] I created a bot that tells me the race value of Pokemon
I made a slack bot that notifies me of the temperature
I made a Line bot that guesses the gender and age of a person from an image
[Python] I made a bot that tells me the current temperature when I enter a place name on LINE
I made a twitter app that decodes the characters of Pricone with heroku (failure)
I want a Slack bot that calculates and tells me the salary of a part-time job from the schedule of Google Calendar!
I made a LINE BOT that returns parrots with Go
I made a Nyanko tweet form with Python, Flask and Heroku
I made a mistake in fetching the hierarchy with MultiIndex of pandas
I started to work at different times, so I made a bot that tells me the time to leave
I made a stamp substitute bot with line
I made a LINE BOT that returns a terrorist image using the Flickr API
With LINEBot, I made an app that informs me of the "bus time"
[Python] I made a LINE Bot that detects faces and performs mosaic processing.
I made a LINE Bot with Serverless Framework!
In Python, I made a LINE Bot that sends pollen information from location information.
I made a Mattermost bot with Python (+ Flask)
I made a system with Raspberry Pi that regularly measures the discomfort index of the room and sends a LINE notification if it is a dangerous value
I made a web application that maps IT event information with Vue and Flask
I made a household account book bot with LINE Bot
I don't like to be frustrated with the release of Pokemon Go, so I made a script to detect the release and tweet it
A server that returns the number of people in front of the camera with bottle.py and OpenCV
Until I return something with a line bot in Django!
[AWS] I made a reminder BOT with LINE WORKS (implementation)
The story of creating a bot that displays active members in a specific channel of slack with python
[Python] I made a Line bot that randomly asks English words.
I made a program that solves the spot the difference in seconds
I made a Twitter bot that mutters Pokemon caught by #PokemonGO
I made a program that automatically calculates the zodiac with tkinter
I made a music bot using discord.py and Google Drive API (tested with Docker → deployed to Heroku)
The story of making a university 100 yen breakfast LINE bot with Python
I made a calendar that automatically updates the distribution schedule of Vtuber
I made a Discord bot in Python that translates when it reacts
I made a GAN with Keras, so I made a video of the learning process.
What I investigated in the process of expressing (schematicizing) containers in a nested frame with Jupyter and making it
[Python] I made a web scraping code that automatically acquires the news title and URL of Nikkei Inc.
I made a program in Python that reads CSV data of FX and creates a large amount of chart images
Create a simple app that incorporates the Fetch API of Ajax requests in Flask and explain it quickly
I created a Slack bot that confirms and notifies AWS Lambda of the expiration date of an SSL certificate
I made a Line Bot that uses Python to retrieve unread Gmail emails!
I made a LINE Bot that sends recommended images every day on time
I made an appdo command to execute a command in the context of the app
I made a SlackBot that notifies me of AtCoder contest information every week
I made a Linebot that notifies me of nearby evacuation sites on AWS
I made a familiar function that can be used in statistics with Python
Create a BOT that displays the number of infected people in the new corona
I compared the moving average of IIR filter type with pandas and scipy
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
I made a github action that notifies Slack of the visual regression test
I made a Python program for Raspberry Pi that operates Omron's environmental sensor in the mode with data storage