本文實例為大家分享了python接入微信聊天機器人的具體代碼,供大家參考,具體內容如下
1.安裝庫wxpy:
pip install -U wxpy
or
pip install -U wxpy -i https://pypi.doubanio.com/simple/
2.簡單操作上手:
from wxpy import * bot = Bot(cache_path=True) #掃碼登錄驗證 friends_stat = bot.friends().stats() friend_loc = [] # 每一個元素是一個二元列表,分別存儲地區和人數信息 for province, count in friends_stat["province"].items(): if province != "": friend_loc.append([province, count]) # 對好友人數倒序排序 friend_loc.sort(key=lambda x: x[1], reverse=True) # 打印好友人數最多的10個地區: for item in friend_loc[:10]: print(item[0], item[1]) #打印好友男女比例: for sex, count in friends_stat["sex"].items(): # 1代表MALE, 2代表FEMALE if sex == 1: print("MALE %d" % count) elif sex == 2: print("FEMALE %d" % count)
3.聊天機器人,一起來調戲好友吧
from wxpy import * bot = Bot(cache_path=True) my_friend = bot.friends().search('好友昵稱')[0] #定位好友 my_friend.send('Hello!') #發送“Hello!”測試一下對接是否成功。 group = bot.groups().search('群名')[0] #定位群 #接入圖靈api:需要去下述網址申請: tuling = Tuling(api_key='在http://www.tuling123.com/申請') # 使用圖靈機器人自動與指定好友聊天 @bot.register(my_friend) def reply_my_friend(msg): tuling.do_reply(msg)
Reference:
[1] wxpy: 用 Python 玩微信
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。