我有一个通过slackclient lib使用Slack API的python应用程序。我在docker映像上工作正常。但是后来我创建了一个Kubernetes部署,并在Minikube上声明了该应用程序。它只是无法连接到互联网以获取Slack消息。
我不需要使用任何K8S服务,因为我不需要公开服务中的任何内容,只需致电已经在互联网上公开的Slack服务即可。
启用该连接我需要做什么?
更新1(已给出例外):
10/03/2019 19:44:50 Starting new HTTPS connection (1): slack.com:443
10/03/2019 19:44:51 https://slack.com:443 "POST /api/rtm.start HTTP/1.1" 200 8581
Traceback (most recent call last):
File "app.py", line 162, in <module>
request, channel, ts, user = parse_slack_output(slack_client.rtm_read())
File "/usr/local/lib/python3.6/site-packages/slackclient/client.py", line 128, in rtm_read
json_data = self.server.websocket_safe_read()
File "/usr/local/lib/python3.6/site-packages/slackclient/server.py", line 186, in websocket_safe_read
data += "{0}\n".format(self.websocket.recv())
File "/usr/local/lib/python3.6/site-packages/websocket/_core.py", line 313, in recv
opcode, data = self.recv_data()
File "/usr/local/lib/python3.6/site-packages/websocket/_core.py", line 330, in recv_data
opcode, frame = self.recv_data_frame(control_frame)
File "/usr/local/lib/python3.6/site-packages/websocket/_core.py", line 343, in recv_data_frame
frame = self.recv_frame()
File "/usr/local/lib/python3.6/site-packages/websocket/_core.py", line 377, in recv_frame
return self.frame_buffer.recv_frame()
File "/usr/local/lib/python3.6/site-packages/websocket/_abnf.py", line 361, in recv_frame
self.recv_header()
File "/usr/local/lib/python3.6/site-packages/websocket/_abnf.py", line 309, in recv_header
header = self.recv_strict(2)
File "/usr/local/lib/python3.6/site-packages/websocket/_abnf.py", line 396, in recv_strict
bytes_ = self.recv(min(16384, shortage))
File "/usr/local/lib/python3.6/site-packages/websocket/_core.py", line 452, in _recv
return recv(self.sock, bufsize)
File "/usr/local/lib/python3.6/site-packages/websocket/_socket.py", line 112, in recv
"Connection is already closed.")
websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.
在app.py中调用函数slack_client.rtm_read()时出现问题
SLACK_BOT_TOKEN = environ.get('SLACK_BOT_TOKEN')
slack_client = SlackClient(SLACK_BOT_TOKEN)
def parse_slack_output(event_list):
"""
The Slack Real Time Messaging API is an events firehose.
this parsing function returns None unless a message is
directed at the Bot, based on its ID.
"""
if len(event_list) > 0:
for event in event_list:
if is_for_me(event):
return event['text'], \
event['channel'], \
event['ts'], \
event['user']
return None, None, None, None
if __name__ == '__main__':
READ_WEBSOCKET_DELAY = 1 # 1 second delay between reading from firehose
if slack_client.rtm_connect():
while True:
request, channel, ts, user = parse_slack_output(slack_client.rtm_read())
答案 0 :(得分:0)
请求该API时遇到的错误或异常是什么? 这是DNS解析问题吗?
如果是DNS解析问题。 然后检查您的kubernetes集群上是否正在运行KubeDNS或CoreDNS。 如果上面指定的命令正在运行,则尝试从您的Pod ping主机。
编辑1:
从服务器读取时,套接字连接似乎已关闭。 尝试在调用rtm_connect函数时传递auto_reconnect参数。
slack_client.rtm_connect(auto_reconnect = True)
答案 1 :(得分:0)
我创建了一个开源项目,其中对所有使用Slackclient在Minikube中运行的Slack机器人进行了调整: https://github.com/staticdev/k8s-python-slackbot