[Ruby] I tried to link AWS I oT button and Slack

3 minute read

Introduction

I made a Slack notification button using AWS IoT button.

  • Actually, I made it more than half a year ago, but I could not publish the article quite easily!

It’s the company’s water server, but the person who used the last one has a flow to inform the general affairs with Slack, but I forget to inform me when I return to my seat.

So I started with the idea of using the IoT button to notify Slack on the spot so that I will not forget it. It’s problem-solving driven.

After that there was an AWS IoT button there.

Let’s do it!

Items to prepare

AWS IoT Button ✕ 1 (¥2,500) https://aws.amazon.com/jp/iotbutton/ It’s an Amazon dash button because I see it This time, I will use this. It seems that you need a PC that will be a server with Amazon Dash button, is it easy to set up and is not this expensive or expensive? So use this.

AWS account ✕ 1 (USD$0.25/month) It seems that it costs about 27 yen per month. https://aws.amazon.com/jp/iot-1-click/pricing/

Enable IoT Button

First, register the AwS IoT button. There seems to be a method of registering from the console and a method of registering with a smartphone, but it seems easy to do with a smartphone.

Download the app from the Store

Install the app from the App Store for iOS or Google Play for Android. I registered on iOS. iOS: https://apps.apple.com/us/app/aws-iot-button/id1178216626 Android: https://play.google.com/store/apps/details?id=com.amazonaws.iotbutton

Log in to AWS to register your device and establish a Wifi connection. Pochipochi. Pippi. All you have to do is scan the barcode on the side of the AWS IoT button with the camera to register. IMG_6601.PNG IMG_6602.PNG

Log in to the AWS console from your PC and activate it.

I took a screenshot after setting it, but I think it’s initially disabled, and the project and placement are unset. ![Screenshot 2019-12-11 18.13.44.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/200156/4f03c70d-3001-c5ef-3b42-(1bcf4efb3f6e.png)

Preparation for Slack integration

Add the Incoming Webhook to the Slack App directory and issue the Webhook URL. Also set the name and icon for the Incoming Webhook and preview the message. ▼I tried it like this ![Screenshot 2019-12-16 16.41.19.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/200156/88d8357c-2aff-80a8-c18c-(d270f57f55d0.png)

Create Lambda function

Prepare a script to run using Lambda on AWS. The script this time looked like this.


require "net/https"
require "uri"

def lambda_handler(event:, context:)
    uri = URI.parse('https://hooks.slack.com/services/XXXXXXXXXXX/XXXXXXXXXXX/XXXXXXXXXXXXXXXXXX')
    res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == "https") {|http|
        http.request(Net::HTTP::Post.new(uri).tap {|req|
            req.body = {
                channel:'Slack channel name you want to be notified',
                username:'Username of Slack post set when webhook',
                text:'<@mention user name to skip> Aqua Clara, the last one has been installed. ',
            }.to_json
        })
    }
    {statusCode: 200, body: res.to_s}
end

Let’s create a test. Anything seems fine, so this time I will create it using the settings of the existing template called HelloWorld as it is, execute it and check the posting to Slack. ![Screenshot 2019-12-16 16.55.33.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/200156/9b4242af-aaea-4ca7-bfd2-(8f443455cae8.png) If you can post to Slack, the code is OK for the time being. Next is the link between the button and AWS.

AWS IoT1-Link to device on Click page

Creating a project

Finally, create a project that links the IoT button and Lambda function from the AWS IoT1-Click page. Log in to the console and open the AWS IoT1-Click page. ![Screenshot 2019-12-26 17.59.41.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/200156/c67834d0-2dbd-819c-8858-(4682e1ba4d0e.png) After entering the project name, you can select the device template name and action. Specify the Lambda function you created. ![Screenshot 2019-12-26 18.01.09.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/200156/71beb23f-6ca3-3658-4a20-(75a9df40a309.png)

Creating a placement

Specify a device and create a placement. Specify the device and enter the attribute name and value ![Screenshot 2019-12-26 18.16.00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/200156/35362bd2-97a4-bd1c-2f21-(3731695426ac.png)

Check the Slack integration by pressing the ### button It is OK if it is linked!

Reference site

This article is based on the Qiita article below. Thanks to you, it has been working well for more than half a year, which is convenient! https://qiita.com/cumet04/items/11bc8883ae9cc603b84d