Post a message from IBM Cloud Functions to Slack in Python

procedure:

  1. Set Incoming Webhook on target channel in Slack
  2. Create an action with IBM Cloud functions

1. Set Incoming Webhook on target channel in Slack

Check the Slack documentation to get the URL.

· [Using Incoming Webhooks in Slack] (https://slack.com/intl/ja-jp/help/articles/115005265063-Slack-%E3%81%A7%E3%81%AE-Incoming-Webhook-%E3%81%AE%E5%88%A9%E7%94%A8) · [Sending messages using Incoming Webhooks] (https://api.slack.com/messaging/webhooks)

=> ”https://hooks.slack.com/services/xxxxxxxxx/xxxxxxx/0KX1TxxxxxwP1xxxxx61axxxxx” Get a URL like


2. Create an action with IBM Cloud functions

Log in to the IBM Cloud console-> Go to Functions https://cloud.ibm.com/login

・ [Create Action] (https://cloud.ibm.com/docs/openwhisk?topic=openwhisk-actions#actions_create_ui) Create an action from the console.

** It is assumed that the IBM Cloud Function service has already been created. スクリーンショット 2020-06-09 9.08.20.png

-> create スクリーンショット 2020-06-09 9.10.11.png -> Select Action

スクリーンショット 2020-06-09 9.10.23.png

-> Specify the action name and package, and select Python as the runtime.

Enter the Code. Sample code

import requests
import json

def main(dict):

 body={'username':'Alert','icon_emoji':':star:','text':'IBM Cloud Functions Test'}

 url = "https://hooks.slack.com/services/xxxxxxxxx/xxxxxxx/0KX1TxxxxxwP1xxxxx61axxxxx" #<-URL obtained from Slack
 response = requests.post(url,data=json.dumps(body))
 return {"message" : "IBM Cloud Functions test post to slack"}

Enter usename, icom_emoji, and text (message) in the body part.

For other options, refer to the manual. ・ Chat.postMessage (https://api.slack.com/methods/chat.postMessage)

スクリーンショット 2020-06-09 9.12.22.png -> Press Save at the top right of the code.

スクリーンショット 2020-06-09 9.27.25.png

-> The Save button changes to Invoke. Perform Invoke. (URL is replaced with the target content)

スクリーンショット 2020-06-09 9.21.18.png

-> The execution result is output in the right column of the code.


As a result, the Slack channel will be notified as follows: スクリーンショット 2020-06-08 19.47.01.png

that's all.


reference: -IBM Cloud Functions

Recommended Posts

Post a message from IBM Cloud Functions to Slack in Python
Send a message from Python to Slack
Send a message from Slack to a Python server
Post from Python to Slack
Post to Slack in Python
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
[Python] Throw a message to the slack channel
POST messages from python to Slack via incoming webhook
Post to vim → Python → Slack
Post to slack with Python 3
How to slice a block multiple array from a multiple array in Python
Post a message to Google Hangouts Chat with a thread (Python)
I want to send a message from Python to LINE Bot
[Lambda] [Python] Post to Twitter from Lambda!
Post to slack in Go language
Post images from Python to Tumblr
How to get a string from a command line argument in python
How to connect to Cloud Firestore from Google Cloud Functions with python code
From installing Ansible to building a Python environment in Vagrant's virtual environment
Log in to Slack using requests in Python
How to get a stacktrace in python
How to switch python versions in cloud9
Generate a class from a string in Python
Convert from Markdown to HTML in Python
How to get a value from a parameter store in lambda (using python)
Connect to postgreSQL from Python and use stored procedures in a loop.
Edit Excel from Python to create a PivotTable
Send a message to LINE with Python (LINE Notify)
Try to calculate a statistical problem in Python
How to open a web browser from python
How to clear tuples in a list (Python)
To execute a Python enumerate function in JavaScript
How to embed a variable in a python string
I want to create a window in Python
From file to graph drawing in Python. Elementary elementary
How to create a JSON file in Python
GCP: Repeat from Pub / Sub to Cloud Functions, Cloud Functions to Pub / Sub
How to generate a Python object from JSON
A note on optimizing blackbox functions in Python
How to call Cloud API from GCP Cloud Functions
A clever way to time processing in Python
Steps to develop a web application in Python
To add a module to python put in Julialang
How to notify a Discord channel in Python
Call a Python script from Embedded Python in C ++ / C ++
Create a datetime object from a string in Python (Python 3.3)
[Python] Get one year's message history from Slack
[Python] How to draw a histogram in Matplotlib
[IBM Cloud] I tried to access the Db2 on Cloud table from Cloud Funtions (python)
WEB scraping with python and try to make a word cloud from reviews
Convert the cURL API to a Python script (using IBM Cloud object storage)
I was soberly addicted to calling awscli from a Python 2.7 script registered in crontab
Parse a JSON string written to a file in Python
How to convert / restore a string with [] in python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
Try to make a Python module in C language
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Receive dictionary data from a Python program in AppleScript
I want to write in Python! (2) Let's write a test