我正在建立一个在线培训门户,并且已经添加了Mattermost作为学生的聊天选项(非常酷的软件)。读取Mattermost API Docs后,您似乎只能在公共频道上查询频道GUID。我还需要在专用频道上获取GUID。
根据学生注册的课程,我想将其添加到一些公共频道中,并将其添加到特定课程的私人频道中。我希望通过API来完成所有这些工作。我宁愿不必为了获取专用通道GUID而去构建数据库,然后再通过API进行其他所有操作。我缺少明显的东西吗?
我正在使用Python 3和Requests模块。这是我一直在测试的片段:
# Login and capture token
r = requests.post(url + '/api/v4/users/login', json={"login_id":"<admin_email>","password":"<password>"})
headers = {'Authorization': 'Bearer ' + r.headers['TOKEN']}
# Get the user id
r = requests.get(url + '/api/v4/users/username/' + '<username>', headers=headers)
user = r.json()
# Get the team id (portal users are only in one team)
r = requests.get(url + '/api/v4/teams', headers=headers)
team = r.json()
# Get the list of channels
r = requests.get(url + '/api/v4/teams/' + team[0]['id'] + '/channels', headers=headers)
channels = r.json()
# Add the user to channels
json = {'user_id': user['id']}
for channel in channels:
r = requests.post(url + '/api/v4/channels/' + channel['id'] + '/members', json=json, headers=headers)
以上代码段将它们添加到公共频道,但不添加到私人频道。