简体   繁体   English

如何更改 python 中的 Slack 机器人图标?

[英]How do I change my Slack bot icon in python?

I'm very (very) novice at playing with the Slack api - so be gentle and use short words.我在玩 Slack api 方面是非常(非常)新手 - 所以要温和并使用简短的单词。 So far I have managed to set up a simple system that can post to our Slack channel.到目前为止,我已经设法建立了一个可以发布到我们的 Slack 频道的简单系统。 All well and good, but the icon associated with the posts is the default.一切都很好,但与帖子关联的图标是默认图标。 How do I go about setting a different icon or even one I create myself? go 如何设置不同的图标,甚至是我自己创建的图标?

Here is the basic code (snagged from a tutorial listed on teh Slack api dev site):这是基本代码(摘自 Slack api 开发站点上列出的教程):

import requests
import json

url = 'https://slack.com/api/chat.postMessage'
token = 'xoxb-00000000000etc'


headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer {0}'.format(token)}

def send_message(message_text):
    message = {'channel': '#channel_name', 'text': message_text}
    requests.post(url, headers=headers, data=json.dumps(message))

send_message('beep boop - this is the OMS bot calling')

Many thanks...非常感谢...

To set the icon image for your message you can simple provide the property icon_url with a URL to an image.要为您的消息设置图标图像,您可以简单地为属性icon_url提供图像的 URL。

Example:例子:

message = {'channel': '#channel_name', 'text': message_text, 'icon_url': 'https://img.icons8.com/emoji/96/000000/penguin--v2.png'}

You can see all available properties on the official documentation page for the API method chat.postMessage .您可以在 API 方法chat.postMessage的官方文档页面上查看所有可用属性。

Since you said you very a beginner allow me to make two additional suggestions.既然你说你是一个初学者,请允许我提出两个额外的建议。

1 - The offical Slack library 1 - 官方 Slack 库

There is an official Slack library for Python which makes thinks much easier, eg you don't need to deal with the requests library and HTTP headers.有一个用于 Python 的官方 Slack 库,它使思考更容易,例如,您不需要处理请求库和 HTTP 标头。 You find it here: https://github.com/slackapi/python-slackclient你可以在这里找到它: https : //github.com/slackapi/python-slackclient

2 - Slack token in environment variables 2 - 环境变量中的 Slack 令牌

For security reasons it is good practice to put your slack token in an environment variable.出于安全原因,将您的 slack 令牌放在环境变量中是一种很好的做法。 That way you also can check in your code into github etc.这样你也可以将你的代码签入github等。

Updated example更新示例

Here is your example with the two suggested improvements:这是您的示例,其中包含两个建议的改进:

import slack
import os

client = slack.WebClient(token=os.environ['SLACK_TOKEN'])

response = client.chat_postMessage(        
    channel='general',
    text='beep boop - this is the OMS bot calling',
    icon_url='https://img.icons8.com/emoji/96/000000/penguin--v2.png'
)

When passing icon_url or icon_emoji to a bot, remember that the scope chat:write.customize is required .icon_urlicon_emoji传递给机器人时,请记住scope chat:write.customize是必需的 Otherwise it will have no effect.否则将没有效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM