WechatPCAPI庫如何實現(xiàn)自動化回復(fù)

小編給大家分享一下WechatPCAPI庫如何實現(xiàn)自動化回復(fù),希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、興業(yè)ssl等。為上1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的興業(yè)網(wǎng)站制作公司

目前有一個項目 WechatPCAPI 可以對微信進行操作,簡單來說它是直接操作 PC 版微信客戶端的,當然它有一定不足之處就是:PC 版微信客戶端和 Python 都需要使用指定版本的,本文我們使用的 Python 版本為 3.7.6 ,微信客戶端使用版本為 2.6.8.52 ,WechatPCAPI 的 GitHub 地址為: https://github.com/Mocha-L/WechatPCAPI。

獲取好友列表

WechatPCAPI 提供了方法 get_friends(),該方法返回信息包括:好友、群和公眾號的列表信息,信息內(nèi)容主要包括:微信號、昵稱和自己設(shè)置的備注。

我們使用獲取的昵稱做個簡單的詞云展示,代碼實現(xiàn)如下所示:

logging.basicConfig(level=logging.INFO)

def on_message(message):
 pass

def get_friends():
 # 初始化微信實例
 wx_inst = WechatPCAPI(on_message=on_message, log=logging)
 # 啟動微信
 wx_inst.start_wechat(block=True)
 # 等待登陸成功,此時需要人為掃碼登錄微信
 while not wx_inst.get_myself():
  time.sleep(5)
 print('登陸成功')
 nicknames = []
 # 排除的詞
 remove = ['還是', '不會', '一些', '所以', '果然',
     '起來', '東西', '為什么', '真的', '這么',
     '但是', '怎么', '還是', '時候', '一個',
     '什么', '自己', '一切', '樣子', '一樣',
     '沒有', '不是', '一種', '這個', '為了'
   ]
 for key, value in wx_inst.get_friends().items():
  if key in ['fmessage', 'floatbottle', 'filehelper'] or 'chatroom' in key:
   continue
  nicknames.append(value['wx_nickname'])
 words = []
 for text in nicknames:
  if not text:
   continue
  for t in jieba.cut(text):
   if t in remove:
    continue
   words.append(t)
 global word_cloud
 # 用逗號隔開詞語
 word_cloud = ','.join(words)

def nk_cloud():
    # 打開詞云背景圖
    cloud_mask = np.array(Image.open('bg.png'))
    # 定義詞云的一些屬性
    wc = WordCloud(
        # 背景圖分割顏色為白色
        background_color='white',
        # 背景圖樣
        mask=cloud_mask,
        # 顯示最大詞數(shù)
        max_words=300,
        # 顯示中文
        font_path='./fonts/simkai.ttf',
        # 最大尺寸
        max_font_size=70
    )
    global word_cloud
    # 詞云函數(shù)
    x = wc.generate(word_cloud)
    # 生成詞云圖片
    image = x.to_image()
    # 展示詞云圖片
    image.show()
    # 保存詞云圖片
    wc.to_file('nk.png')

看一下效果:

消息防撤回

我們在使用微信和好友聊天時,對方有時會有撤回消息的情況,正常情況下,我們是不知道好友撤回的消息是什么的,通過 WechatPCAPI 就可以實現(xiàn)消息防撤回的功能。

我們知道通常撤回的消息是點擊撤回操作前一步發(fā)送的內(nèi)容,當然也可能撤回的是前兩步、三步 ... 的消息,這里我們只對撤回前一步的消息做處理,基本思路是:我們將撤回前一步發(fā)送的消息存一下,當對方點擊撤回操作時,我們再將前一步的消息再次返回給自己。

下面看一下實現(xiàn)代碼:

logging.basicConfig(level=logging.INFO)
queue_recved_event = Queue()

def on_message(msg):
    queue_recved_event.put(msg)

def login():
    pre_msg = ''
    # 初始化微信實例
    wx_inst = WechatPCAPI(on_message=on_message, log=logging)
    # 啟動微信
    wx_inst.start_wechat(block=True)
    # 等待登陸成功,此時需要人為掃碼登錄微信
    while not wx_inst.get_myself():
        time.sleep(5)
    print('登陸成功')
    while True:
        msg = queue_recved_event.get()
        data = msg.get('data')
        sendinfo = data.get('sendinfo')
        data_type = str(data.get('data_type'))
        msgcontent = str(data.get('msgcontent'))
        is_recv = data.get('is_recv')
        print(msg)
        if data_type == '1' and 'revokemsg' not in msgcontent:
            pre_msg = msgcontent
        if sendinfo is not None and 'revokemsg' in msgcontent:
            user = str(sendinfo.get('wx_id_search'))
            recall = '撤回的消息:' + pre_msg
            wx_inst.send_text(to_user=user, msg=recall)

看一下操作

WechatPCAPI庫如何實現(xiàn)自動化回復(fù)


看完了這篇文章,相信你對WechatPCAPI庫如何實現(xiàn)自動化回復(fù)有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

新聞名稱:WechatPCAPI庫如何實現(xiàn)自動化回復(fù)
網(wǎng)站地址:http://muchs.cn/article14/ipgege.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、品牌網(wǎng)站制作、手機網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、網(wǎng)頁設(shè)計公司、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司