[PYTHON] I wrote a Slack bot that notifies delay information with AWS Lambda

Introduction

――I had the opportunity to summarize the Slack Bot that I created using AWS Lambda before, so I thought I'd post it to Qiita including a memorandum. ――The memory at the time of creation has faded, but I hope it will be helpful to anyone, such as where they got caught in the range they remember.

How did you come to create the Slack bot in the first place?

――The author's house is located between stations on multiple lines, and it was difficult to move from one station to another. ――It was difficult to take the option of taking another line (moving the station) even if you noticed a delay after arriving at the station. ――It would be nice if you could check the route information yourself when you wake up, but in the morning hours, it was often fluttering, and there were many days when I missed it. ――I felt that it would be convenient if notifications came to the tools (LINE and Slack) that I usually check and let me know. ――I'm genuinely interested in making something with Lambda (including FaaS and SaaS), so I decided to make it myself. -(One of the reasons is that I wanted to program after a long time because I didn't code the occupational pattern at that time.)

Thing you want to do

――I want you to be notified automatically if the train is delayed between the time you wake up in the morning and before you get on the train. --Monday-Friday morning 7-9am --Notification destination is the channel specified by Slack

Configuration diagram and processing image

--I wrote it with draw.io (https://www.draw.io/). --The language I was using is Python 3.x. スクリーンショット 2020-05-24 15.54.44.png

A brief introduction to the technology used

--AWS Lambda: AWS service that can execute event-driven (runs triggered by something (= event)) application - https://aws.amazon.com/jp/lambda/ --Amazon CloudWatch: Resource and application monitoring / management service --This time, I used CloudWatch Event (a function to distribute events to targets (Lambda etc.) based on specific rules). - https://aws.amazon.com/jp/cloudwatch/ --Scraping: Technology that extracts information from information obtained from websites using HTML tags --This time, I used "Beautiul Soup" which is one of Python's scraping libraries. - https://www.crummy.com/software/BeautifulSoup/bs4/doc/ --Slack Incoming Webhooks: Add-ons (like) that post specified messages to specific channels using HTTP requests ――The specifications have changed from when you created it, and it seems that you can add Incoming Webhooks to the created Slack application. - https://api.slack.com/messaging/webhooks

What you made (Slack screen)

――It looks like the image below. (It will check and notify you every 10 minutes.) スクリーンショット 2020-05-24 14.20.44.png

What I thought after actually using it for a while

――I was able to solve the problem at first. ――By receiving the morning notification, you can now select a route (station) that is not delayed. ――I started thinking that it would be nice to be notified when the delay was resolved. ――Currently, it is a specification to judge that the delay has been resolved by not receiving notifications. ――I felt that it would be convenient to make various bots other than this. --Shopping notes, today's schedule (arrival of luggage, etc.) ...

(Finally) I got caught, etc.

――When I thought that the timing of notification was wrong, the time specified was UTC. ――It was necessary to add 9:00 to fix it to Japan time. --Lambda timed out as a result of the information acquisition & scraping process taking a long time. --Lambda's default timeout is 3 seconds, so it looks like you'll need to change it if necessary. (It was possible to change it later with the GUI.)

Recommended Posts

I wrote a Slack bot that notifies delay information with AWS Lambda
I created a Slack bot that confirms and notifies AWS Lambda of the expiration date of an SSL certificate
I made a slack bot that notifies me of the temperature
I wrote a script to create a Twitter Bot development environment quickly with AWS Lambda + Python 2.7
Create a bot with AWS Lambda that automatically starts / stops Instances with specific tags
I made a LINE BOT that returns parrots with Go
I just built a virtual environment with AWS lambda layer
[AWS] I made a reminder BOT with LINE WORKS (implementation)
A story that I was addicted to calling Lambda from AWS Lambda.
[Python] I wrote a REST API using AWS API Gateway and Lambda.
I just did FizzBuzz with AWS Lambda
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
I made a bot to post on twitter by web scraping a dynamic site with AWS Lambda (continued)
I made a stamp substitute bot with line
A memo that I wrote a quicksort in Python
Try implementing a Cisco Spark bot with AWS Lambda + Amazon API Gateway (Python)
LINE BOT with Python + AWS Lambda + API Gateway
I made a SlackBot that notifies me of AtCoder contest information every week
I made a Linebot that notifies me of nearby evacuation sites on AWS
I made a LINE Bot with Serverless Framework!
I tried connecting AWS Lambda with other services
In Python, I made a LINE Bot that sends pollen information from location information.
I made a github action that notifies Slack of the visual regression test
I made a Mattermost bot with Python (+ Flask)
Create a discord bot that notifies unilaterally with python (use only requests and json)
I made a web application that maps IT event information with Vue and Flask
I made a Twitter BOT with GAE (python) (with a reference)
I made a household account book bot with LINE Bot
I made a LINE BOT with Python and Heroku
Create a Layer for AWS Lambda Python with Docker
I want to AWS Lambda with Python on Mac!
A memo that I touched the Datastore with python
Procedure for creating a Line Bot on AWS Lambda
[Python, ObsPy] I wrote a beach ball with Matplotlib + ObsPy
I want to bind a local variable with lambda
Create a slack bot
I built an application with Lambda that notifies LINE of "likes" using the Qiita API
A story that stumbled when I made a chatbot with Transformer
I tried to notify the train delay information with LINE Notify
I wrote a program quickly to study DI with Python ①
Until I return something with a line bot in Django!
I made a rigid Pomodoro timer that works with CUI
Site monitoring and alert notification with AWS Lambda + Python + Slack
I wrote a script that splits the image in two
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
The story of creating a bot that displays active members in a specific channel of slack with python
I wrote GP with numpy
AWS Lambda with PyTorch [Lambda import]
I made a discord bot
[Python] I made a Line bot that randomly asks English words.
I made a package that can compare morphological analyzers with Python
I want to use a wildcard that I want to shell with Python remove
Build a Flask / Bottle-like web application on AWS Lambda with Chalice
A story that I fixed when I got Lambda logs from Cloudwatch Logs
[Python] A memo that I tried to get started with asyncio
I wrote a script to get you started with AtCoder fast!
I made a Twitter bot that mutters Pokemon caught by #PokemonGO
I stopped my instance at a specific time using AWS Lambda
I made a shuffle that can be reset (reverted) with Python
[LINE Messaging API] Create a BOT that connects with someone with Python
Serverless scraping on a regular basis with AWS lambda + scrapy Part 1