python开发微信聊天机器人

直接上代码吧,里面比较简单。很容易实现

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from wxpy import *
import requests


# 图灵机器人 - 中文语境下智能度最高的机器人大脑
def tuling(info):
    params = {
        'key': 'db0b623ae0dd4e9ca28a89174abe156c',
        'info': info,
    }
    try:
        response = requests.get('http://www.tuling123.com/openapi/api', params = params, timeout = 10)
        return response.json()['text'] + '〖自动回复〗'
    except Exception as e:
        pass


# 手机微信扫码登陆
bot = Bot(cache_path = True)

# 加自己为好友
bot.self.add()
bot.self.accept()

# 给自己发消息
bot.self.send('能收到吗?')

# 我的群组
my_groups = bot.groups().search('群组名称')[0]

# 我的妻子
my_wife = bot.friends().search('你老婆名称')[0]

# 测试消息发送功能
my_wife.send('我们分手吧')
my_wife.send('哈哈哈哈,这是机器人发的。')


# 自动打印剩余消息
@bot.register()
def print_others(msg):
    print(msg)


# 自动回复群组消息
@bot.register(my_groups)
def reply_my_groups(msg):
    if msg.type == 'Text':
        content = msg.text
        return tuling(content)


# 自动回复妻子消息
@bot.register(my_wife)
def reply_my_wife(msg):
    if msg.type == 'Text':
        content = msg.text
        return tuling(content)


# 自动接受好友请求
@bot.register(msg_types = FRIENDS)
def auto_accept_friends(msg):
    # 接受请求
    new_friend = msg.card.accept()
    # 发送消息
    new_friend.send('哈哈,我自动接受了你的好友请求!')


# 进入 Python 命令行,程序在后台保持运行
embed()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值