2018 New Year's Eve | Let Python accompany you tonight, simple robot chat

Install:
QQ:
pip install qqbot
WeChat:
cheek install itchat
1. Choose
from qqbot import _bot as bot
bot.Login(['-q','QQ number'])
friends= bot.List('buddy','xxx')
for friend in friends:
    print (friend.name)
    bot.SendTo(friend,'Hello%s~xxxxxxxx'%(friend.name))
First we log in to SmartQQ:

from qqbot import _bot as botbot.Login(['-q', 'XXXX'])

Among them, XXXX input your own QQ number. If you log in for the first time, a QR code will pop up for you to scan, just scan it, and then your account information will be saved, and you can call it directly next time you log in.
Then we choose friends:

friends = bot.List('buddy')

If you want to randomly select a part, such as 10, then:

friends = random.sample(friends,10)

If you want to send at a fixed point, then:

friends = bot.List('buddy', 'XXX')

Put friends' notes in XXX.
Let's send a message to a random (or pretend to be random , which should be highlighted here) friend:
for friend in friends:
    print(friend.name)
    bot.SendTo(friend,'Hello%s~This is a message from a Python robot~Congratulations on being selected by the robot as one of the ten lucky people~'%(friend.name))
2. WeChat
微信的操作非常类似,不过账号信息只能保存一阵子,所以需要经常扫码,如果扫码过于频繁,就会登录不上了……别问我怎么知道的,我刚想给好久没有勾搭的女神发消息呢……就崩溃了……
首先还是登录:
import itchat itchat . auto_login ( True )
True参数是指保存一阵子登录信息,如果不加则不保存。
获取好友列表和随机抽取:
friends = itchat . get_friends ()
随机抽取10位好友:
import random
friends = random.sample(friends, 10)
或者 假装随机 抽取好友:
friends = itchat.search_friends(name='XXX')
XXX 填上备注/微信号/昵称。
发送消息:

import time
for friend in friends:
    print(friend)
    itchat.send('%s你好~这是一条来自Python机器人的问候~我随机抽取了十个人发送了这条消息~'%(friend['DisplayName']), friend['UserName'])
    time.sleep(0.5)

为了防止发送过快被封,加了一个小小的暂停。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325788234&siteId=291194637