Automate Daily WeChat Greetings with Python: Step‑by‑Step Tutorial
Learn how to use Python, wxpy, and a timer to automatically fetch a daily quote and send personalized good‑morning or good‑night messages to a WeChat contact, complete with code examples and deployment tips.
Many programmers are single, and this tutorial shows how to use Python to automatically send a daily good‑morning or good‑night message to a chosen WeChat contact.
Prerequisites
Language: Python 3 IDE: PyCharm
Import Packages
We use wxpy to control WeChat, requests to fetch a daily quote, and Timer (a subclass of threading.Thread) for scheduling.
from wxpy import *
import requests
from threading import TimerLogin to WeChat
Create a Bot object with caching enabled so you don’t need to scan the QR code each time.
bot = Bot(cache_path=True)Fetch Daily Quote
Define get_msg() to request the ICIBA daily sentence API and return the English sentence and its Chinese translation.
def get_msg():
url = 'http://open.iciba.com/dsapi/' # API endpoint
html = requests.get(url)
content = html.json()['content'] # English sentence
note = html.json()['note'] # Chinese translation
return content, noteSend Message
Define send_msg() to retrieve the quote, locate the friend by their WeChat nickname, send the English sentence, its translation, and a custom greeting, then schedule the next run with Timer. If sending fails, a failure notice is sent to yourself.
def send_msg():
try:
msgs = get_msg()
content, note = msgs[0], msgs[1]
my_friend = bot.friends().search(u'机器人')[0] # target nickname
my_friend.send(content)
my_friend.send(note)
my_friend.send(u'来自 brucepk 的问候')
t = Timer(10, send_msg) # repeat after 10 seconds (example)
t.start()
except BaseException:
my_friend = bot.friends().search(u'brucepk')[0]
my_friend.send(u'消息发送失败')Run the Script
Execute the main function to start the automated messaging loop.
if __name__ == '__main__':
send_msg()Result
The script sends the daily quote to the specified contact. A screenshot of the successful messages is shown below.
Because the script must keep running, you can host it on a cloud server; some providers offer free trial periods.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
