[PYTHON] I started to work at different times, so I made a bot that tells me the time to leave

Because of the recent infectious disease, staggered commuting has started. I made a bot that tells me the time to leave when I post the time to work.

environment

reference

For the directory structure of the SlackBot project and the cooperation with Heroku, I referred to the following site. https://qiita.com/akabei/items/ec5179794f9e4e1df203

Source code

I was able to confirm the operation of SlackBot based on the site I referred to above (I closed the fortune), so I will create a code to calculate the leaving time.

・ Calculate the leaving time using the posting time as the arrival time

from datetime import datetime, timedelta, timezone
from slackbot.bot import listen_to

@listen_to(r'^Attendance$')
def work_time(message):
    JST = timezone(timedelta(hours=+9), 'JST')

    start_now = datetime.now(JST)
    end_time = start_now + timedelta(hours=8, minutes=45)
    message.reply(end_time.strftime("%H:%M"))

Execution result ↓ ↓ キャプチャ1.PNG

If this is the case, you will have to send a message immediately after coming to work, so create a pattern that allows you to specify the time.

・ Specify the time of arrival and calculate the time of departure

import re
from datetime import datetime, timedelta, timezone

from slackbot.bot import respond_to, listen_to

@listen_to(r'^Attendance\s[0-9]+:[0-9]+$')
def work_time(message):

    JST = timezone(timedelta(hours=+9), 'JST')
    current = datetime.now(JST)

    text = message.body['text']
    result = re.match(".*\s([0-9]+):([0-9]+)", text)

    hour = result.group(1)
    minute = result.group(2)

    start_now = datetime(
        year=current.year,
        month=current.month,
        day=current.day,
        hour=int(hour),
        minute=int(minute))
    end_time = start_now + timedelta(hours=8, minutes=45)
    message.reply(end_time.strftime("%H:%M"))

Execution result ↓ ↓ キャプチャ2.PNG

No matter what time you come to work, what time is it on time? I don't have to worry

Summary

・ It was easy to deploy SlackBot and be able to interact with it (because there was an article that was summarized in an easy-to-understand manner). ・ Because many people commute to work at different times, the trains at late hours became crowded, so after all, I returned to the original time and there was no place for bots to play an active role.

Recommended Posts

I started to work at different times, so I made a bot that tells me the time to leave
I made a slack bot that notifies me of the temperature
[Python] I made a bot that tells me the current temperature when I enter a place name on LINE
[Discode Bot] I created a bot that tells me the race value of Pokemon
I made a simple timer that can be started from the terminal
I made a library konoha that switches the tokenizer to a nice feeling
I made a LINE bot that tells me the type and strength of Pokemon in the Galar region with Heroku + Flask + PostgreSQL (Heroku Postgres)
I made a tool to estimate the execution time of cron (+ PyPI debut)
I made a LINE BOT that returns a terrorist image using the Flickr API
The story of IPv6 address that I want to keep at a minimum
I made a Line Bot that uses Python to retrieve unread Gmail emails!
I made a LINE Bot that sends recommended images every day on time
I want to make a music player and file music at the same time
With LINEBot, I made an app that informs me of the "bus time"
I want a Slack bot that calculates and tells me the salary of a part-time job from the schedule of Google Calendar!
A Python beginner made a chat bot, so I tried to summarize how to make it.
How to test the current time with Go (I made a very thin library)
A story that I was addicted to at np.where
I made a command to markdown the table clipboard
I made a program in Python that changes the 1-minute data of FX to an arbitrary time frame (1 hour frame, etc.)
I created a Discord bot on Docker that reports the number of corona infected people in Tokyo at a specified time.
I converted the time to an integer, factored it into prime factors, and made a bot to tweet the result (xkcd material)
I made a tool to get the answer links of OpenAI Gym all at once
I was so mushy that I wanted to have a national plane heal me. I have no regrets.
I made a function to crop the image of python openCV, so please use it.
I made a LINE BOT that returns parrots with Go
I made a package to filter time series with python
Grep so that grep does not appear at the time of grep
I made a function to check the model of DCGAN
I want to create a Dockerfile for the time being.
I made a script to say hello at my Koshien
I made a Line bot that guesses the gender and age of a person from an image
I made a function to check if the webhook is received in Lambda for the time being
A story that didn't work when I tried to log in with the Python requests module
・ <Slack> Write a function to notify Slack so that it can be quoted at any time (Python)
I made a discord bot
[Python] I made a Line bot that randomly asks English words.
I made a program to solve (hint) Saizeriya's spot the difference
I want to record the execution time and keep a log.
I made a program that solves the spot the difference in seconds
[Python] A memo that I tried to get started with asyncio
I made a Twitter bot that mutters Pokemon caught by #PokemonGO
I made a library that adds docstring to a Python stub file.
I made a Discord chat reading bot by imitating the appearance
I made a command to display a colorful calendar in the terminal
I made a program that automatically calculates the zodiac with tkinter
[python] A note that started to understand the behavior of matplotlib.pyplot
Neat, whose employment has been postponed to Corona, has started Docker, so I will leave a personal note.
[Hi Py (Part 1)] I want to make something for the time being, so first set a goal.
It seems that the Messaging API has been newly released from LINE, so I made a BOT that just analyzes morphological elements as usual.
I made a wikipedia gacha bot
[Python] I made a decorator that doesn't seem to have any use.
I made a web application in Python that converts Markdown to HTML
When Selenium tells me that the Chrome driver version is different (Python)
[Django] I made a field to enter the date with 4 digit numbers
I made a kitchen timer to be displayed on the status bar!
I made a Discord bot in Python that translates when it reacts
I want to identify the alert email. --Is that x a wildcard? ---
I made a GAN with Keras, so I made a video of the learning process.
I made a program to check the size of a file in Python
Set up a server that processes multiple connections at the same time