Skip to content

Socket Mode Client example using aiohttp doesn't work #1244

@DonDebonair

Description

@DonDebonair

I'm trying to run the following example, straight from the documentation

import asyncio
import os
from slack_sdk.web.async_client import AsyncWebClient
from slack_sdk.socket_mode.aiohttp import SocketModeClient

# Initialize SocketModeClient with an app-level token + AsyncWebClient
client = SocketModeClient(
    # This app-level token will be used only for establishing a connection
    app_token=os.environ.get("SLACK_APP_TOKEN"),  # xapp-A111-222-xyz
    # You will be using this AsyncWebClient for performing Web API calls in listeners
    web_client=AsyncWebClient(token=os.environ.get("SLACK_BOT_TOKEN"))  # xoxb-111-222-xyz
)

# Use async method
async def main():
    from slack_sdk.socket_mode.response import SocketModeResponse
    from slack_sdk.socket_mode.request import SocketModeRequest

    # Use async method
    async def process(client: SocketModeClient, req: SocketModeRequest):
        if req.type == "events_api":
            # Acknowledge the request anyway
            response = SocketModeResponse(envelope_id=req.envelope_id)
            # Don't forget having await for method calls
            await 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:
                await client.web_client.reactions_add(
                    name="eyes",
                    channel=req.payload["event"]["channel"],
                    timestamp=req.payload["event"]["ts"],
                )
	# I removed the interactive examples that were here...

    # Add a new listener to receive messages from Slack
    # You can add more listeners like this
    client.socket_mode_request_listeners.append(process)
    # Establish a WebSocket connection to the Socket Mode servers
    await client.connect()
    # Just not to stop this process
    await asyncio.sleep(float("inf"))

# You can go with other way to run it. This is just for easiness to try it out.
asyncio.run(main())

and it returns the following error:

Traceback (most recent call last):
  File "/Users/daan/code/tools/slack-machine/test.py", line 81, in <module>
    asyncio.run(main())
  File "/Users/daan/.pyenv/versions/3.10.0/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Users/daan/.pyenv/versions/3.10.0/lib/python3.10/asyncio/base_events.py", line 641, in run_until_complete
    return future.result()
  File "/Users/daan/code/tools/slack-machine/test.py", line 76, in main
    await client.connect()
  File "/Users/daan/.pyenv/versions/slack-machine/lib/python3.10/site-packages/slack_sdk/socket_mode/aiohttp/__init__.py", line 351, in connect
    self.current_session = await self.aiohttp_client_session.ws_connect(
  File "/Users/daan/.pyenv/versions/slack-machine/lib/python3.10/site-packages/aiohttp/client.py", line 776, in _ws_connect
    resp = await self.request(
  File "/Users/daan/.pyenv/versions/slack-machine/lib/python3.10/site-packages/aiohttp/client.py", line 466, in _request
    with timer:
  File "/Users/daan/.pyenv/versions/slack-machine/lib/python3.10/site-packages/aiohttp/helpers.py", line 701, in __enter__
    raise RuntimeError(
RuntimeError: Timeout context manager should be used inside a task
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x1028cd9f0>
Task was destroyed but it is pending!
task: <Task pending name='Task-1' coro=<AsyncBaseSocketModeClient.process_messages() running at /Users/daan/.pyenv/versions/slack-machine/lib/python3.10/site-packages/slack_sdk/socket_mode/async_client.py:107>>
sys:1: RuntimeWarning: coroutine 'AsyncBaseSocketModeClient.process_messages' was never awaited

using:

aiohttp = "^3.8.1"
slack-sdk = "^3.12.0"

Can you point me to what I'm doing wrong?

Metadata

Metadata

Assignees

Labels

docsM-T: Documentation work onlyquestionM-T: User needs support to use the project

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions