我正在尝试为我的小组构建一个slackbot,我尝试了示例代码和其他一些东西,但它没有向该组发送消息。
首先我尝试通过终端
export SLACK_API_TOKEN="my_token_id"
然后
from slackclient import SlackClient
import os
slack_token = os.environ["SLACK_API_TOKEN"]
sc = SlackClient(slack_token)
sc.api_call(
"chat.postMessage",
channel="#random",
text="Hello from Python! :tada:",
thread_ts="283.5127(dummy_id)",
reply_broadcast=False
)
print(sc)
#<slackclient.client.SlackClient object at 0x109b77ba8>
但松弛组中没有消息。
我尝试使用此代码:
from slackclient import SlackClient
import os
slack_token = os.environ['SLACK_API_TOKEN']
sc = SlackClient(slack_token)
print(sc.api_call("channels.list"))
重新调整:
{'error': 'invalid_auth', 'ok': False}
我没有得到我做错了,访问令牌是正确的,我想通过机器人发布一些消息,所以我如何创建一个机器人松弛和使用该机器人我可以通过python发送消息?
答案 0 :(得分:1)
当我用php&amp ;;实现一个松弛的机器人时,我遇到了类似的问题。 symfony的。 正确地创建和配置松弛的app,bot和OAuth权限并不是那么简单。
如果您需要,我在此博客文章中解释了所有这些配置:pilcrow
此外,我在PHP中的代码非常类似于解析Slack请求并发布到其API所需的代码。
摘要,TL; DR:
转到https://blog.eleven-labs.com/en/en/replace-erp-by-slack-bot-with-dialogflow-and-symfony/并点击“创建新应用”。
在此应用配置中,转到左侧菜单&#39; Bot Users&#39;或者来自&#39;基本信息&#39; &GT; &#39;添加功能和#39; &GT; &#39;机器人&#39;
仍然在此应用配置中,转到菜单&#39; OAuth&amp;权限&#39;并允许范围&#39;聊天:写:bot&#39;并复制&#39; OAuth访问令牌&#39;
从您的代码中,拨打&#39; chat.postMessage&#39;使用&#39;授权&#39;的API方法标头使用以前的标记值。
答案 1 :(得分:0)
通过网络上的一些示例构建了这个:SELECT_TO_DATE
和
liza daly - brobot : github.com
当然不是最好的实现,但它可以作为(我认为)
的适当答案import random
import time
import re
from slackclient import SlackClient
bot_id = None
slack_token = 'xoxb-no.more.mister.nice.gui'
sc = SlackClient(slack_token)
# constants
RTM_READ_DELAY = 1 # 1 second delay between reading from RTM
DEFAULT_RESPONSE = "greetings: 'hello', 'hi', 'greetings', 'sup', 'what's up' / commands: 'do'"
DEFAULT_COMMAND = "do"
MENTION_REGEX = "^<@(|[WU].+?)>(.*)"
def parse_bot_commands(slack_events):
"""
parses a list of events coming from the slack rtm api to find bot commands
:param slack_events:
:return:
"""
for event in slack_events:
if event["type"] == "message" and not "subtype" in event:
user_id, message = parse_direct_mention(event["text"])
if user_id == bot_id:
return message, event["channel"]
return None, None
def parse_direct_mention(message_text):
"""
finds direct message and returns user id
:param message_text:
:return:
"""
matches = re.search(MENTION_REGEX, message_text)
# the first group contains the user name, the second group contains
# the remaining message
return (matches.group(1), matches.group(2).strip()) if matches else (None, None)
def handle_command(command, channel):
"""
executes bot command if the command is known
:param command:
:param channel:
:return:
"""
GREETING_KEYWORDS = ("hello", "hi", "greetings", "sup", "what's up",)
GREETING_RESPONSES = ["'sup brah", "hey", "*headnod*", "didjageddathingahsencha?"]
# default response is help text for the user
default_response = "Not sure what you mean. Try *{}*.".format(DEFAULT_RESPONSE)
# finds and executes the given command, filling the response
response = None
#implement more commands below this line
if command in GREETING_KEYWORDS:
response = random.choice(GREETING_RESPONSES)
else:
if command.startswith(DEFAULT_COMMAND):
response = "Sure...write some more code and I'll do that"
# Sends the response back to the channel
sc.api_call(
"chat.postMessage",
channel="#the_danger_room",
as_user="true:",
text=response or default_response)
if __name__ == "__main__":
if sc.rtm_connect(with_team_state=False):
print("Connected and running!")
#call web api method auth.test to get bot usre id
bot_id = sc.api_call("auth.test")["user_id"]
while True:
command, channel = parse_bot_commands(sc.rtm_read())
if command:
handle_command(command, channel)
time.sleep(RTM_READ_DELAY)
else:
print("Connection failed. Exception traceback printed above.")