无法在balebot中使用time.sleep()发送一组消息

时间:2018-05-23 13:34:30

标签: python-3.x bots bale-messenger

我需要使用balebot python SDK向一组草捆用户发送短信。我想直接发送它们,并且不想等待它们与我的机器人开始对话。我有他们的user_id和access_hash。我已尝试过这段代码,但我遇到了一个' HIT_LIMIT'错误。

  

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

我使用for循环再次使用time.sleep()发送它们但是当我使用此sendMessage函数时无效!

代码

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)

1 个答案:

答案 0 :(得分:3)

这是因为asyncio和time.sleep()不匹配。如果我是你,我会使用asyncio.get_event_loop()而不是for循环和time.sleep()。 这是一个示例代码:

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()