I want to know the weather with LINE bot feat.Heroku + Python

Purpose

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.

reference

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]

Register with LINE Developers

First, register with LINE Developers. Registration is easy. https://developers.line.me/ja/

Creating Providers & Channels

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

  1. Press Create Provider
  2. Enter provider information (project-like)
  3. Enter the channel information (such as the name of the bot)

is. It will end soon.

Get the information you need

To use the API -** Channel access token ** -** Channel Secret **

Heroku settings

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

Heroku login

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.

Creating a Heroku application

You can create a new app with $ heroku create app name. Please note that the underscore _ cannot be used.

Setting environment variables

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

Webhook settings

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.

coding

・ Bot settings in LINE Developers ・ Heroku settings ・ Cooperation between the two Is complete. Start coding from here.

Library installation

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

Working directory and its structure

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

The contents of the file

①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.

Deploy to Heroku


//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 got an error here

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 on Heroku side

Check your activity on the Heroku dashboard. If it looks like the image, it is successful. b7276e159aa0cadcfe46ba23ed0d4cc4.png

webhook validation error

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.

Operation check

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

I want to know the weather with LINE bot feat.Heroku + Python
[Python] I asked LINE BOT to answer the weather forecast.
I want to inherit to the back with python dataclass
I want to debug with Python
[Python] I want to use the -h option with argparse
I want to send a message from Python to LINE Bot
I want to know the features of Python and pip
I want to analyze logs with Python
I want to play with aws with python
I want to output the beginning of the next month with Python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I tried to make the weather forecast on the official line by referring to the weather forecast bot of "Dialogue system made with python".
I want to use MATLAB feval with python
I want to make a game with Python
I want to use Temporary Directory with Python2
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
I want to write to a file with Python
I want to display the progress in Python!
[Python] I want to know the variables in the function when an error occurs!
I want to handle optimization with python and cplex
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I want to work with a robot in python.
I made a LINE BOT with Python and Heroku
I want to write in Python! (3) Utilize the mock
I want to AWS Lambda with Python on Mac!
[ML Ops] I want to do multi-project with Python
I tried to solve the problem with Python Vol.1
I want to get the file name, line number, and function name in Python 3.4
I want to use the R dataset in python
I want to run a quantum computer with Python
I tried to automatically send the literature of the new coronavirus to LINE with Python
I tried to put out the frequent word ranking of LINE talk with Python
I tried to find the entropy of the image with python
I want to be able to analyze data with Python (Part 3)
I want to initialize if the value is empty (python)
I tried to simulate how the infection spreads with Python
I want to specify another version of Python with pyvenv
I wanted to solve the Panasonic Programming Contest 2020 with Python
I want to save the photos sent by LINE to S3
I tried to notify the train delay information with LINE Notify
maya Python I want to fix the baked animation again.
I want to be able to analyze data with Python (Part 1)
I want to change the Japanese flag to the Palau flag with Numpy
I want to be able to analyze data with Python (Part 4)
What I did to welcome the Python2 EOL with confidence
I want to be able to analyze data with Python (Part 2)
I want to automatically attend online classes with Python + Selenium!
I didn't know how to use the [python] for statement
I want to know the legend of the IT technology world
I tried to divide the file into folders with Python
Get the weather with Python requests
Get the weather with Python requests 2
I liked the tweet with python. ..
I want to do ○○ with Pandas
I know? Data analysis using Python or things you want to use when you want with numpy
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
I tried to solve the ant book beginner's edition with python
I want to use a wildcard that I want to shell with Python remove
I want to monitor UNIQLO + J page updates [Scraping with python]