前面三期全新上线!免费体验最热门的Claude2API接口Python版本!介绍了Claude2 api python版本的基础功能:包括创建会话、聊天、获取历史会话,清理历史记录等功能,文件上传功能和发送消息并附带附件功能;docker容器运行,运行cookie传参数使用,避免程序写死;增加replit 部署。之前也有很大家说到过后面整合微信和企业微信功能。今天将企业微信代码编写完成了,目前该项目已经更新到了v0.0.7 版本。v0.0.6版本实现微信功能,感兴趣的小伙伴可以在西瓜视频上面观看 《全新上线!免费体验最热门的Claude2API接口Python第三集支持微信》。
2023年8月26日我又更新了一期视频《全新上线!免费体验最热门的Claude2API接口第四集微信企业微信》。今天我把代码更新了目前最新版本是v0.0.8版本。本次版本主要升级了整合了QQ功能。实现了go-cqhttp和Claude2 api 接口的整合。下面介绍一下新增加功能:
下载
1.下载代码
https://github.com/wwwzhouhui/Claude2-PyAPI
我们需要通过git 客户端下载代码
git clone https://github.com/wwwzhouhui/Claude2-PyAPI.git
cd Claude2-PyAPI
如果国内网速下载代码比较慢可以增加一个代理
git clone https://ghproxy.com/https://github.com/wwwzhouhui/Claude2-PyAPI.git
以上完成代码下载。
代码更新说明
ImApp.py
import config
from common.log import logger
from channel import channel_factory
def run():
try:
# load config
config.load_config()
# create channel
channel = channel_factory.create_channel("qq")
# startup channel
channel.startup()
except Exception as e:
logger.error("App startup failed!")
logger.exception(e)
if __name__ == "__main__":
run()
主要功能是读取配置文件,然后通过创建一个工厂方法 来创建一个QQ频道。后面考虑整合更多IM 使用了工厂方法设计模式。
channel_factory.py
"""
channel factory
"""
from channel.wechat.wechat_channel import WechatChannel
from channel.wechatcom.wechatenterprise_channel import WechatEnterpriseChannel
from channel.qqchat.qqchat_channel import QqchaChannel
def create_channel(channel_type):
"""
create a channel instance
:param channel_type: channel type code
:return: channel instance
"""
if channel_type == 'wx':
return WechatChannel()
if channel_type == 'wxcom':
return WechatEnterpriseChannel()
if channel_type == 'qq':
return QqchaChannel()
raise RuntimeError
目前工厂实现QQ、微信、企业微信功能。后面如果实现其他IM 可以在此基础上进行扩展,代码耦合性更好了。
qqchat_channel.py 代码如下
from aiocqhttp import CQHttp, Event, MessageSegment
from channel.channel import Channel
from common.log import logger
from config import conf
from bridge.bridge import Bridge
bot = CQHttp()
@bot.on_message('private')
async def _(event: Event):
context = dict()
logger.info(event.message)
await bot.send(event, QqchaChannel.build_reply_content(event.message,event.message, context))
return {'reply': event.message}
@bot.on_startup
async def startup():
logger.info("启动完毕,接收消息中……")
class QqchaChannel(Channel):
def __init__(self):
self.host = conf().get('reverse_ws_host')
self.port = conf().get('reverse_ws_port')
def startup(self):
# startup()
# bot.on_startup(startup)
# logger.info("startup启动完毕,接收消息中……")
bot.run(host=self.host, port=self.port)
def handle(self, msg):
logger.info("handle"+msg)
def send(self, msg, receiver):
logger.info('[QQ] sendMsg={}, receiver={}'.format(msg, receiver))
bot.send(receiver, msg)
def build_reply_content(self, query, context=None):
return Bridge().fetch_reply_content(query, context)
为了后期方便扩展,代码目前配置文件从config.json中读取
这里面增加了websocket 反向代理链接go-cqhttp,程序启动后会监听 8080端口
go-cqhttp
这里我们借用了go-cqhttp这个软件来实现QQ 功能。它的代码下载地址:
https://github.com/Mrs4s/go-cqhttp 官方这里介绍这个软件使用,我在这里就不详细展开了。
我们下载go-cqhttp 解压到widows 本地 启动go-cqhttp.exe 会生成一个go-cqhttp.bat 文件
双击go-cqhttp.bat 文件会提示我们生成哪中代理模式
这里我们就选择3 反向代理websocket.选择好后会生成一个config.yml 文件,打开config.yml文件
# go-cqhttp 默认配置文件
account: # 账号相关
uin: 1004359274 # QQ账号
password: '' # 密码为空时使用扫码登录
encrypt: false # 是否开启密码加密
status: 0 # 在线状态 请参考 https://docs.go-cqhttp.org/guide/config.html#在线状态
relogin: # 重连设置
delay: 3 # 首次重连延迟, 单位秒
interval: 3 # 重连间隔
max-times: 0 # 最大重连次数, 0为无限制
# 是否使用服务器下发的新地址进行重连
# 注意, 此设置可能导致在海外服务器上连接情况更差
use-sso-address: true
# 是否允许发送临时会话消息
allow-temp-session: false
# 数据包的签名服务器
# 兼容 https://github.com/fuqiuluo/unidbg-fetch-qsign
# 如果遇到 登录 45 错误, 或者发送信息风控的话需要填入一个服务器
# 示例:
# sign-server: 'http://127.0.0.1:8080' # 本地签名服务器
# sign-server: 'https://signserver.example.com' # 线上签名服务器
# 服务器可使用docker在本地搭建或者使用他人开放的服务
sign-server: '-'
heartbeat:
# 心跳频率, 单位秒
# -1 为关闭心跳
interval: 5
message:
# 上报数据类型
# 可选: string,array
post-format: string
# 是否忽略无效的CQ码, 如果为假将原样发送
ignore-invalid-cqcode: false
# 是否强制分片发送消息
# 分片发送将会带来更快的速度
# 但是兼容性会有些问题
force-fragment: false
# 是否将url分片发送
fix-url: false
# 下载图片等请求网络代理
proxy-rewrite: ''
# 是否上报自身消息
report-self-message: false
# 移除服务端的Reply附带的At
remove-reply-at: false
# 为Reply附加更多信息
extra-reply-data: false
# 跳过 Mime 扫描, 忽略错误数据
skip-mime-scan: false
# 是否自动转换 WebP 图片
convert-webp-image: false
# http超时时间
http-timeout: 0
output:
# 日志等级 trace,debug,info,warn,error
log-level: warn
# 日志时效 单位天. 超过这个时间之前的日志将会被自动删除. 设置为 0 表示永久保留.
log-aging: 15
# 是否在每次启动时强制创建全新的文件储存日志. 为 false 的情况下将会在上次启动时创建的日志文件续写
log-force-new: true
# 是否启用日志颜色
log-colorful: true
# 是否启用 DEBUG
debug: false # 开启调试模式
# 默认中间件锚点
default-middlewares: &default
# 访问密钥, 强烈推荐在公网的服务器设置
access-token: ''
# 事件过滤器文件目录
filter: ''
# API限速设置
# 该设置为全局生效
# 原 cqhttp 虽然启用了 rate_limit 后缀, 但是基本没插件适配
# 目前该限速设置为令牌桶算法, 请参考:
# https://baike.baidu.com/item/%E4%BB%A4%E7%89%8C%E6%A1%B6%E7%AE%97%E6%B3%95/6597000?fr=aladdin
rate-limit:
enabled: false # 是否启用限速
frequency: 1 # 令牌回复频率, 单位秒
bucket: 1 # 令牌桶大小
database: # 数据库相关设置
leveldb:
# 是否启用内置leveldb数据库
# 启用将会增加10-20MB的内存占用和一定的磁盘空间
# 关闭将无法使用 撤回 回复 get_msg 等上下文相关功能
enable: true
sqlite3:
# 是否启用内置sqlite3数据库
# 启用将会增加一定的内存占用和一定的磁盘空间
# 关闭将无法使用 撤回 回复 get_msg 等上下文相关功能
enable: false
cachettl: 3600000000000 # 1h
# 连接服务列表
servers:
# 添加方式,同一连接方式可添加多个,具体配置说明请查看文档
#- http: # http 通信
#- ws: # 正向 Websocket
#- ws-reverse: # 反向 Websocket
#- pprof: #性能分析服务器
# 反向WS设置
- ws-reverse:
# 反向WS Universal 地址
# 注意 设置了此项地址后下面两项将会被忽略
universal: ws://127.0.0.1:8080/ws
# 反向WS API 地址
api: ws://your_websocket_api.server
# 反向WS Event 地址
event: ws://your_websocket_event.server
# 重连间隔 单位毫秒
reconnect-interval: 3000
middlewares:
<<: *default # 引用默认中间件
我们需要填写 QQ 号 这个作为机器人QQ使用。 其他都不需要修改。
这个就是我们go-cqhttp 程序连接服务端反向代理地址。另外在go-cqhttp.exe文件目录会生成一个device.json
打开这个device.json
{"display":"MIRAI.584732.001","product":"mirai","device":"mirai","board":"mirai","model":"mirai","finger_print":"mamoe/mirai/mirai:10/MIRAI.200122.001/0164521:user/release-keys","boot_id":"8baf0dbc-8d42-1233-aa38-b1f701f27a31","proc_version":"Linux version 3.0.31-w9KHXyO8 (android-build@xxx.xxx.xxx.xxx.com)","protocol":2,"imei":"063641062330092","brand":"mamoe","bootloader":"unknown","base_band":"","version":{"incremental":"5891938","release":"10","codename":"REL","sdk":29},"sim_info":"T-Mobile","os_type":"android","mac_address":"00:50:56:C0:00:08","ip_address":[10,0,1,3],"wifi_bssid":"00:50:56:C0:00:08","wifi_ssid":"\u003cunknown ssid\u003e","imsi_md5":"591793082e2b65517948acf3cbe70805","android_id":"9b9f638f7a51d072","apn":"wifi","vendor_name":"MIUI","vendor_os_name":"mirai"}
修改"protocol":2 2代表QQ 手表模式。后面启动程序就可以监听服务端了
启动程序
运行ImApp.py 就可以启动程序了。
第一次启动go-cqhttp 会出现 QQ 登录的二维码,我们使用手机端QQ 扫描二维码完成登录。
程序演示
我们启动好ImApp.py 启动好go-cqhttp,电脑登录另外一个QQ
我们使用 另外的qq 给这个机器人发送消息。
演示效果如下
程序运行后我们查看网页端claude2界面是否是我们通过QQ 程序调用的
以上截图就可以发现完全是QQ 消息通过claude2api接口发送实现调用的。
总结
总体来说以上操作还是比较简单的看过前面几期视频和文章的小伙伴 应该能够快速上手,怎么免费体验一下qq调用cluade2接口,还不快乐试一试吧。
本期就分享到这里面,觉的还不错麻烦您留言 、点赞+ 关注。