微信机器人--定时给朋友发送消息and 自动回复消息

定时给朋友发送消息:

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


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值