async def send_async_response( client: AsyncBaseSocketModeClient, req: SocketModeRequest, bolt_resp: BoltResponse, start_time: float, ): if bolt_resp.status == 200: content_type = bolt_resp.headers.get("content-type", [""])[0] if bolt_resp.body is None or len(bolt_resp.body) == 0: await client.send_socket_mode_response( SocketModeResponse(envelope_id=req.envelope_id) ) elif content_type.startswith("application/json"): dict_body = json.loads(bolt_resp.body) await client.send_socket_mode_response( SocketModeResponse(envelope_id=req.envelope_id, payload=dict_body) ) else: await client.send_socket_mode_response( SocketModeResponse( envelope_id=req.envelope_id, payload={"text": bolt_resp.body}, ) ) if client.logger.level <= logging.DEBUG: spent_time = int((time() - start_time) * 1000) client.logger.debug(f"Response time: {spent_time} milliseconds") else: client.logger.info( f"Unsuccessful Bolt execution result (status: {bolt_resp.status}, body: {bolt_resp.body})" )
def test_to_dict(self): response = SocketModeResponse(envelope_id="xxx", payload={"text": "hi"}) self.assertDictEqual(response.to_dict(), { "envelope_id": "xxx", "payload": { "text": "hi" } })
async def process(client: SocketModeClient, req: SocketModeRequest): db_session = SessionLocal() background_tasks = BackgroundTasks() if req.type == "events_api": response = await handle_slack_event( db_session=db_session, client=client.web_client, request=req.payload, background_tasks=background_tasks, ) if req.type == "slash_commands": response = await handle_slack_command( db_session=db_session, client=client.web_client, request=req.payload, background_tasks=background_tasks, ) if req.type == "interactive": response = await handle_slack_action( db_session=db_session, client=client.web_client, request=req.payload, background_tasks=background_tasks, ) response = SocketModeResponse(envelope_id=req.envelope_id, payload=response) await client.send_socket_mode_response(response) # run the background tasks await background_tasks()
def process_event(config, socket_client, req): '''Handler for a Slack event.''' payload = DottedDict(req.payload) if req.type == 'slash_commands': # Don't even acknowledge events in forbidden channels, to avoid # interfering with separate bot instances in other channels. if payload.channel_id in config.get('channels_deny', []): return if config.get('channels_allow' ) and payload.channel_id not in config.channels_allow: return # Acknowledge the event, as required by Slack. resp = SocketModeResponse(envelope_id=req.envelope_id) socket_client.send_socket_mode_response(resp) # Idempotency with Database(config) as db: if not db.add_event(payload.channel_id, payload.trigger_id): # When we ignore some events, Slack can send us duplicate # retries. Detect and ignore those after acknowledging. return # Process it CommandHandler(config, payload)() else: raise Fail(f'Unexpected event type "{req.type}"')
def test_parser(self): text = """{"envelope_id":"1d3c79ab-0ffb-41f3-a080-d19e85f53649","payload":{"text":"Thanks!"}}""" body = json.loads(text) response = SocketModeResponse(body["envelope_id"], body["payload"]) self.assertIsNotNone(response) self.assertEqual(response.envelope_id, "1d3c79ab-0ffb-41f3-a080-d19e85f53649") self.assertEqual(response.payload.get("text"), "Thanks!")
def process(client: SocketModeClient, req: SocketModeRequest): if req.type == "events_api": response = SocketModeResponse(envelope_id=req.envelope_id) client.send_socket_mode_response(response) client.web_client.reactions_add( name="eyes", channel=req.payload["event"]["channel"], timestamp=req.payload["event"]["ts"], )
async def socket_event_handler(self, client: SocketModeClient, req: SocketModeRequest): payload = {} response = SocketModeResponse(envelope_id=req.envelope_id) await client.send_socket_mode_response(response) payload = req.payload await self.event_handler(payload)
def _process_event(self, client: SocketModeClient, req: SocketModeRequest): if req.type == "events_api": # Acknowledge the request anyway response = SocketModeResponse(envelope_id=req.envelope_id) client.send_socket_mode_response(response) # Add a reaction to the message if it's a new message message = req.payload["event"] if message["type"] == "message" and message.get("subtype") is None: self.message_queue.put(message)
def process(self, client: SocketModeClient, req: SocketModeRequest): if req.type != "events_api": return # Acknowledge the request anyway response = SocketModeResponse(envelope_id=req.envelope_id) client.send_socket_mode_response(response) if (req.payload["event"]["type"] == "message" or req.payload["event"]["type"] == "app_mention" ) and req.payload["event"].get("subtype") is None: with self.app.app_context(): bot.run_action(req.payload, req.payload["event"]["channel"])
def process(client: SocketModeClient, req: SocketModeRequest): if req.type == "events_api": # Acknowledge the request anyway response = SocketModeResponse(envelope_id=req.envelope_id) client.send_socket_mode_response(response) # Add a reaction to the message if it's a new message if req.payload["event"]["type"] == "message" \ and req.payload["event"].get("subtype") is None: client.web_client.reactions_add( name="eyes", channel=req.payload["event"]["channel"], timestamp=req.payload["event"]["ts"], )
def on_socket_mode_request(self, client: SocketModeClient, req: SocketModeRequest): if req.type == "events_api": # Acknowledge the request anyway response = SocketModeResponse(envelope_id=req.envelope_id) client.send_socket_mode_response(response) if req.payload['event']['type'] == 'open': self.on_open(req.payload['event']) elif req.payload['event']['type'] == 'message': self.on_message(req.payload['event']) elif req.payload['event']['type'] == 'channel_joined': self.on_channel_joined(req.payload['event']) elif req.payload['event']['type'] == 'channel_left': self.on_channel_left(req.payload['event']) elif req.payload['event']['type'] == 'member_joined_channel': self.on_member_joined_channel(req.payload['event']) elif req.payload['event']['type'] == 'member_left_channel': self.on_member_left_channel(req.payload['event'])
async def process(client: SocketModeClient, req: SocketModeRequest): background_tasks = BackgroundTasks() if req.type == "events_api": response = await handle_slack_event( config=config, client=client.web_client, event=EventEnvelope(**req.payload), background_tasks=background_tasks, ) if req.type == "slash_commands": response = await handle_slack_command( config=config, client=client.web_client, request=req.payload, background_tasks=background_tasks, ) if req.type == "interactive": if req.payload["type"] == "block_suggestion": response = await handle_slack_menu( config=config, client=client.web_client, request=req.payload, ) else: response = await handle_slack_action( config=config, client=client.web_client, request=req.payload, background_tasks=background_tasks, ) response = SocketModeResponse(envelope_id=req.envelope_id, payload=response) await client.send_socket_mode_response(response) # run the background tasks await background_tasks()
def ack_event(): '''Acknowledge the event, as required by Slack.''' resp = SocketModeResponse(envelope_id=req.envelope_id) socket_client.send_socket_mode_response(resp)