gpt4 book ai didi

python - 如何通过 Mattermost 传入 webhook 发送文件?

转载 作者:行者123 更新时间:2023-11-28 21:06:48 30 4
gpt4 key购买 nike

我可以发送文本到 Mattermost channel 通过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))

此代码只是发布文本。我找不到将文件发布到 channel 的方法。假设我想发布位于/home/alok/Downloads/Screenshot_20170217_221447.png 的文件。如果有人知道请分享。

最佳答案

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

这是一个如何实现该目标的示例(使用 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',
}))

关于python - 如何通过 Mattermost 传入 webhook 发送文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42305599/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com