更新:20170425 15:10

微信自帶的【群發功能 】不能檢測好友是否刪除了您,群發的訊息不會出現在聊天記錄中,這麼看來這個程式還是有一點作用。。。。

——————————————————————————————-

前言

itchat 是一個python 擴充套件模組,封裝了網頁版微信並開放了介面給我們使用

github: littlecodersh/ItChat 作者 littlecoder

文件:專案簡介 - itchat

推酷介紹:Python微信庫:itchat - 推酷

需要具備的知識:

python基本語法。。

稍微涉及了點多執行緒

我的程式碼

https://

github。com/RenjiaLu9527

/itchatserver/blob/master/wechatrobot_sendmsgtoall。py

(另一個工程,也基於 itchat實現的群聊助手

https://

github。com/RenjiaLu9527

/itchatserver/tree/master/wechatRobot

正文

寫在前面:

這個群發助手的主要功能就是 檢測好友是否刪除了您、群發祝福;當然,微信自帶了這一群發功能【設定-通用-功能-群發助手】;這助手簡直就是一個

雞肋程式

,我為什麼寫這個,因為我看到了有人用這個來‘賺外快’,我身邊居然還真的有人使用,於是我就去試了一下

我的好友某天發給我的

itchat-python 微信封裝包-實現群發檢測好友功能

itchat-python 微信封裝包-實現群發檢測好友功能

點開它的連結

itchat-python 微信封裝包-實現群發檢測好友功能

itchat-python 微信封裝包-實現群發檢測好友功能

然後懷著好奇試了下

itchat-python 微信封裝包-實現群發檢測好友功能

itchat-python 微信封裝包-實現群發檢測好友功能

然後大概明白了他是怎麼做的,

首先按照提示給機器人【傳送好友數量】,然後【轉賬】,接著機器人會發給你一個二維碼讓你登陸,必須使用一個裝置掃另一個裝置才行;登陸後就立刻開始了群發操作

其實這個二維碼就是微信pc版登陸二維碼,這個助手讓你掃碼就是在它的子執行緒中遠端登陸你的微信(所以要注意隱私安全,雖然pc版微信沒有轉賬功能),你的好友群聊都暴露給這個機器人

itchat-python 微信封裝包-實現群發檢測好友功能

itchat-python 微信封裝包-實現群發檢測好友功能

其中啟動子執行緒後的操作就是檢測好友的同時不忘推廣自己的資訊,操作麻煩,最重要的是還收費!於是我也用這個思路簡單弄了個,順便學習學習這個itchat的使用

定義了兩個類:

在 run(self) 函式中,使用者登入完成後直接啟動【遍歷好友群發訊息】的操作,其實這個實現方式不好,判斷好友是否刪除了你的方式據我所知,有兩個實現方法

【群發訊息】和【拉好友進群聊】

第二種方法比較友好不打擾別人,但是參考作者littlecodershde 文章

https://

gist。github。com/littlec

odersh/3fef7d2afb2d502e4735be083c9f79e1

微信限制了頻率,這就尷尬了,目前沒有想到解決辦法

qrCallBack 函式是 self。newInstance。auto_login()中的回掉函式,即生成登陸二維碼後呼叫這個函式,這個函式再操作 mainInstance 主執行緒傳送二維碼給【剛剛確認下單的使用者】

在使用全域性變數 mainInstance 的時候需要加鎖訪問避免訪問混亂,這是執行緒同步的問題

mutex = threading。Lock()

if mutex。acquire():

#操作共享變數

mutex。release()

#客戶執行緒類

class itchat_client (threading。Thread):

#初始化函式

def __init__(self, threadID, name,UserNameValue,friendsnum=100):

threading。Thread。__init__(self)

self。threadID = threadID

self。name = name

self。UserNameValue = UserNameValue

self。friendsnum = friendsnum

self。picDir = ‘qr/%s。png’%(UserNameValue。replace(‘@’,‘qr’)[0:7])

self。step = 0 #0-初始化 1-完成 向客戶傳送二維碼

self。newInstance = itchat。new_instance()

#qrCallBack 函式

def qrsendtouser(self,uuid, status, qrcode):

global mainInstance

strtm = time。strftime(‘%Y_%m_%d_%H_%M_%S’,time。localtime(time。time()))

print ‘qrsendtouser’

with open(self。picDir, ‘wb’) as f:

f。write(qrcode)

print ‘write success’

if self。step < 1:

if mutex。acquire():

#print “Starting ” + self。name

print ‘c send QR G_UserNameValue=’,self。UserNameValue

mainInstance。send(‘@img@%s’ %(self。picDir),self。UserNameValue)

mainInstance。send(‘記得是一個手機掃另一個手機才有效哦~’,self。UserNameValue)

print ‘c send QR end’

self。step = 1

mutex。release()

else:

print ‘mutex error’

print ‘send success’

#run 函式

def run(self):

global mainInstance

print ‘’

print ‘Welcome using [ wechatRobot ] version: beta1。2 -only for learning-’

print ‘[Announce]: If you learn more about this program, search \’itchat\‘ on Github or contact me by wheee9527@163。com’

print ‘’

try:

self。newInstance。auto_login(picDir=self。picDir,qrCallback=self。qrsendtouser)

list_dict_friends = self。newInstance。get_friends()

# print type(list_dict_friends),list_dict_friends[0]

# print list_dict_friends[0][‘UserName’]

# print ‘’

friendsnum = len(list_dict_friends)

i=0

for x in xrange(0,friendsnum):

NickName = list_dict_friends[x][‘NickName’]

UserNameValue = list_dict_friends[x][‘UserName’]

self。newInstance。send(‘@img@qr/turing。png’,UserNameValue)

time。sleep(0。2)

print x,‘OK’

i+=1

# @self。newInstance。msg_register([TEXT, MAP, CARD, NOTE, SHARING])

# def text_reply(msg):

# self。newInstance。send(‘%s: %s’ % (msg[‘Type’], msg[‘Text’]), msg[‘FromUserName’])

#self。newInstance。run()

if mutex。acquire():

print ‘c thread end G_UserNameValue=’,self。UserNameValue

mainInstance。send(‘完成!共檢測【%d】個好友’%i,self。UserNameValue)

print ‘c thread end ’

self。step = 1

mutex。release()

else:

print ‘thread end mutex error’

except Exception as e:

myException(“STEP OF startdelete mainprocess”,“startdelete”,e)

else:

pass

print ‘thread end’

# 沒有下單的客戶資訊 Customer類

class Customer(object):

“”“docstring for customer”“”

def __init__(self,nickname,namevalue,friendsnum,step):

global cnt

self。id = cnt

self。nickname = nickname

self。namevalue = namevalue

self。friendsnum = friendsnum

self。step = step

self。shouldpay = 1。0

cnt += 1

兩個簡單的類,然後簡單設定一下關鍵字回覆

# 回覆文字資訊 指導使用者下單

@mainInstance。msg_register([TEXT])

def text_reply(msg):

global mainInstance #全域性變數

print ‘msg_register(TEXT) ’,msg[‘Text’]

replystr = getreply(msg[‘Text’],msg[‘FromUserName’])

mainInstance。send(replystr,msg[‘FromUserName’])

getreply函式返回對應的訊息

讀取配置檔案

# config。conf

# 分隔符

[splitstr]

splitstr=_

parameter=[parameter]

# 幫助資訊

[helpinfo]

main= 親!請稍等哦,小薇機器人正在努力 【注意事項】: 0、【不能長按識別 必須手機掃另一臺手機哦】1、等會發個二維碼給你【三分鐘失效】2、把二維碼用第二臺手機拍下來。3、在用你自己的微信掃拍下來的二維碼。4、點選登入,等待1分鐘就開始檢測了。 確定具備操作條件請回復:1[調皮]】

# 步驟提示語

[step]

# 第零步 打招呼

0= 在的親 [呲牙] 本機器人可以幫您測試您的好友有沒有刪除您 需要服務嗎?

# 第一步 詢問好友人數

1=【測試您的好友有沒有刪除您】請問您的好友數量多少呢?回覆數字 [呲牙]

# 第二步 傳送收費資訊

2=您需要測試的好友數為 【[parameter]】 人,向我轉賬 【[parameter]】 元立刻開始測試[愛心]

# 第三步 等待付款

3=等待付款中哦[呲牙] 轉賬後立即開始

# 第四步 測試中

4=測試中,請稍等[呲牙]

init()函式獲取配置文字,記得匯入包 ConfigParser

# 初始化

def

init

():

global

helpinfo

dict_stepinfo

cp

=

ConfigParser

SafeConfigParser

()

cp

read

‘config。conf’

helpinfo

=

cp

get

‘helpinfo’

‘main’

dict_stepinfo

0

=

cp

get

‘step’

‘0’

# step 節點下的 0 對應的文字

dict_stepinfo

1

=

cp

get

‘step’

‘1’

dict_stepinfo

2

=

cp

get

‘step’

‘2’

dict_stepinfo

3

=

cp

get

‘step’

‘3’

dict_stepinfo

4

=

cp

get

‘step’

‘4’

研究了原始碼之後發現不能構造一條 [Sharing]的訊息,還待研究,現在基本實現了和它一樣的功能,不過我用的是微信網頁版,它使用的是客戶端

已經部署到雲伺服器,測試好友的機器人

二維碼在標題封面

aHR0cDovL3dlaXhpbi5xcS5jb20vci82ZlNSaVdmRXBEcmJyU1BxOTZINw== (二維碼自動識別)

結束