2018別樣跨年|今晚讓Python陪你,簡單的機器人聊天
安裝:
QQ:
pip install qqbot
微信:
pipi install itchat
1.選擇
from qqbot import _bot as bot
bot.Login(['-q','QQ號碼'])
friends= bot.List('buddy','xxx')
for friend in friends:
print (friend.name)
bot.SendTo(friend,'Hello%s~xxxxxxxx'%(friend.name))
首先我們登錄SmartQQ:
from qqbot import _bot as botbot.Login(['-q', 'XXXX'])
其中,XXXX輸入你自己的QQ號。如果第一次登錄,會跳出來一個二維碼讓你掃,掃一下即可,之後你的賬號信息會被保存下來,下次再登錄就可以直接調用了。
然後我們選擇好友:
friends = bot.List('buddy')
如果想隨機抽取一部分,比如10個, 則:
friends = random.sample(friends,10)
如果想定點發送,則:
friends = bot.List('buddy', 'XXX')
XXX裏面放好友的備註。
下面我們來給隨機(或者假裝隨機,此處應該劃重點)的好友發消息:
for friend in friends:
print(friend.name)
bot.SendTo(friend,'Hello%s~這是一條來自Python機器人的消息~恭喜你被機器人抽中成爲幸運的十個人之一~'%(friend.name))
2.微信
微信的操作非常類似,不過賬號信息只能保存一陣子,所以需要經常掃碼,如果掃碼過於頻繁,就會登錄不上了……別問我怎麼知道的,我剛想給好久沒有勾搭的女神發消息呢……就崩潰了……
首先還是登錄:
import
itchatitchat.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)
爲了防止發送過快被封,加了一個小小的暫停。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.