Use Python to automatically reply to WeChat friends' New Year greetings

Use Python to automatically reply to WeChat friends' New Year greetings

It's New Year's Eve today, and my friends have already returned home, and started to send blessings on WeChat, give out red envelopes, and grab red envelopes,

itchat

itchat is an open source WeChat personal account interface. It is so simple to use Python to call WeChat, and a WeChat robot that can process all information can be completed with very simple code. Nowadays, WeChat has become a way of life for everyone to socialize, travel, pay, etc. The convenience WeChat brings to us is beyond doubt. itchat WeChat robot can expand the function of personal WeChat, making it faster and more convenient for us my own life.

Install

pip install ithat

Command line QR code

Through this command, you can use the QR code on the command line when logging in, and scan the code on your mobile phone to log in to realize the function.

Temporarily save the login status after exiting the program

itchat.auto_login(hotReload=True)

Benefits: Dry goods are coming

Next, let's use itchat to automatically reply to friends' New Year greetings.

First: import package

import itchat
import requests
import time
import random
from itchat.content import 

This command is used to record the information of friends who have replied

replied = []

Get new year greetings

Log in to the Aijihe data website ( http://www.xjihe.com/ ), select the API function in the upper right corner, and scroll down to find a complete collection of blessings.
insert image description hereinsert image description hereinsert image description here

Get New Year Wishes Code

def GetRandomGreeting():
	res = requests.get("http://www.xjihe.com/api/life/greetings?festival=新年&page=10", headers = {'apiKey':'sQS2ylErlfm9Ao2oNPqw6TqMYbJjbs4g'})
	results = res.json()['result']
	return results[random.randrange(len(results))]['words']

Send New Year greetings

def SendGreeting(msg):
	global replied
	friend = itchat.search_friends(userName=msg['FromUserName'])
	if friend['RemarkName']:
		itchat.send((friend['RemarkName']+','+GetRandomGreeting()), msg['FromUserName'])
	else:
		itchat.send((friend['NickName']+','+GetRandomGreeting()), msg['FromUserName'])
	replied.append(msg['FromUserName'])

text message

@itchat.msg_register([TEXT])
def text_reply(msg):
	if '年' in msg['Text'] and msg['FromUserName'] not in replied:
		SendGreeting(msg)

other information

@itchat.msg_register([PICTURE, RECORDING, VIDEO, SHARING])
def others_reply(msg):
	if msg['FromUserName'] not in replied:
		SendGreeting(msg)
if __name__ == '__main__':
	itchat.auto_login()
itchat.run()

The effect achieved is as follows

insert image description here

write at the end

In the new year, I wish everyone a happy new year, a happy festival, good health, and a happy family.

Guess you like

Origin blog.csdn.net/weixin_43401243/article/details/86762500