桔子互動|use interval in Wechaty to overcome web-wechat API limitations

Author: Seabook, @kungfu-software, Founder of Kungfu Software

Hello, first of all, thanks for inviting me to write a knowledge sharing post in Wechaty. I am just starting to learn Wechaty and immediately fall in love with it. It』s really easy to get started to make your own wechatbot. :-)

I am currently exploring all the examples from the Wechaty and try to customize it to make my own requirements happening. Everyday I try to think of a user case that I can use wechaty to make it happen.

So I had a particular evil thought in my mind one day. I wanted to add a Wechaty bot to any chatrooms and sent bot a command, then somehow the bot can start to send Friend Requests to all the people in the room.

Below is the code snippet

The code is simple, when the bot saying 希望和大家做朋友, the bots will get all the contacts from the room and start to send FriendRequest.

However, it failed quickly with some exceptions in the log sometimes, or the Send Friend Request is always False. At this stage, the Send Friend Request is pretty much useless. So I created ISSUE in github and asked around.

The original ISSUE link: github.com/Chatie/wecha

Thanks for the help from @zixia and @lijiarui, which let me understand the Limitations of web-wechat and I did some research online saying web-wechat only allows to send 100 user request per day. And the api call throttle need to be steady. Obvisouly the original For-Loop is just too fast. I am wondering is there any kinda Sleep function in java?

Thanks for the help from @zixia. Turns out there is a built in Sleep function already. Here is how to use Wechaty Sleep.

And I used same technics to finally make my Send Friend Request stable. Below is the full code. Bear in mind, I set the sleep time threshold to 2mins, which successfully send out 100 Friend Requestsbefore web-wechat shut me down.

I think it』s a common pattern when we are using Wechaty to do sth in the For-Loop block. It』s better to make it wait a bit.

Hopefully this article helps.

Thanks Seabook

【文章最初版本來自blog.chatie.io/由桔子互動(BotOrange)聯合發布】

博客地址:

blog.chatie.io/

Github地址:

github.com/chatie/wecha


推薦閱讀:

音樂VS新科技:跨越時空的混搭
用戶是否有權利不被大數據所分析?
科技成就顯著背後的冷思考 | 方承志
目前我國有實力生產、研發X86架構的CPU嗎?如果不考慮授權的情況下
獨家揭秘|Google量子人工智慧實驗室:4個月內實現「量子霸權」,量子機器學習或將提上日程

TAG:科技 | 人工智慧 |