我正在尝试使用python SlackClient打印消息发布的信道。运行此代码后,我只获得一个ID,而不是通道名称。
import time
import os
from slackclient import SlackClient
BOT_TOKEN = os.environ.get('SLACK_BOT_TOKEN')
def main():
# Creates a slackclient instance with bots token
sc = SlackClient(BOT_TOKEN)
#Connect to slack
if sc.rtm_connect():
print "connected"
while True:
# Read latest messages
for slack_message in sc.rtm_read():
message = slack_message.get("text")
print message
channel = slack_message.get("channel")
print channels
time.sleep(1)
if __name__ == '__main__':
main()
这是输出:
test
U1K78788H
答案 0 :(得分:1)
这将始终生成频道ID而非频道名称。您必须拨打 channels.info 才能获取频道名称。
import time
import os
from slackclient import SlackClient
BOT_TOKEN = os.environ.get('SLACK_BOT_TOKEN')
def main():
# Creates a slackclient instance with bots token
sc = SlackClient(BOT_TOKEN)
#Connect to slack
if sc.rtm_connect():
print "connected"
while True:
# Read latest messages
for slack_message in sc.rtm_read():
message = slack_message.get("text")
print message
channel = slack_message.get("channel")
print channel
channel_info=sc.api_call("channels.info",channel=channel)
print channel_info["channel"]["name"]
time.sleep(1)
if __name__ == '__main__':
main()
这也将打印频道名称。 另一种方法是您可以预先将所有通道的名称及其channel_id存储在字典中。然后获取id为key的频道名称。
答案 1 :(得分:0)
我不确定你输出的是什么。不应该"#34;频道" be"频道" ?另外,我认为这个输出是"用户"领域。 "频道"字段应该产生以C或G开头的id(doc)。
@Override
public void configure(WebSecurity web) throws Exception {
web.expressionHandler(new DefaultWebSecurityExpressionHandler() {
@Override
protected SecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication, FilterInvocation fi) {
WebSecurityExpressionRoot root = (WebSecurityExpressionRoot) super.createSecurityExpressionRoot(authentication, fi);
root.setDefaultRolePrefix(""); //remove the prefix ROLE_
return root;
}
});
}
然后,使用python客户端检索通道名称,如果它存储它(我不知道Python客户端),或使用Web API方法channels.info来检索通道名称。