This article is participating in Python Theme Month. See the link to the event for more details

HXDM, HJMM, I’m still the same unwomanly cat.

First of all, all wechat can be used! All wechat can be used! All wechat can be used! The Web version can be used without login!

Today we use Python to write a micro letter robot, similar to gold mining sauce automatic welcome, automatic invitation and so on, before seeing the group in the arc big guy also useful micro letter robot, at @ will judge the time, if the time is more than such as 11 o ‘clock, directly reply: Lao Tze fell asleep, something to hold up tomorrow!

This article mainly solve the feasibility and provide ideas, all kinds of implementation does not participate, need to dig friends play their own, it is very simple to achieve.

Everyone rest assured to eat, this project this cat use normal ~, do not go to the Web interface, the Web version can not login can also use

So, take the small bench

start

First, you need to have a Python environment, using Python3. If you are still a beginner, I recommend reading my previous article [Minimum Necessary Knowledge] Getting Started with Python. The Python Topic month is sufficient for getting started with Python

Python3’s environment is fine so let’s move on

Please install it through Github for the time being. The modified version of ItChat has been officially using the Web interface and has not been fixed, so you can only use it yourself

pip3 install git+https://github.com/yizhihuamao/ItChat.git
Copy the code

Check whether the installation is successful

Pip3 list itchat 1.4.0Copy the code

Seeing this guy means it’s a success

Initialize the

import itchat
from itchat.content import *

Receive general messages, texts, maps, business cards, notifications, shares, etc
@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
def text_reply(msg) :
    print('msg', msg)
    msg.user.send('xxx%s: %s' % (msg.type, msg.text))

itchat.auto_login(hotReload=False)
itchat.run(True)
Copy the code

perform

python3 bot.py
Copy the code

You can scan the QR code to log in

We can also optimize this

# And save the login status, the second time does not need to scan the code login
itchat.auto_login(hotReload=False)
Copy the code

Can also be

Display the QR code in the terminal, and save the login status, the second time does not need to scan the code login
itchat.auto_login(hotReload=False, enableCmdQR=2)
Copy the code

See the demand, scan the code to try

See? This is desktop wechat login

Send a message when you’re sure

That’s half the battle

Single chat text, map, business card, notification, share

The above initialization time is these, text can be connected to chat API, the language specification rules, can be simple implementation of the weather, check the bus and so on.

Single chat multimedia

Receive multimedia messages, pictures, voice, video, attachments, etc
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg) :
    print('media', msg)
    msg.download(msg.fileName)
    typeSymbol = {
        PICTURE: 'img',
        VIDEO: 'vid', }.get(msg.type.'fil')
    return '@%s@%s' % (typeSymbol, msg.fileName)
Copy the code

Multimedia and other attachments will be downloaded to the project directory, according to the need to change to other directories or OSS can be, voice can be connected to THE API to text, according to natural language analysis, remember before there is a go out to ask the public number, is so to play, voice can intelligentiously answer your questions, interested can play. Pictures can be searched, such as Taobao search picture, can also be ai processing under the generation of art painting, returned to the user, now there is a similar library, video, attachment you can imagine, what good ideas can be synchronized with this cat

Friend request

# Received automatic reply to friend request
@itchat.msg_register(FRIENDS)
def add_friend(msg) :
    print('friend', msg)
    msg.user.verify()
    msg.user.send('Nice to meet you! ')
Copy the code

This doesn’t seem like much to say. Read the comments

Group chat receiving notifications

# Receive notification
@itchat.msg_register(NOTE, isGroupChat=True)
def text_reply(msg) :
    # Self regular match to extract nicknames, as well as modify the greeting
    return msg.user.send(U '@ welcome xx % s' % (msg.text))
Copy the code

This can achieve gold sauce in the wechat group in the greeting, such as a new group, send a greeting; You can also send promotional messages, but @ everyone doesn’t work

Receive business cards in group chats

# Receive shared business cards
@itchat.msg_register(CARD, isGroupChat=True)
def text_reply(msg) :
    print('CARD', msg)
Copy the code

It doesn’t seem to work. Sending your business card to a group will trigger

Group chat to share

# Receive shared articles, group invitations to share, etc
@itchat.msg_register(SHARING, isGroupChat=True)
def text_reply(msg) :
    print('SHARING', msg)
Copy the code

This can be achieved, such as sharing some irrelevant pages to the group, we count the number of times, more than how many warnings, more than how many times directly kicked out

Group chat, text, picture, recording, attachment, video, etc

PICTURE, RECORDING, ATTACHMENT, VIDEO
@itchat.msg_register(TEXT, isGroupChat=True)
def text_reply(msg) :
    print('group', msg)
    print(json.dumps(msg, sort_keys=True, indent=2))
    # @
    if msg.isAt:
        msg.user.send(u'@%s\u2005I received: %s' % (
            msg.actualNickName, msg.text))
Copy the code

This interface can be achieved before see the group in the excellent arc big guy is also useful wechat robot, @ will judge the time, if the time exceeds such as 11 o ‘clock, directly reply: Lao Tze fell asleep, something to hold up tomorrow!

Application scenarios

Ben also organized the following scenes, more good ideas are welcome to share

  • Single chat interface can achieve such as sending Python, push the TWO-DIMENSIONAL code of Python wechat group, more than the number can also simulate push group invitation, My cat tried, can be left for friends to dig homework. The invitation link and the QR code will expire. Ben meow also has an idea that when it is about to expire, use the group main number (because the invitation will be followed by the group main) to take screenshots or send them to the file assistant and write them into the database, so that the group invitation or THE QR code will not be sent by people. But there is no API for the invitation group for the time being
  • The maximum number of wechat groups is 500, and multiple groups can be synchronized
  • Custom time period reply (the same robot of excellent arc boss)
  • Pull groups based on responses
  • Automatically welcomes new group members
  • Group message repository, backup, analysis, statistics, display and so on
  • Responsible for third-party client, chat tool, statistical analysis, and customer service assessment (business)
  • Message prevention retraction
  • Send wechat messages regularly
  • Message monitoring, custom reply messages

2021-7-22 8:47pm Test normal, rest assured to eat you ~