首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过Mattermost传入的webhook发送文件?

如何通过Mattermost传入的webhook发送文件?
EN

Stack Overflow用户
提问于 2017-02-18 03:05:37
回答 3查看 7.3K关注 0票数 3

我可以通过incoming webhooksMattermost频道发送文本

代码语言:javascript
复制
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.的文件如果有人知道,请分享。

EN

回答 3

Stack Overflow用户

发布于 2017-03-01 17:46:25

您当前不能使用传入的Webhooks API附加文件。您需要使用Mattermost Client API来发布附加了文件的帖子。

这里有一个如何实现这一点的示例(使用Mattermost >= 3.5的Mattermost API v3 )

代码语言:javascript
复制
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',
}))
票数 5
EN

Stack Overflow用户

发布于 2019-04-16 23:52:13

我已经为API v4做了一个版本,使用了个人访问令牌。https://docs.mattermost.com/developer/personal-access-tokens.html

代码语言:javascript
复制
import os
import json
import requests
SERVER_URL = "YOUR_SERVER_URL"
CHANNEL_ID = "YOUR_CHANNEL_ID"
FILE_PATH = './test.jpg'

s = requests.Session()
s.headers.update({"Authorization": "Bearer YOUR_PERSONAL_ACCESS_TOKEN"})

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/v4/files', files=form_data)

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

p = s.post(SERVER_URL + '/api/v4/posts', data=json.dumps({
    "channel_id": CHANNEL_ID,
    "message": "YOUR_MESSAGE",
    "file_ids": [ FILE_ID ]
}))

编辑:

我已经创建了一个简单的CLI。https://github.com/Tim-Schwalbe/python_mattermost

票数 4
EN

Stack Overflow用户

发布于 2019-07-22 21:13:01

根据@George,您不能将文件直接发送到传入的webhook。

下面是将文件发送到通道的代码

代码语言:javascript
复制
from mattermostdriver import Driver

team_name = "<name of your team in mattermost>"
channel_name = "<channel name>"  # name of channel which you want to upload document
file_path = "<file to uploaded >"  # name of the file to upload
message = "<message to sent on channel>"


options = {
    "url": "",  # url of your mattermost acocunt https://<url>
    "port": 8065,  # port of the website
    "password": "<account password>",
    "login_id": "<login id>",
    "token": None
}

x = Driver(options=options)

# loggin into the mattermost server
x.login()

# getting team id
team_id = x.teams.get_team_by_name(team_name)['id']

# getting channel id
channel_id = x.channels.get_channel_by_name(team_id, channel_name)['id']  # give channel id

#setting up the options
form_data = {
    "channel_id": ('', channel_id),
    "client_ids": ('', "id_for_the_file"),
    "files": (file_path, open(file_path, 'rb'))
}

pp = x.files.upload_file(channel_id, form_data)
file_id = pp['file_infos'][0]['id']

# uploading the file
x.posts.create_post({'channel_id': channel_id, "message": message, "file_ids": [file_id]})

# logout from the server
x.logout()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42305599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档