[Python] Create a LineBot that runs regularly

Last time Use the created LineBot so that you can send messages regularly.

Create a schedule for periodic execution

Add a schedule to Heroku.

heroku addons:add scheduler:standard

Heroku Scheduler will be added, so select it. スクリーンショット 2020-03-17 0.06.56.jpg

Select Create job to create a new schedule. スクリーンショット 2020-03-17 0.07.11.jpg

Specify when to execute and the command to execute. スクリーンショット 2020-03-17 0.07.58.jpg

Add a file to be executed on a schedule separately from main.py.

scheduler.py


from flask import Flask, request, abort
import os

from linebot import (
    LineBotApi, WebhookHandler
)
from linebot.exceptions import (
    InvalidSignatureError
)
from linebot.models import (
    MessageEvent, TextMessage, TextSendMessage,
)

app = Flask(__name__)

LINE_CHANNEL_ACCESS_TOKEN = os.environ["LINE_CHANNEL_ACCESS_TOKEN"]
LINE_CHANNEL_SECRET = os.environ["LINE_CHANNEL_SECRET"]
USER_ID = os.environ["USER_ID"]
line_bot_api = LineBotApi(LINE_CHANNEL_ACCESS_TOKEN)

def main():
    pushText = TextSendMessage(text="Do you want to register your attendance?")
    line_bot_api.push_message(USER_ID, messages=pushText)
    
if __name__ == "__main__":
    main()

Response message

Make main.py also give a specific response to the received message.

main.py


@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):

    if "Attendance" in event.message.text and "Registration" in event.message.text :
        #registration process

        replyText = "Has registered"
        line_bot_api.reply_message(event.reply_token, TextSendMessage(text=replyText))
    
    line_bot_api.reply_message(event.reply_token, TextSendMessage(text=event.message.text))

Recommended Posts

[Python] Create a LineBot that runs regularly
[Python] Create a linebot that draws any date on a photo
Create a Python environment
Create a page that loads infinitely with python
Create a Wox plugin (Python)
Create a function in Python
Create a dictionary in Python
I made a VM that runs OpenCV for Python
Create a directory with python
Let's create a script that registers with Ideone.com in Python.
Use a macro that runs when saving python with vscode
Create code that outputs "A and pretending B" in python
I made a Line-bot using Python!
Create a python GUI using tkinter
Create a DI Container in Python
Create a Python environment on Mac (2017/4)
Create a virtual environment with Python!
Create a binary file in Python
Create a python environment on centos
Create a Python general-purpose decorator framework
Create a Kubernetes Operator in Python
5 Ways to Create a Python Chatbot
Create a random string in Python
A program that failed when trying to create a linebot with reference to "Dialogue system made with python"
I tried to create a server environment that runs on Windows 10
Easy! Implement a Twitter bot that runs on Heroku in Python
[Python / Django] Create a web API that responds in JSON format
[Ev3dev] Create a program that captures the LCD (screen) using python
[LINE Messaging API] Create a BOT that connects with someone with Python
Let's create a Python directory structure that you won't regret later
I tried to create a linebot (implementation)
[Python] A program that creates stairs with #
Create a new Python numerical calculation project
I tried to create a linebot (preparation)
Create a dummy image with Python + PIL.
Create a python environment on your Mac
Let's create a virtual environment for Python
Create a JSON object mapper in Python
[Python] [LINE Bot] Create a parrot return LINE Bot
Create a word frequency counter with Python 3.4
Create a deb file from a python package
[GPS] Create a kml file in Python
A typed world that begins with Python
Create a bot that boosts Twitter trends
A program that plays rock-paper-scissors using Python
[Python] A program that rounds the score
[Python] Create a linebot to write a name and age on an image
Let's create a customer database that automatically issues a QR code in Python
Create a frame with transparent background with tkinter [Python]
[Python] List Comprehension Various ways to create a list
Edit Excel from Python to create a PivotTable
Create a Vim + Python test environment in 1 minute
Create a GIF file using Pillow in Python
[Python] A tool that allows intuitive relative import
Create an app that guesses students with python
Create a C array from a Python> Excel sheet
I tried to create a class that can easily serialize Json in Python
[python] Create a list of various character types
Create a LINE BOT with Minette for Python
A nice nimporter that connects nim and python
I want to create a window in Python