定时给朋友发送消息:
from __future__ import unicode_literals
from threading import Timer
from wxpy import *
import requests
# bot=Bot()
# 从缓存中获取登录信息,刚登陆过,无需一直登陆
bot = Bot(cache_path=True)
def get_news():
"""获取金山词霸每日一句,英文和翻译"""
url = "http://open.iciba.com/dsapi/"
r = requests.get(url)
content = r.json()['content']
note = r.json()['note']
return content, note
def send_news():
try:
contents = get_news()
# 你朋友的微信名称,不是备注,也不是微信帐号。
my_friend = bot.friends().search(u'Quincy.Coder')[0]
my_friend.send(contents[0])
my_friend.send(contents[1])
my_friend.send(u"HELLO !")
# 每86400秒(1天),发送1次
t = Timer(86400, send_news)
t.start()
except:
# 你的微信名称,不是微信帐号。
my_friend = bot.friends().search('庸人莫自扰')[0]
my_friend.send(u"今天消息发送失败了")
if __name__ == "__main__":
send_news()
自动回复消息:
# from wxpy import *
#
# bot = Bot(cache_path=True)
#
# girl_friend = bot.search('刘刘刘')[0]
# print(girl_friend)
#
#
# @bot.register() # 接收从指定好友发来的消息,发送者即recv_msg.sender为指定好友girl_friend
# def recv_send_msg(recv_msg):
# print('收到的消息:', recv_msg.text) # recv_msg.text取得文本
# if recv_msg.sender == girl_friend:
# recv_msg.forward(bot.file_helper, prefix='老婆留言: ') # 在文件传输助手里留一份,方便自己忙完了回头查看
# ms = '老婆最美丽,我对老婆的爱如滔滔江水,连绵不绝'
# print('>>>给老婆回复的:', ms)
# return ms # 给老婆回一份
#
#
# embed()
from wxpy import *
# bot = Bot()
bot = Bot(cache_path=True)
gf = bot.friends().search('Quincy.Coder')[0]
@bot.register()
def recv_send_msg(msg):
print('收到的消息:', msg.text)
# msg.reply_file()
if msg.sender == gf:
msg.forward(bot.file_helper, prefix='女同学留言:')
ms = '【测试】好好学习,天天向上!'
return ms
embed()
参考官方文档:https://wxpy.readthedocs.io/zh/latest/chats.html#wxpy.Chat.send_file