![](/img/trans.png)
[英]How do I get a slack bot that is sending a message via Python to post @here and ping the channel?
[英]How to message several people with a slack bot without a channel?
我一直在使用python slackclient在Tutorial1和Tutorial2之后开发一个Slack机器人。 我需要创建一个可以同时与几个人聊天的机器人,但可以通过私人聊天而不是在群组渠道中进行。 我已经尝试过多次更改通道来运行bot的代码,并且它可以工作,但是我想在同一脚本中完成所有操作。 这是我的代码:
from slackclient import SlackClient
Token = 'xoxb-xxxxxxxxxxx.....'
usr= 'XXXXXXXX'
chat = 'XXXXXXXX'
sc = SlackClient(Token)
# c is just to exit the loop
c=0
# Initialize the bot and send a message
sc.api_call('chat.postMessage', as_user='true:', channel=chat, text='Bot is working, if you want me to sleep, just send the message \"end\"')
while c<1:
# Get the whole conversation
hist = sc.api_call('im.history', channel=chat)
m=hist.get('messages')
# Get the last message
last_message = m[0]
# Find who sent it
who = last_message.get('user')
# If the user sent the message, then find a keyword and reply something
if who==usr:
answer=last_message.get('text')
if 'hi' in answer:
sc.api_call('chat.postMessage', as_user='true:', channel=chat, text='Hello!:smiley: can I help you?')
elif 'no' in answer:
sc.api_call('chat.postMessage', as_user='true:', channel=chat, text='Ok, let me know if you need something')
elif 'yes' in answer:
sc.api_call('chat.postMessage', as_user='true:', channel=chat, text='What can I do for you?')
elif 'end' in answer:
c=2
与用户私下交谈的最简单方法是通过使用用户ID作为chat.postMessage
通道来发送直接消息。 然后,该消息将出现在该用户的私人slackbot
频道中。
或者,您也可以在专用的应用程序频道中为用户发送消息。 请参阅此答案以了解其工作原理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.