[PYTHON] LINE Bot that notifies you of the stocks of interest

We have created a BOT that notifies you on LINE of stocks whose 75-day and 200-day moving averages, which are often used in stocks, have a golden cross. The reason why "75 days" and "200 days" are described in note, so please have a look.

Here I will write how I implemented it. The source code is GitHub Is published. The difference from other people is that the source code is very simple because it focuses on the message sending function from the bot side.

What I wanted to do

The golden crossed stocks are published on Stock Tech. The stock market business days are updated every day, but I just forget to go see it, so I thought it would be convenient if there was a bot that would automatically notify me.

The flow is

  1. Get [stock tech] by scraping (https://www.kabutec.jp/contents/compare/com.php?col1=8&scol1=0&col2=26&scol2=0&col3=27&scol3=0&market=0).
  2. Send a message on LINE. is. I wanted to leave it alone, so I decided to run it on Heroku.

Source code

The code was implemented in Python. It is roughly divided into "scraping" and "sending messages via LINE". Scraping is based on the fact that the stock market is closed on Saturdays, Sundays, and holidays.

import os
import datetime
import re

from bs4 import BeautifulSoup
import jpholiday
from linebot import LineBotApi
from linebot.exceptions import LineBotApiError
from linebot.models import TextSendMessage
import requests

KABUTEC_GC_URL = "https://www.kabutec.jp/contents/compare/com.php?col1=8&scol1=0&col2=26&scol2=0&col3=27&scol3=0&market=0"


def get_today_list():
    """
Acquire GC stock from stock tech.
    """
    res = requests.get(KABUTEC_GC_URL)
    soup = BeautifulSoup(res.text, "html.parser")
    li = soup.find_all(href=re.compile("kabutec.jp/company/fs_"))
    company_list = list(map(lambda item: item.contents[0], li))
    return company_list


def get_weekday():
    """
Make a weekday judgment.
    Returns
    -------
    ret : boolean
        True :Weekdays
        False :holiday
    """
    dt_jst = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=9)))
    if dt_jst.weekday() == 5 or dt_jst.weekday() == 6:
        ret = False
    else:
        if jpholiday.is_holiday(dt_jst.date()):
            ret = False
        else:
            ret = True
    return ret


if __name__ == "__main__":
    if get_weekday():
        li = get_today_list()
        line_bot_api = LineBotApi(os.environ["LINE_CHANNEL_ACCESS_TOKEN"])
        msg = "Today's GC brand{num}It is a brand.\n{li}".format(num=len(li), li="\n".join(li))
        print(msg)
        messages = TextSendMessage(text=msg)
        try:
            line_bot_api.broadcast(messages=messages)
        except LineBotApiError:
            pass
    else:
        print("I am out of duty today.")

The LINE Bot part was very simple to write using LINE's Messaging API. Get the "Channel Access Token (Long Term)" from LINE Developers and set "LINE_CHANNEL_ACCESS_TOKEN" as an environment variable.

Others use Flask to receive messages sent by LINE, but if you just send it from the bot side, you don't need Flask either. I'm using line_bot_api.broadcast to broadcast to all users who are friends with the bot.

        line_bot_api = LineBotApi(os.environ["LINE_CHANNEL_ACCESS_TOKEN"])
        msg = "Today's GC brand{num}It is a brand.\n{li}".format(num=len(li), li="\n".join(li))
        print(msg)
        messages = TextSendMessage(text=msg)
        try:
            line_bot_api.broadcast(messages=messages)
        except LineBotApiError:
            pass

When using broadcast, you have to be careful about the number of messages sent. The LINE API itself can be used for free, but the number of messages that can be sent for free is up to 1000 messages / month. Given that the stock market is open on the 20th of the month, if you have more than 50 bot friends, you will not be able to send at the end of the month.

Operation

It's operated on Heroku. If you have a bot that works at a fixed time every day like this time, Heroku only needs two settings.

Setting environment variables

Set LINE_CHANNEL_ACCESS_TOKEN to Config Vars in Heroku's Settings. You can get the channel access token from "Channel Access Token (Long Term)" in LINE Developers.

image.png

Scheduler settings

Add Heroku Scheduler from Installed add-ons. Heroku Scheduler itself is free to use, but you'll need to register a credit card. For example, in the case of this program, if you register the command python main.py as a schedule, it will move at the desired time and a message will be sent from the bot. The schedule itself seems to have an error of several minutes, so be careful with programs that require strict time definition.

image.png

Recommended Posts

LINE Bot that notifies you of the stocks of interest
Create an app that notifies LINE of the weather every morning
I made a slack bot that notifies me of the temperature
The story of making a Line Bot that tells us the schedule of competitive programming
Make a BOT that shortens the URL of Discord
I built an application with Lambda that notifies LINE of "likes" using the Qiita API
Create an app that notifies LINE of the weather every morning
I made a Line bot that guesses the gender and age of a person from an image
Explaining the mechanism of Linux that you do not know unexpectedly
The story of making a university 100 yen breakfast LINE bot with Python
This and that of the inclusion notation.
You search commandlinefu on the command line
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 returns a terrorist image using the Flickr API
Creating a LINE BOT to notify you of additional AtCoder contests using AWS
Create a BOT that displays the number of infected people in the new corona
LINE Bot sent me the scraping results of IT trend information [LINE Messaging API]
[Discode Bot] I created a bot that tells me the race value of Pokemon
I made a github action that notifies Slack of the visual regression test
Check the type of the variable you are using
zsh settings that facilitate the use of virtualenv
One liner that lists the colors of matplotlib
The story of misreading the swap line of the top command
A program that notifies slack of the operating status of fully automatic botanical photography equipment
[Ansible] Example of playbook that adds a character string to the first line of the file
A program that summarizes the transaction history csv data of SBI SECURITIES stocks [Python3]
The world's most easy-to-understand explanation of how to make a LINE BOT (1) [Account preparation]
An article that gives you a little understanding of the rigid sphere collision algorithm