Slack API Chinese version


Other links: python gets user information in the slack channel
Other links: python sends messages to the slack channel

1. What is Slack?

Slack is chat groups + massive tool integration + file integration + unified search. As of the end of 2014, Slack has integrated 65 tools and services such as email, SMS, Google Drives, Twitter, Trello, Asana, GitHub, etc., which can bring together various fragmented enterprise communication and collaboration.
Slack has three modes: free, paid, and value-added. The subscription fee for enterprise users is the main source of income. In the free mode, users can search for data without paying as much as in the paid mode.
The open integration function will bring more cooperation opportunities to Slack, and will also bring more possibilities to Slack.
The above are all from Baidu Encyclopedia information.
My own understanding is that Slack is a PaaS-side instant messaging software, and the software has super open integration functions, which is why it is so popular with foreign companies. You can use it as a chat software, an alarm information notification platform, a file management platform, a search platform, etc. Slack integrates so many functions, which inevitably brings about a huge overall size and complicated documents. If you want to understand all about Slack, you need It takes hard work. But what is even more valuable is that we don't need to pay attention to what Slack is, and only use a certain function of this huge system to fulfill our needs. Doesn't this just confirm the meaning of Slack: relaxation.

2. What is the Slack API?

Slack provides some external calling methods, which can be called by external programs to achieve specific effects.
90% of manual operations in Slack software can be implemented through programs (java, python, js) using the API provided by Slack.
If you have experience in JAVA development, you can understand that Slack API is the interface method provided by Slack.
View various information in Slack
Slack API chat.postMessage introduction
Slack API users.list introduction

3. How to use Slack API to send messages?

The most common scenario is Slack used by customers, but some information in your product (such as: alarms, notification messages) cannot be directly notified to customers (because customers cannot pay attention to your product all the time) information), so when the customer requests to generate new information in the product, send this information to Slack and @ the corresponding person, telling him: Hey, you have new news.
In this way, customers can know the real-time situation on the Slack mobile phone or computer.

1. Quick start (nanny teaching)

Since you want to send a message to Slack, you must have a Slack user or channel or workspace to accept this message. If you already have this information, please skip step 1.1.
After you have a user or channel, how can you send a message to this user or channel? In what role do you send messages? You can't see who sent the message after calling the Slack API interface, right? Don't worry, Slack provides a robot role through which we can send messages, please move to step 1.2.

1.1 Create Slack workspace, channel, user

Links: create workspace, channel, user
insert image description here
insert image description here
insert image description here
insert image description here

insert image description here
insert image description here
insert image description here

At this point, our workspace, channel, and user have all been created.

1.2 Create a Slack bot and add it to the test channel

Link: Create a robot APP
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

insert image description here
insert image description here
insert image description here
insert image description here

insert image description here

At this point we have created a Slack bot called MyFirstBot with permission to send messages to the channel.
Now we're going to add this bot to the test channel we created earlier:

insert image description here

![](https://img-blog.csdnimg.cn/10f62a5b444d4b94b40fceb82442d8ba.pnginsert image description here

![Insert picture description here](https://img-blog.csdnimg.cn/f3aefb03bbf646e18d96f3ed103cab8b.png

insert image description here
insert image description here
At this point we have finished creating the bot and binding the bot to a channel.

1.3 Python calls the Slack API to send messages to the channel through the robot

The Slack API supports multiple calling methods, including python, js, http, and java. This time I use python.
Get the channel ID here
insert image description here

import json
import requests

def sendMessage2Slack(token,channel,message):

    payload = {
    
    "text": message,"channel": channel}
    data = json.dumps(payload).encode("utf8")
    url = 'https://slack.com/api/chat.postMessage'
    header = {
    
    "Content-Type": "application/json; charset=utf-8", "Authorization": "Bearer " + token}
    response = requests.post(url, data=data, headers=header)
    print(response.text)


if __name__ == "__main__":
    token ="xoxb-*************-************-***************8***" #Bot的Token
    channel = 'C0********' #频道ID
    message = "Hello world ! " #发送的消息
    sendMessage2Slack(token, channel, message)

After running pthon, you can see the messages sent by the Bot in the channel.
insert image description here
At this point we have completed a very simple function of sending messages to the slack channel using python.
There are still many wonderful things in Slack API waiting for your discovery. I will continue to update the Slack API knowledge I have learned. If you have any questions or inappropriate points, please feel free to ask me.

Guess you like

Origin blog.csdn.net/weixin_43807520/article/details/128727253