GreatReza
GreatReza

Reputation: 45

Can't send a set of messages with time.sleep() in balebot

I need to send a text message to a set of bale users with balebot python SDK. I want to send them directly and don't want to wait for them to start a conversation with my bot. I have their user_id and access_hash. I've tried this code but I've faced a 'HIT_LIMIT' error.

BotErrorResponse { _id: '1001', _body: { code: 403, tag: 'HIT_LIMIT', data: {}, retryIn: null }, code: 403, tag: 'HIT_LIMIT', data: {}, retryIn: null }

I used a for loop to send them again with time.sleep() but when I use this sendMessage function is not working!

Code

def success(response, user_data):
print("success : ", response)
print(user_data)


def failure(response, user_data):
print("failure : ", response)
print(user_data)


def send_message(bot, update):
for peer in save_peers:
    bot.send_message(TextMessage("Text"), Peer(peer_type=PeerType.user, peer_id=peer["id"],
    access_hash=peer["accessHash"]),
    success_callback=success, failure_callback=failure)
    time.sleep(2)

Upvotes: 2

Views: 47

Answers (1)

Ehsan
Ehsan

Reputation: 4291

It's because asyncio and time.sleep() doesn't match with together.If i were you i would use asyncio.get_event_loop() instead of for loop and time.sleep(). here is a sample code :

save_peers = [{"$type": "User", "id": 128521332, "accessHash": "9256310829016723727"},
              {"$type": "User", "id": 928571821, "accessHash": "4540300829906797423"}
              ]
limit_message = 50000
bot_send_delay = 2
my_send_loop = asyncio.get_event_loop()
my_send_loop2 = asyncio.get_event_loop()


def send_message(bot, how_many, message_number, loop):
    print("sending for ", message_number, " st time")

    my_send_loop2.call_soon(send_message2, bot, len(save_peers), 0, my_send_loop2)

    if message_number < how_many:
        loop.call_later(bot_send_delay, send_message, bot, how_many, message_number + 1, loop)
    else:
        print("index finished")

def send_message2(bot, top, down, loop):
    print("sending for ", down, " st time")
    if down < top:
        bot.send_message(TextMessage("TEXT"), Peer(peer_type=PeerType.user, peer_id=save_peers[down]["id"],
                                                   access_hash=save_peers[down]["accessHash"]),
                         success_callback=success, failure_callback=failure)
        loop.call_later(bot_send_delay, send_message2, bot, top, down + 1, loop)


my_send_loop.call_soon(send_message, bot, limit_message, 0, my_send_loop)

updater.run()

Upvotes: 4

Related Questions