1. 程式人生 > >python筆記之微信機器人

python筆記之微信機器人

# 初始化登陸資訊
# 匯入微信機器人包
import wxpy
# 完成二維碼登陸,cache_path:建立wxpy.pkl快取檔案
wxpy.Bot(cache_path=True)
# 保持微信執行狀態
wxpy.embed()

bot物件

# 將wxpy中所有變數匯入
from wxpy import *
bot = Bot(cache_path=True)
# 返回值為登陸該賬戶的使用者
print(bot)

# 獲取所有好友
friends = bot.friends()
# print(friends)

# 自己
_self = friends[0]
print(_self)
# 性別
print(_self.sex)
# 省份
# print(_self.province)

# 所有分組
groups = bot.groups()
# print(groups)

# 指定分組
# 指定好友,指定分組都不能排除重名情況,所以search的結果為list型別
# 通常只有指定的一個好友|分組,那麼就是list的第0位
groups = bot.groups().search("嘿嘿群")
print(groups)

embed()

指定好友回覆

from wxpy import *
bot = Bot(cache_path=True)
friend = bot.friends().search("予你")


@bot.register()
def abs(msg):
    name = msg.sender.name
    print(msg.text)
    print(name + ":" + msg.text)
    if name == friend.name:
        return "自動回覆,工作中勿擾"


embed()