Using Python to access wechat chat robot

Time:2020-1-19

The example of this article shares the specific code of Python accessing wechat chat robot for your reference. The specific content is as follows

1. Installation library wxpy:


pip install -U wxpy

or


pip install -U wxpy -i https://pypi.doubanio.com/simple/

2. Simple operation:

from wxpy import *
BOT = BOT (cache? Path = true) ා password scanning login verification
friends_stat = bot.friends().stats()

Friend? LOC = [] each element is a binary list, which stores area and number of people information respectively
for province, count in friends_stat["province"].items():
  if province != "":
    friend_loc.append([province, count])

#Sort friends in reverse order
friend_loc.sort(key=lambda x: x[1], reverse=True)
#Print the top 10 regions with the most friends:
for item in friend_loc[:10]:
  print(item[0], item[1])

#Print gender ratio of friends:
for sex, count in friends_stat["sex"].items():
  #1 for male, 2 for female
  if sex == 1:
    print("MALE %d" % count)
  elif sex == 2:
    print("FEMALE %d" % count)

3. Chat robot, let’s flirt with friends

from wxpy import *
bot = Bot(cache_path=True)
My friend = bot. Friends(). Search ('friend nickname ') [0] ාlocate a friend
my_friend.send('Hello! ') send "Hello!" Test whether the docking is successful.
Group = bot. Groups(). Search ('group name ') [0] ා locate a group

#Access to Turing API: you need to apply at the following website:
Tuling = Tuling (api_key = 'apply at http://www.tuling123.com/)

#Use Turing robot to automatically chat with designated friends
@bot.register(my_friend)
def reply_my_friend(msg):
  tuling.do_reply(msg)

Reference:

[1] Wxpy: playing wechat in Python

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.

Recommended Today

RCAST 35: add type to currency

– font ALT: Simsun; MSO font charset: 134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} @font-face {font-family:”Cambria Math”; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Calibri; Variable; Ose-1: 216301111; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} /\* Style […]