python实现微信机器人

  • 首先去思知申请个账号,创建个机器人,把appid换成你自己的;

  • 安装相应的包,将代码贴到python文件中,运行文件,手机微信扫码登陆,然后就机器人就跑起来了,为这里没有对文字、图片、自带的图片进行处理,还是可以优化的,图片分为自己收集的图片、表情和系统自带的表情。

  • 相关资源:
    思知官网:https://www.ownthink.com/
    思知接口文档:https://www.ownthink.com/docs/
    wxpy其他相关用法:https://www.cnblogs.com/linhaifeng/articles/9057200.html

  • 附代码:

#! --*-- coding: utf-8 --*--

import requests
import json
from wxpy import *
import pprint

bot = Bot(console_qr=True, cache_path=True)  # 必须先登录过一次以后才可以使用缓存

si_zhi_url = 'https://api.ownthink.com/bot'
appid = ''

# 获取返回消息
def auto_reply_sizhi(text):
    payload = {
    "spoken": text,
    "appid": appid,
    "userid": "user"
    }
    r = requests.post(si_zhi_url, data=json.dumps(payload))
    result = json.loads(r.content)
    message += result['data']['info']['text']
    if 'heuristic' in result['data']['info'] and result['data']['info']['heuristic']:
        for item in result['data']['info']['heuristic']:
            message += ',  ' + item

    return message

# 监听接收到的消息
@bot.register()
def forward_message(msg):
    return auto_reply_sizhi(msg.text)

# 让程序一直运行
embed()

如果你不想自动回复群消息,可以将下面这个方法重写:

@bot.register()
def forward_message(msg):
    pprint.pprint("接收到的数据:")
    pprint.pprint(msg)
    print(msg)
    if 'Group' not in str(type(msg.sender)):
        return auto_reply_sizhi(msg.text)
    else:
        pass
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值