背景
前面已经说过要做一个将抖音的视频转成图文并保存到自己的文件库的系统。其中有一个比较困扰的点。
如何在用户成本最少的情况下,方便地接入到自己的系统中
选型
当时想到了几种方案,并做了简单的调研
1. 微信
方案:没有提供接口,如果要做的话,需要用辅助功能或者是hook微信,也就是说,需要使用非正常的操作来完成。
优缺点:
-
技术成本比较高 技术上需要较大的修改,并且后续的兼容成本也存在问题
-
业务成本大,需要有一个手机专门来做这个事情,并且需要一个单独的微信小号来处理。
2. 企业微信:
优缺点:需要开启企业微信
3. 钉钉
方案:暂未发现可用方案
4. 公众号
方案:接收普通消息
优缺点:优点是可以直接使用,缺点就是可能会影响到现在有的公众号业务
综上,选择企业微信进行尝试
实现
回调的验证和信息的接收
我已经打听好了,我们要用的是企业微信的客服,查看官方文档 我们可以知道获取到用户的消息需要两个方法
-
通过get方法验证接口
-
通过post接口接收发送的数据
get方法的实现,可以参考get接口验证参考
post的方法需要进行一个改造
-
从post方法中获取到必须的参数
if request.method == 'POST':
data = str(request.data.decode(encoding="utf-8"))
sign = request.args.get("msg_signature")
timestamp = request.args.get("timestamp")
nonce = request.args.get("nonce")
return qiweiOut.postReciveMsg(sign, timestamp, nonce, data)
-
获取到token为下一步拉取内容做准备
def postReciveMsg(sReqMsgSig, sReqTimeStamp, sReqNonce, sReqData):
sToken = global_config.sToken
sEncodingAESKey = global_config.sEncodingAESKey
sCorpID = global_config.sCorpID
wxcpt = WXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID)
ret, sMsg = wxcpt.DecryptMsg(sReqData, sReqMsgSig, sReqTimeStamp, sReqNonce)
print(ret, sMsg)
if (ret != 0):
print
"ERR: DecryptMsg ret: " + str(ret)
sys.exit(1)
xml_tree = ET.fromstring(sMsg)
token = xml_tree.find("Token").text
return getKFMsgByToken(token)
-
检查 access_token 如果没有就请求,如果有就拉去信息
-
解析拉取到的信息,将解析的内容交给内容处理逻辑
def getKFMsgByToken(token):
getaccess()
url = "https://qyapi.weixin.qq.com/cgi-bin/kf/sync_msg?access_token=" + qiwei.QiWeiOut.access_token
body = {
"cursor": global_config.cursor,
"token": token,
"limit": 1000,
"voice_format": 0
}
response = requests.post(url, data=json.dumps(body))
content = response.content.decode("utf-8")
jsonObj = json.loads(content)
errorCode = jsonObj['errcode']
print("getKFMsgByToken error:"+str(errorCode))
if jsonObj['errcode'] == 0:
global_config.cursor = jsonObj['next_cursor']
list = jsonObj['msg_list']
for i in list:
if i['msgtype'] == 'text':
contentMsg = i['text']['content']
print(contentMsg)
douyin.receverDouyinShareMsg(contentMsg)
return ""
本以为到此就可以了,我高高兴兴地去设置客服了,但是发现,因为开启了api,不能设置了,所以还要通过api管理客服。
微信客服管理
可以参考微信客服管理
首先我们要先创建一个微信客服,但是发现微信客服的创建,必须要有一个图片! 所以只能
-
先上传图片。
def uploadTempImg():
getaccess()
url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=" + qiwei.QiWeiOut.access_token + "&type=image"
with open("././static/icon.jpeg", 'rb') as f:
size = f.__sizeof__()
print(size)
file = {'file': ("icon.jpeg", f, 'Content-Type: image/jpg')}
response = requests.post(url, files=file)
print(response.content)
传完图片,我们就继续
-
创建微信客服吧
def createCustomServer():
getaccess()
url = "https://qyapi.weixin.qq.com/cgi-bin/kf/account/add?access_token=" + qiwei.QiWeiOut.access_token
body = {
"name": "测试的客服帐号",
"media_id": "上一步打印出来的id"
}
response = requests.post(url, data=json.dumps(body))
result = response.content.decode('utf-8')
#将客服id打印出来
print(result)
jsonObj = json.loads(result)
if jsonObj['errcode'] == 0:
print("=====客服创建成功")
else:
print("=====客服创建失败")
完了之后,发现还是没有办法直接安排客服,需要
-
通过api安排客服
为了方便使用,创建一个客服链接,以后在任何地方使用,把链接丢过去就好了
def getCustomServerUrl():
getaccess()
url = "https://qyapi.weixin.qq.com/cgi-bin/kf/add_contact_way?access_token=" + qiwei.QiWeiOut.access_token
body = {
"open_kfid": "上一步打印出来的客服id",
"scene": "12345"
}
response = requests.post(url, data=json.dumps(body))
#打印客服链接
print(response.content)
然后把这个链接放到合适的位置,以后用户就可以直接使用了。