Last active
August 23, 2017 06:49
-
-
Save leeyisoft/5a7b13f8cc248a15b5a7cab34cda947b to your computer and use it in GitHub Desktop.
qqbot 登陆二维码发送到指定服务的插件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
# QQBot 的配置文件 | |
# 使用 qqbot -u somebody 启动程序时,依次加载: | |
# 根配置 -> 默认配置 -> 用户 somebody 的配置 -> 命令行参数配置 | |
# 使用 qqbot 启动程序时,依次加载: | |
# 根配置 -> 默认配置 -> 命令行参数配置 | |
# 用户 somebody 的配置 | |
"8187" : { | |
# QQBot-term (HTTP-API) 服务器端口号(该服务器监听 IP 为 127.0.0.1 ) | |
# 设置为 0 则不会开启本服务器(此时 qq 命令和 HTTP-API 接口都无法使用)。 | |
"termServerPort" : 8187, | |
# 是否以文本模式显示二维码 | |
"cmdQrcode" : False, | |
# 显示/关闭调试信息 | |
"debug" : True, | |
# QQBot 掉线后自动重启 | |
"restartOnOffline" : True, | |
# 在后台运行 qqbot ( daemon 模式) | |
"daemon": False, | |
# 完成全部联系人列表获取之后才启动 QQBot | |
"startAfterFetch" : False, | |
}, | |
"8188" : { | |
# QQBot-term (HTTP-API) 服务器端口号(该服务器监听 IP 为 127.0.0.1 ) | |
# 设置为 0 则不会开启本服务器(此时 qq 命令和 HTTP-API 接口都无法使用)。 | |
"termServerPort" : 8188, | |
# 是否以文本模式显示二维码 | |
"cmdQrcode" : False, | |
# 显示/关闭调试信息 | |
"debug" : True, | |
# QQBot 掉线后自动重启 | |
"restartOnOffline" : True, | |
# 在后台运行 qqbot ( daemon 模式) | |
"daemon": False, | |
# 完成全部联系人列表获取之后才启动 QQBot | |
"startAfterFetch" : False, | |
}, | |
# 可以在 默认配置 中配置所有用户都通用的设置 | |
"默认配置" : { | |
"qq" : "", | |
"pluginPath" : "/Users/leeyi/workspace/afd/afd_sender/qqbot/plugins/", | |
"plugins" : [ | |
'qqbot.plugins.sampleslots', | |
'qqbot.plugins.schedrestart', | |
'upload_qrcode', | |
], | |
"pluginsConf" : { | |
'qqbot.plugins.schedrestart' : '8:00', | |
'upload_qrcode' : { | |
"qrcode_upload_api" : "http://marketing.dev.afd56.com.cn/api/qq/save-qrcode", | |
"security_key" : "44324234243232432432234", | |
}, | |
} | |
}, | |
# # 注意:根配置是固定的,用户无法修改(在本文件中修改根配置不会生效) | |
# "根配置" : { | |
# "termServerPort" : 8188, | |
# "httpServerIP" : "", | |
# "httpServerPort" : 8189, | |
# "qq" : "", | |
# "mailAccount" : "", | |
# "mailAuthCode" : "", | |
# "cmdQrcode" : False, | |
# "debug" : False, | |
# "restartOnOffline" : False, | |
# "daemon" : False, | |
# "startAfterFetch" : False, | |
# "pluginPath" : "", | |
# "plugins" : [], | |
# "pluginsConf" : {} | |
# }, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# encoding: utf-8 | |
import requests | |
import base64 | |
import hashlib | |
from qqbot.utf8logger import DEBUG | |
def _md5(Str): | |
m = hashlib.md5(Str.encode(encoding='utf8')) | |
return m.hexdigest() | |
def onQrcode(bot, pngPath, pngContent): | |
# 获取到二维码时被调用 | |
# 注意 : 此时 bot 尚未启动,因此请勿在本函数中调用 bot.List/SendTo/GroupXXX/Stop/Restart 等接口 | |
# 只可以访问配置信息 bot.conf | |
# bot : QQBot 对象 | |
# pngPath : 二维码图片路径 | |
# pngContent : 二维码图片内容 | |
DEBUG('%s.onQrcode: %s (%d bytes)', __name__, pngPath, len(pngContent)) | |
qrcodePath = './qrcode_%s.png' % bot.conf.termServerPort | |
with open(qrcodePath, 'wb') as f: | |
f.write(pngContent) | |
b64img = base64.b64encode(pngContent) #读取文件内容,转换为base64编码 | |
# qrcodePath = './qrcode_%s.html' % bot.conf.termServerPort | |
# with open(qrcodePath, 'wb') as f: | |
# con = '<html><body><img src="data:image/png;base64,%s" /></body></html>' % bytes.decode(b64img) | |
# f.write(str.encode(con)) | |
# 下面参数顺序很不能够变 | |
payload = { | |
'data' : 'data:image/png;base64,%s' % bytes.decode(b64img), | |
'port' : bot.conf.termServerPort, | |
'qq' : '', | |
} | |
sign = bot.conf.pluginsConf.get('upload_qrcode', {}).get('security_key', None) | |
for key in payload: | |
sign += '%s=%s' % (str(key), str(payload[key])) | |
payload['sign'] = _md5(sign) | |
url = bot.conf.pluginsConf.get('upload_qrcode', {}).get('qrcode_upload_api', None) | |
response = requests.post(url, data=payload) | |
DEBUG('requests.post/2 response: %s', __name__, response.text) | |
# 我配置文件里面同时配置了很多QQ,几个QQ同时需要扫描二维码登陆, | |
def onStartupComplete(bot): | |
# 启动完成时被调用 | |
# bot : QQBot 对象,提供 List/SendTo/GroupXXX/Stop/Restart 等接口,详见文档第五节 | |
# | |
# 解决问题如下: | |
# 现在的问题是,我在配置文件里面定义了一组关系{a_qq, port1, 二维码} , {b_qq, port2, 二维码}, {c_qq, port3, 二维码} | |
# 由于用户可以随意扫描二维码(这概率很高),可以导致端口和QQ的映射关系不是配置文件定义的一样, | |
# 导致调用 服务器的时候(http://127.0.0.1:8187/list/buddy)不是原先定义好的用户好友 | |
DEBUG('%s.onStartupComplete', __name__) | |
# 下面参数顺序很不能够变 | |
payload = { | |
'data' : '', | |
'port' : bot.conf.termServerPort, | |
'qq' : bot.conf.qq, | |
} | |
sign = bot.conf.pluginsConf.get('upload_qrcode', {}).get('security_key', None) | |
for key in payload: | |
sign += '%s=%s' % (str(key), str(payload[key])) | |
payload['sign'] = _md5(sign) | |
url = bot.conf.pluginsConf.get('upload_qrcode', {}).get('qrcode_upload_api', None) | |
response = requests.post(url, data=payload) | |
DEBUG('requests.onStartupComplete/1 response: %s', __name__, response.text) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment