如何通过Mattermost传入的Webhook发送文件? [英] How to send file through Mattermost incoming webhook?

查看:618
本文介绍了如何通过Mattermost传入的Webhook发送文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够通过最重要频道.mattermost.com/developer/webhooks-incoming.html"rel =" nofollow noreferrer>传入的Webhooks

I am able to send text to Mattermost channel through incoming webhooks

import requests, json
URL = 'http://chat.something.com/hooks/1pgrmsj88qf5jfjb4eotmgfh5e'
payload = {"channel": "general", "text": "some text"}
r = requests.post(URL, data=json.dumps(payload))

此代码简单地发布文本.我找不到将文件发布到频道的方法.假设我要发布位于/home/alok/Downloads/Screenshot_20170217_221447.png的文件.如果有人知道,请分享.

this code simplly post text. I could not find a way to post file to channel. Suppose I want to post file located at /home/alok/Downloads/Screenshot_20170217_221447.png. If anyone know please share.

推荐答案

您目前无法使用传入的Webhooks API附加文件.您需要使用 Mattermost Client API 来发布带有附加文件的帖子.

You can't currently attach files using the Incoming Webhooks API. You would need to use the Mattermost Client API to make a post with files attached to it.

这里是如何实现此目的的示例(将Mattermost API v3用于Mattermost> = 3.5)

Here's an example of how you could achieve that (using Mattermost API v3 for Mattermost >= 3.5)

SERVER_URL = "http://chat.example.com/"
TEAM_ID = "team_id_goes_here"
CHANNEL_ID = "channel_id_goes_here"
USER_EMAIL = "you@example.com"
USER_PASS = "password123"
FILE_PATH = '/home/user/thing_to_upload.png'

import requests, json, os

# Login
s = requests.Session() # So that the auth cookie gets saved.
s.headers.update({"X-Requested-With": "XMLHttpRequest"}) # To stop Mattermost rejecting our requests as CSRF.

l = s.post(SERVER_URL + 'api/v3/users/login', data = json.dumps({'login_id': USER_EMAIL, 'password': USER_PASS}))

USER_ID = l.json()["id"]

# Upload the File.
form_data = {
        "channel_id": ('', CHANNEL_ID),
        "client_ids": ('', "id_for_the_file"),
        "files": (os.path.basename(FILE_PATH), open(FILE_PATH, 'rb')),
}
r = s.post(SERVER_URL + 'api/v3/teams/' + TEAM_ID + '/files/upload', files=form_data)

FILE_ID = r.json()["file_infos"][0]["id"]

# Create a post and attach the uploaded file to it.
p = s.post(SERVER_URL + 'api/v3/teams/' + TEAM_ID + '/channels/' + CHANNEL_ID + '/posts/create', data = json.dumps({
    'user_id': USER_ID,
    'channel_id': CHANNEL_ID,
    'message': 'Post message goes here',
    'file_ids': [FILE_ID,],
    'create_at': 0,
    'pending_post_id': 'randomstuffogeshere',
}))

这篇关于如何通过Mattermost传入的Webhook发送文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆