1

I'm trying to send about 70 requests to slack api but can't find a way to implement it in asynchronous way, I have about 3 second for it or I'm getting timeout error

here how I've tried to t implement it:

import asyncio

def send_msg_to_all(sc,request,msg):
    user_list = sc.api_call(
       "users.list"
    )
members_array = user_list["members"]

ids_array = []
for member in members_array:
    ids_array.append(member['id'])
real_users = []

for user_id in ids_array:

    user_channel = sc.api_call(
        "im.open",
        user=user_id,
    )
    if user_channel['ok'] == True:
        real_users.append(User(user_id, user_channel['channel']['id']) )

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(send_msg(sc, real_users, request, msg))
loop.close()
return HttpResponse()


async def send_msg(sc, real_users, req, msg):
    for user in real_users:    
        send_ephemeral_msg(sc,user.user_id,user.dm_channel, msg)


def send_ephemeral_msg(sc, user, channel, text):
    sc.api_call(
        "chat.postEphemeral",
        channel=channel,
        user=user,
        text=text
    )  

But it looks like I'm still doing it in a synchronous way

Any ideas guys?

  • Your code has syntax error, lines below `send_msg_to_all` function declaration have no indentations. Please fix your code first. – szamani20 Oct 27 '17 at 16:18

1 Answers1

0

Slack's API has a rate limit of 1 query per second (QPS) as documented here.

Even if you get this working you'll be well exceeding the limits and you will start to see HTTP 429 Too Many Requests errors. Your API token may even get revoked / cancelled if you continue at that rate.

I think you'll need to find a different way.

Joel
  • 724
  • 5
  • 12