[PYTHON] Post to Slack via Subscriber

I posted to Slack via ROS Subscriber. As usual, we do not consider demand.

You can easily connect to Slack using SlackClient.

Preparation

pip install slackclient

Put the Slack Web API library in pip. (What should I do with ROS as a virtual environment?)

code

I'm posting to Slack with a Subscriber callback. Channels and user names may be parameterized.

sc.py


#!/usr/bin/env python

import rospy

from std_msgs.msg import String
from slackclient import SlackClient

token = "TOKEN" #Token is https://api.slack.com/Make on the web
username = "ros"
channel = "#ros"

sc = SlackClient(token)

def callback(message):
    print sc.api_call("chat.postMessage", channel=channel, text=message.data, username=username)

rospy.init_node("rosslack")
sub = rospy.Subscriber("postMessage", String, callback)
rospy.spin()

Run

chmod +x sc.py
./sc.py

Publish using rostopic from another console.

rostopic pub /postMessage std_msgs/String -- 'hoge'

Now, if you look at the Slack screen, you should see a message from ROS.

Recommended Posts

Post to Slack via Subscriber
Post from Python to Slack
Post to vim → Python → Slack
Post to slack with Python 3
Post to Slack in Python
POST messages from python to Slack via incoming webhook
Post Jenkins console output to Slack
Post to slack in Go language
Image characters and post to slack (python slackbot)
[Windows] RDP to Windows via Linux
Post to Twitter using Python
Post a message from IBM Cloud Functions to Slack in Python
Post from python to facebook timeline
[Lambda] [Python] Post to Twitter from Lambda!
How to make a slack bot
Post images from Python to Tumblr
Easily post to twitter with Python 3
[Nanonets] How to post Memo [Python]