在 slack API 上发送我的第一条消息
Sending my first message on the slack API
我注册了我的第一个应用程序,它看起来像这样:
屏幕截图下方填充了所有字段。
现在,我有一些基本的 python 代码,使用 their repo 上的示例。
我创建了以下测试脚本:
import traceback
app_id = 'FAKE.VALUE'
client_id = 'FAKE.VALUE'
client_secret = 'FAKE.VALUE'
signin_secret = 'FAKE.VALUE'
verification_token = 'FAKE.VALUE'
items = locals()
import os
import slack
items = locals().copy()
for k in items:
if '__' not in k:
val = items[k]
try:
client = slack.WebClient(token=val)
response = client.chat_postMessage(
channel='CE476K9HT',
text='Hello-----' + str(val))
print(response)
except:
print(k)
traceback.print_exc()
print('-'*50)
但我得到的所有回复都是:
The server responses with: {'ok':False,'error':'invalid_auth'}
出于某种原因,是否有必要使用路径变量?
我不清楚这里需要什么类型的授权。
按照 Erik 的建议进行操作后,
我有一个 xoxp
代码并将重定向 url 注册到 http://localhost
。
并添加了以下范围:
并将我的代码更新为:
oauth_token ='xoxp-*****************'
import os
import slack
items = locals().copy()
client = slack.WebClient(token=oauth_token)
response = client.chat_postMessage(
channel='my_channel_id',
text='Hello-----')
我的频道 ID 来自 url:
https://app.slack.com/client/FOO/my_channel_id
当我 运行 我的代码时,我得到以下结果:
Traceback (most recent call last):
File "/home/usr/git/slack_messaging/slack_messaging.py", line 20, in <module>
text='Hello-----')
File "/home/usr/git/python-slackclient/slack/web/client.py", line 382, in chat_postMessage
return self.api_call("chat.postMessage", json=kwargs)
File "/home/usr/git/python-slackclient/slack/web/base_client.py", line 172, in api_call
return self._event_loop.run_until_complete(future)
File "/home/usr/anaconda2/envs/beer/lib/python3.7/asyncio/base_events.py", line 573, in run_until_complete
return future.result()
File "/home/usr/git/python-slackclient/slack/web/base_client.py", line 241, in _send
return SlackResponse(**{**data, **res}).validate()
File "/home/usr/git/python-slackclient/slack/web/slack_response.py", line 176, in validate
raise e.SlackApiError(message=msg, response=self)
slack.errors.SlackApiError: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'missing_scope', 'needed': 'chat:write:user', 'provided': 'admin,identify'}
进程已完成,退出代码为 1
要使脚本正常工作,您需要做两件事。
1。 OAuth 令牌
您需要一个有效的 OAuth 令牌并在初始化 Slack 客户端时提供它:
client = slack.WebClient(token="xoxb-xxx")
要获得令牌,您需要将 Slack 应用程序安装到工作区。您可以在 "Install App" 下的应用程序管理页面上执行此操作。安装后,您的 OAuth 令牌也会显示在该页面上。
2。权限
您的 Oauth 令牌/Slack 应用程序需要获得 post 消息的权限。在应用程序管理页面上,转到 "OAuth & permission" 并为您的应用程序添加所需的权限:。例如chat:write:user 用于用户令牌。
请注意,每次添加权限都需要重新安装您的应用。
我注册了我的第一个应用程序,它看起来像这样:
屏幕截图下方填充了所有字段。
现在,我有一些基本的 python 代码,使用 their repo 上的示例。
我创建了以下测试脚本:
import traceback
app_id = 'FAKE.VALUE'
client_id = 'FAKE.VALUE'
client_secret = 'FAKE.VALUE'
signin_secret = 'FAKE.VALUE'
verification_token = 'FAKE.VALUE'
items = locals()
import os
import slack
items = locals().copy()
for k in items:
if '__' not in k:
val = items[k]
try:
client = slack.WebClient(token=val)
response = client.chat_postMessage(
channel='CE476K9HT',
text='Hello-----' + str(val))
print(response)
except:
print(k)
traceback.print_exc()
print('-'*50)
但我得到的所有回复都是:
The server responses with: {'ok':False,'error':'invalid_auth'}
出于某种原因,是否有必要使用路径变量?
我不清楚这里需要什么类型的授权。
按照 Erik 的建议进行操作后,
我有一个 xoxp
代码并将重定向 url 注册到 http://localhost
。
并添加了以下范围:
并将我的代码更新为:
oauth_token ='xoxp-*****************'
import os
import slack
items = locals().copy()
client = slack.WebClient(token=oauth_token)
response = client.chat_postMessage(
channel='my_channel_id',
text='Hello-----')
我的频道 ID 来自 url:
https://app.slack.com/client/FOO/my_channel_id
当我 运行 我的代码时,我得到以下结果:
Traceback (most recent call last):
File "/home/usr/git/slack_messaging/slack_messaging.py", line 20, in <module>
text='Hello-----')
File "/home/usr/git/python-slackclient/slack/web/client.py", line 382, in chat_postMessage
return self.api_call("chat.postMessage", json=kwargs)
File "/home/usr/git/python-slackclient/slack/web/base_client.py", line 172, in api_call
return self._event_loop.run_until_complete(future)
File "/home/usr/anaconda2/envs/beer/lib/python3.7/asyncio/base_events.py", line 573, in run_until_complete
return future.result()
File "/home/usr/git/python-slackclient/slack/web/base_client.py", line 241, in _send
return SlackResponse(**{**data, **res}).validate()
File "/home/usr/git/python-slackclient/slack/web/slack_response.py", line 176, in validate
raise e.SlackApiError(message=msg, response=self)
slack.errors.SlackApiError: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'missing_scope', 'needed': 'chat:write:user', 'provided': 'admin,identify'}
进程已完成,退出代码为 1
要使脚本正常工作,您需要做两件事。
1。 OAuth 令牌
您需要一个有效的 OAuth 令牌并在初始化 Slack 客户端时提供它:
client = slack.WebClient(token="xoxb-xxx")
要获得令牌,您需要将 Slack 应用程序安装到工作区。您可以在 "Install App" 下的应用程序管理页面上执行此操作。安装后,您的 OAuth 令牌也会显示在该页面上。
2。权限
您的 Oauth 令牌/Slack 应用程序需要获得 post 消息的权限。在应用程序管理页面上,转到 "OAuth & permission" 并为您的应用程序添加所需的权限:。例如chat:write:user 用于用户令牌。
请注意,每次添加权限都需要重新安装您的应用。