Logic Apps, a PaaS service on Azure, GA in July 2016. This time, I tried to notify Slack using only the basic functions of Logic Apps.
On the on-premise side, I put raspbian in the Raspberry Pi 2 Model B that is left over from the company. This time I will use Python to send to Azure Service Bus.
Azure has Logic Apps and Service Bus that exchanges messages.
For Logic Apps, you can select Twitter, Slack, etc. as the notification destination to social, but this time I chose Slack.
Python is already installed in rapbian, and the pip command to add various modules is also included, so after that, you can enter the following command and prepare to use the Azure SDK.
$ sudo pip install azure
The above command will install and use the Azure SDK for Python.
Create quickly from the portal screen. Recently, it has become available in the East Japan and West Japan regions.
This is also created quickly from the portal screen. Since the price plan is for verification purposes, the cheapest Basic and the region were created in eastern Japan.
The name of the queue is pythontest, and the other items are for verification purposes, so create them with the defaults.
Set the Shared access policies for the created queue. Grants queue send and browse permissions to the policy name "user".
Make a note of the contents of the Policy name (user) and PRIMARY KEY of the created Shared access policies because they will be embedded in the code on the Python side. The contents of the CONNECTION STRING-PRIMARY KEY are required on the Logic Apps side, so make a note of them as well.
When you deploy and first press "Edit" in Logic Apps, the Logic Apps Designer will open, so this time select "Empty Logic App". Since the designer screen opens, first set the trigger of Service Bus Queue. Select "Service Bus --When a message is received in a queue" and name the connection appropriately. For the connection string, enter the CONNECTION STRING of the Sercice Bus created earlier, but exclude the "Entity Path" at the end of the elements in that CONNECTION STARING. (Include only Endpoint, ShareAccessKeyName, SharedAccessKey)
Next, enter the name of the queue created in Service Bus in "Queue Name", and set the frequency and interval appropriately.
Then add an action. Click "Add Action" from "+ New Step".
If you select Slack, it will be added.
After that, when you sign in to Slack, you can enter the channel name to post to Slack and the message to post. This time, in the MESSAGE TEXT field, we will simply use the content sent from Service Bus from the screen as it is.
Finally, don't forget to "save".
This is an example program on the sending side. Enter the name of the created Service Bus in service_namespace. Paste the contents of the Policy name and PRIMARY KEY that you wrote down earlier into share_access_key_name and shared_access_key_value. The first argument of bus_service.send_event sets the name of the queue created earlier (python test).
sender.py
# -*- coding: utf-8 -*-
import json
from azure.servicebus import ServiceBusService, Message, Queue
if __name__ == '__main__':
bus_service = ServiceBusService(
service_namespace='ktkrqiitatest',
shared_access_key_name='user',
shared_access_key_value='XXXXXXXXXXXXXXXXXXXXXXXXXX')
msg = Message('Hello! World')
bus_service.send_event('pythontest', msg)
Now let's run a Python script.
$ python sender.py
Confirm that it was executed normally on the portal screen.
I've also received a notification message from BOT on Slack's #logicappstest channel!
This time I tried using Azure Logic Apps with a very simple mechanism, but the Azure side was all completed with just UI operations. If you want to create elaborate branches or actions, you need to edit the JSON code directly, but I would like to try it at another time.
Recommended Posts