Skip to content

Commit

Permalink
Merge pull request #33 from zhayujie/feature-qq
Browse files Browse the repository at this point in the history
feat: support qq private chat
  • Loading branch information
zhayujie authored Feb 26, 2023
2 parents a8d385a + dae91bf commit 2d3e679
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 1 deletion.
1 change: 0 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from channel import channel_factory
from common import log


if __name__ == '__main__':
try:
# load config
Expand Down
5 changes: 5 additions & 0 deletions channel/channel_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ def create_channel(channel_type):
from channel.wechat.wechat_mp_service_channel import WechatServiceAccount
return WechatServiceAccount()

elif channel_type == const.QQ:
from channel.qq.qq_channel import QQChannel
return QQChannel()

elif channel_type == const.GMAIL:
from channel.gmail.gmail_channel import GmailChannel
return GmailChannel()

elif channel_type == const.TELEGRAM:
from channel.telegram.telegram_channel import TelegramChannel
return TelegramChannel()
Expand Down
106 changes: 106 additions & 0 deletions channel/qq/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# go-cqhttp 默认配置文件

account: # 账号相关
uin: 123456 # 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

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

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

# 媒体文件缓存, 删除此项则使用缓存文件(旧版行为)
cache:
image: data/image.db
video: data/video.db

# 连接服务列表
servers:
# 添加方式,同一连接方式可添加多个,具体配置说明请查看文档
#- http: # http 通信
#- ws: # 正向 Websocket
#- ws-reverse: # 反向 Websocket
#- pprof: #性能分析服务器
# 反向WS设置
- http:
# 服务端监听地址
# 如需指定监听ipv4, 可使用 `address: tcp4://0.0.0.0:5700` (ipv6同理)
address: 0.0.0.0:5700
# 反向HTTP超时时间, 单位秒
# 最小值为5,小于5将会忽略本项设置
timeout: 5
middlewares:
<<: *default # 引用默认中间件
# 反向HTTP POST地址列表
post:
- url: http://127.0.0.1:8080
26 changes: 26 additions & 0 deletions channel/qq/qq_channel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from channel.channel import Channel
from aiocqhttp import CQHttp, Event
from common import log
from concurrent.futures import ThreadPoolExecutor

bot = CQHttp(api_root='http://127.0.0.1:5700')
thread_pool = ThreadPoolExecutor(max_workers=8)

@bot.on_message('private')
def handle_private_msg(event: Event):
log.info("event: {}", event)
QQChannel().handle(event)

class QQChannel(Channel):
def startup(self):
bot.run(host='127.0.0.1', port=8080)

def handle(self, msg):
thread_pool.submit(self._do_handle, msg)

def _do_handle(self, msg):
context = dict()
log.info("event: {}", "do_handle")
context['from_user_id'] = msg.user_id
reply_text = super().build_reply_content(msg.message, context)
bot.sync.send_private_msg(user_id=msg.user_id, message=reply_text)
1 change: 1 addition & 0 deletions common/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
WECHAT = "wechat"
WECHAT_MP = "wechat_mp"
WECHAT_MP_SERVICE = "wechat_mp_service"
QQ = "qq"
GMAIL = "gmail"
TELEGRAM = "telegram"

Expand Down

0 comments on commit 2d3e679

Please sign in to comment.