Backend Development 6 min read

Python Script to Send New Year Greetings to WeChat Friends Automatically

This article provides a Python script that uses the itchat and schedule libraries to automatically log in to WeChat, select friends with remarks, and send randomly chosen New Year greeting messages to each contact at a specified date and time.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Script to Send New Year Greetings to WeChat Friends Automatically

The tutorial demonstrates how to create a simple Python program that automatically sends New Year greetings to all WeChat contacts. It outlines the required environment, noting that any Python version can be used and that the third‑party libraries itchat and schedule must be installed.

The core of the script logs into WeChat, retrieves the friend list, filters friends that have a remark name, and stores their usernames in a dictionary. A list of Chinese New Year greeting strings is defined, and a function send_greet selects a random greeting, prefixes it with the friend's remark name, and sends the message via itchat.send .

A scheduled job is defined using the schedule library to run daily at midnight. When the current date matches the target New Year date (hard‑coded as 2019‑01‑01 in the example), the job iterates over the filtered friends and calls send_greet for each.

<code>#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 18-12-9 上午9:08
# @Author  : KainHuck
# @Email   : [email protected]
# @File    : 元旦祝福.py

import itchat
import random
import schedule
import datetime
import time

# 登录,并暂存登录状态
itchat.auto_login(hotReload=True)
# 获取所有好友信息
friends = itchat.get_friends(update=True)

# 筛选掉没有备注名的好友, 并将要发送祝福语的好友放置在一个字典里
final_friends = {}
for each in friends:
    if len(each['RemarkName']) > 0:
        final_friends[each['RemarkName']] = each['UserName']

# 祝福语列表
greeting = ['元旦到了,在辞旧迎新的日子里,我愿为你送走烦恼迎来开心,送走压力迎来健康,送走失意迎来顺利,送走意外迎来平安,并希望你快快乐乐过个元旦节。',
            '圆圆的梦想,七色的花;圆圆的人生,五彩的画;圆圆的元旦,幸福的家;圆圆的问候,事业大发;圆圆的祝福,你笑哈哈!祝你元旦快乐! ',
            '元旦即将来到,信息不能迟到;好友前来报到,祝福提前送到:爱情浪漫美好,友情温暖笼罩,亲情时刻围绕,工作业绩攀高,生活幸福欢笑,来年更美好! ',
            '喜悦,在心中荡漾;笑容,在脸颊洋溢;歌声,在悠扬回荡;舞步,在惬意游走;礼花,在尽情绽放;祝福,在频频发送。朋友,元旦快乐!祝你幸福,阖家欢乐! ',
            '元旦加祝福,新的一年好幸福;元旦加努力,新的一年好成绩;元旦加上你,新的一年好给力!祝元旦的你格外美丽,快乐一整个世纪!',
            '新的开始新希望,新的一天新阳光,开始新的追求,播下新的梦想,翻开新的一页,写下新的辉煌。新的一年开始,送你深深的祝福,元旦快乐。',
            '元旦将来到,我心费思量。朋友关系好,送个什么好。我无多钱财,也没中彩票。短信送祝福,礼轻情意重。祝你轻轻松松无烦恼,快快乐乐过元旦!',
            '将快乐化作短信,发送给你,愿你新的一年快乐无边;将好运融入信息,传达给你,愿新的一年里,好运连连幸福不断;元旦里我将所有的祝福复制,一并发于你,愿你明年万事如意! ']

# 发送祝福函数
def send_greet(RemarkName,userName):
    greet = random.choice(greeting)  # 随机选择一句祝福语
    message = RemarkName + ',' + greet  # 添加上备注名
    itchat.send(message, toUserName=userName)

# 定义任务
def job():
    now_date = str(datetime.datetime.now().date())  # 获取函数执行时的时间
    if now_date == '2019-01-01' or now_date == '2019-1-1':  # 如果是2019年元旦就执行(PS:忘记datetime.datetime.now().date()输出的格式了...)
        for each_friend in final_friends:
            send_greet(each_friend, final_friends['final_friends'])

# 每天00:00执行一次job函数
schedule.every().day.at("00:00").do(job)

while True:
    schedule.run_pending()
    time.sleep(1)
</code>

Note: The script is intended to run on a server for continuous execution, and the greeting texts are sourced from the internet.

Schedulingwechatitchat
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.