[PYTHON] Monitor web page updates with LINE BOT

tl;dr

Introduction

flow

  1. Register with LINE Developers
  2. Get a Channel access token
  3. Make friends with bots on LINE
  4. Write a LINE BOT using line / line-bot-sdk-python
  5. Write update monitoring code using psf / requests-html

Description

Register with LINE Developers

Channel selection

Get a Channel access token

channel access token

Register as a friend

Write code using line / line-bot-sdk-python

Install SDK

$ pip install line-bot-sdk

I will post to all my friends and the group I got earlier

from linebot import LineBotApi
from linebot.models import TextSendMessage

access_token = 'XXXXXXXXXXXXXXX'

line_bot_api = LineBotApi(access_token)
line_bot_api.broadcast(TextSendMessage(text='Broadcast to all friends'))

Operation check

Message example
1578397220270.jpg

4. Write update monitoring code using psf / requests-html

Installation

$ pip install requests-html

How to use

Description
<div class="status-text">
	<div class="status-heading">
		<span class="status">Items that cannot be ordered*</span>
	</div>
	<div class="status-note">
	<p>

	</p>
	</div><!-- /div#status-note -->
</div>
Try scraping
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://XXXXXXXXXXXXX/') # <----Enter the URL you want to access here
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('hxxps://XXXXXXXXXXXXX/') # <----Enter the URL you want to access here
r.html.find('div.status-heading span.status')[0].text # <-----Items that cannot be ordered*

Try to combine it with the notification part of LINE

# -*- coding:utf-8 -*-
from linebot import LineBotApi
from linebot.models import TextSendMessage
from requests_html import HTMLSession

ACCESS_TOKEN = 'XXXXXXXXXXX'
TARGET_URL = 'hxxps://XXXXXXXXXXXXX/rb/16057071/'
STATUS_CSS_SELECTOR = 'div.status-heading span.status'
NG_STATUS = 'Items that cannot be ordered'

def get_status():
    session = HTMLSession()
    r = session.get(TARGET_URL)
    return r.html.find(STATUS_CSS_SELECTOR)[0].text

def broadcast_to_friends(message):
    line_bot_api = LineBotApi(ACCESS_TOKEN)
    line_bot_api.broadcast(TextSendMessage(text=message))

if not NG_STATUS in get_status():
    broadcast_to_friends("You can buy the product:" + TARGET_URL)

|Message example| |:-:|: |1578400246124.jpg|

When you want to post to a specific user or a specific group

Receive webhook

Setting Example

Put a BOT in a group and get a group_id

Setting Example
Example of checking on the inspect screen of ngrok
Screenshot_2020-01-03 ngrok - Inspect.png

To push_message instead of broadcast

-line_bot_api.broadcast(TextSendMessage(text='Broadcast to all friends'))
+line_bot_api.push_message(group_id, TextSendMessage(text='Push message to an individual'))

Finally

Recommended Posts

Monitor web page updates with LINE BOT
Web scraping with BeautifulSoup4 (layered page)
Monitor Python web apps with Prometheus
Display Disney's waiting time with LINE bot
[Personal note] Web page scraping with python3
Web scraping with BeautifulSoup4 (serial number page)
Create a LINE BOT with Minette for Python
[python] Quickly fetch web page metadata with lassie
I made a LINE Bot with Serverless Framework!
Serverless LINE bot made with IBM Cloud Functions
Make a LINE WORKS bot with Amazon Lex
Extract data from a web page with Python
[AWS] I made a reminder BOT with LINE WORKS
I made a household account book bot with LINE Bot
Make a morphological analysis bot loosely with LINE + Flask
[Super easy] Let's make a LINE BOT with Python.
[LINE Messaging API] Create parrot return BOT with Python
Web page summary (preprocessing)
Make LINE BOT (Echolalia)
LINE BOT if ~ stumbled
Flask-Python realization web page
I made a LINE BOT that returns parrots with Go
Create a machine learning app with ABEJA Platform + LINE Bot
[Part.2] Crawling with Python! Click the web page to move!
Until I return something with a line bot in Django!
Display a web page with FastAPI + uvicorn + Nginx (SSL / HTTPS)
[AWS] I made a reminder BOT with LINE WORKS (implementation)