2017-03-27 50 views

回答

3

以下是通过发送消息到Slack频道的示例。蟒蛇。您必须为您的Slack组配置一个Slack Web钩子,然后您可以将其添加到hook变种。

import json, requests 

def sendMessageToSlack(): 
    hook = "https://hooks.slack.com/services/<hook goes here>" 
    headers = {'content-type': 'application/json'} 
    payload = {"attachments":[ 
        { 
        "fallback":"", 
        "pretext":"", 
        "color":"#fff", 
        "fields":[ 
         { 
          "title":"", 
          "value":"", 
          "short": False 
          } 
         ] 
        } 
       ] 
       } 

     r = requests.post(hook, data=json.dumps(payload), headers=headers) 
     print("Response: " + str(r.status_code) + "," + str(r.reason)) 

提供响应返回200代码,您应该在您的频道中留言。

1

我不知道你为什么要利用flask ...

我想你可以只使用Slack API clientDocumentation在一起。基本用法就是你要找的东西(着色等)。

不建议使用您自己的解决方案 - 有时API更改,您将不得不维护它。官方API客户端为您提供了抽象级别,您不必担心突发错误。

发布消息(从GitHub复制):

from slackclient import SlackClient 

slack_token = os.environ["SLACK_API_TOKEN"] 
sc = SlackClient(slack_token) 

sc.api_call(
    "chat.postMessage", 
    channel="#python", 
    text="Hello from Python! :tada:" 
) 

您可以在this website生成测试令牌,并亲身体验。

相关问题