Even though I'm an engineering person, I always think about clothes because I go out, but I often make mistakes because I'm a person who doesn't check the weather forecast. Therefore, I would like to teach LINE bot the weather (temperature). We recommend that you first build and test with Echolalia and then do the actual coding.
This article is like a work record & memorandum. If you want to read detailed and easy-to-understand ones, please follow the link below. [I made a LINE BOT with Python + Heroku](https://qiita.com/shimajiri/items/cf7ccf69d184fdb2fb26#flask%E3%81%A8line-bot-sdk%E3%82%92%E3%82%A4 % E3% 83% B3% E3% 82% B9% E3% 83% 88% E3% 83% BC% E3% 83% AB) I started LINE Bot with Python (chilled Chinese) [Part 1] I started LINE Bot with Python (chilled Chinese) [Part 2]
First, register with LINE Developers. Registration is easy. https://developers.line.me/ja/
After registering with LINE Developers, create a provider. For English, switch the language at the bottom right. Create a channel (app) in the provider. I think it's hard to understand because the screen continues
is. It will end soon.
To use the API -** Channel access token ** -** Channel Secret **
Let's move on to Heroku settings. First, let's register an account. Then install a command line interface called Heroku CLI. Heroku CLI : https://devcenter.heroku.com/articles/heroku-cli#download-and-install
At the prompt
$heroku login
When you execute, you will be prompted to enter a key, so enter something. Then you can log in via your browser.
You can create a new app with $ heroku create app name
. Please note that the underscore _
cannot be used.
Set environment variables. The method here seems to be different for each person, but I set it in the same way as the reference site.
Let's set the two strings that we wrote down earlier.
$ heroku config: set YOUR_CHANNEL_ACCESS_TOKEN =" Channel access token "--app App name
$ heroku config: set YOUR_CHANNEL_SECRET =" channel secret "--app app name
Go back to the LINE Developers console and set the URL for your webhook.
Webhook URL: https: // app name.herokuapp.com/callback
Press Update and set to use webhooks and you're done.
https://github.com/line/line-bot-sdk-java/tree/master/sample-spring-boot-echo#step-2 You can deploy EchoBot by pressing [** Deploy to Heroku **] here and creating an app from it.
・ Bot settings in LINE Developers ・ Heroku settings ・ Cooperation between the two Is complete. Start coding from here.
This time, I will mainly use flask
and line-bot-sdk
.
pip install flask line-bot-sdk
In my environment
flask : 1.1.1
line-bot-sdk : 1.16.0
is.
In addition, the library for scraping uses: soupsieve==2.0 urllib3==1.25.8 beautifulsoup4
It can be anywhere, but it's a good idea to create a directory for your project. Constitution: line_bot (work dir) ├ main.py ├ scrape.py ├ Procfile ├ runtime.txt └ requirements.txt
①main.py It is the main body. If you want to test with parrot return [here](https://qiita.com/shimajiri/items/cf7ccf69d184fdb2fb26#flask%E3%81%A8line-bot-sdk%E3%82%92%E3%82%A4%E3 % 83% B3% E3% 82% B9% E3% 83% 88% E3% 83% BC% E3% 83% AB) will be helpful.
main.py
#Import what you need
from flask import Flask, request, abort
import os
import scrape as sc
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
#Get events on LINE
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
)
app = Flask(__name__)
#Get environment variables
YOUR_CHANNEL_ACCESS_TOKEN = os.environ["LINE_BOT_CHANNEL_TOKEN"]
YOUR_CHANNEL_SECRET = os.environ["LINE_BOT_CHANNEL_SECRET"]
line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN)
handler = WebhookHandler(YOUR_CHANNEL_SECRET)
#Executed when the application body is opened
@app.route("/")
def hello_world():
return "hello world!"
#/Processing when accessing the callback link. For webhooks.
@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'
#Event when receiving a message
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
'''
#line_bot_api reply_event with message method.message.text(User message)Reply
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=event.message.text))
'''
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=sc.getWeather())
)
if __name__ == "__main__":
# app.run()
port = int(os.getenv("PORT"))
app.run(host="0.0.0.0", port=port)
②scrape.py Scraping weather information. I was planning to write it myself, but for the time being Click here → https://qiita.com/RIRO/items/1b67b0418b08a52de0d6 I modified the code of. Thank you very much.
scrape.py
#Library import
import requests
from bs4 import BeautifulSoup
def getWeather():
#tenki.URL of the page of the target area of jp (Chofu City, Tokyo this time)
url = 'https://tenki.jp/forecast/3/16/4410/13208/'
#HTTP request
r = requests.get(url)
#Describe the following in the case of a proxy environment
"""
proxies = {
"http":"http://proxy.xxx.xxx.xxx:8080",
"https":"http://proxy.xxx.xxx.xxx:8080"
}
r = requests.get(url, proxies=proxies)
"""
#HTML parsing
bsObj = BeautifulSoup(r.content, "html.parser")
#Get today's weather
today = bsObj.find(class_="today-weather")
weather = today.p.string
#A collection of temperature information
temp=today.div.find(class_="date-value-wrap")
#Get the temperature
temp=temp.find_all("dd")
temp_max = temp[0].span.string #Highest temperature
temp_max_diff=temp[1].string #Compared to the day before the highest temperature
temp_min = temp[2].span.string #Lowest Temperature
temp_min_diff=temp[3].string #Compared to the day before the lowest temperature
#I want to see it move for the time being, so I'm returning the weather and temperature.
return weather+temp_max+temp_min
③Procfile Describes how to execute the program.
web: python main.py
④runtime.txt A text file that describes the Python version. Since an error occurred in 3.7.7, I set it to 3.6.10. Is Heroku not compatible with 3.7?
runtime.txt
python-3.6.10
⑤ requirements.txt A text file that describes the libraries to install.
requirements.txt
Flask==1.1.1
line-bot-sdk==1.16.0
soupsieve==2.0
urllib3==1.25.8
beautifulsoup4
There is scrape.py
because it scrapes the weather information. EchoBot (echolalia) etc. are all right with the other four.
//Create an initial file for git
git init
//Set up a remote repository that connects to your local repository
heroku git:remote -a (App name decided by yourself)
//Index all changed files
git add .
//Write the modified file to the repository ("inital commit"Is a comment, so anything is fine)
git commit -m "inital commit"
//Push files created locally on heroku
git push heroku master
I misspelled beautifulsoup4
in requirements.txt
and got an error.
I thought that the version specification was bad and tried changing the version, but the library name was wrong ()
Check your activity on the Heroku dashboard. If it looks like the image, it is successful.
I used this part by copying the sample.
I get the error The bot server returned an HTTP status code other than 200
.
However, the BOT seems to work normally, so I ignored it.
Let's add a friend with QR on the BOT management screen and send a message. If the weather reply (or parrot return) is successful, it is successful.
Next Currently, we are only sending the weather and temperature as strings.
--Format --Addition of settings (Region and periodic transmission) --Flex Message Challenge
I would like to do something like that. There are other projects and studies, so I will update slowly. Thank you to all the reference articles.
Recommended Posts