132 lines
4.4 KiB
Python
132 lines
4.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
import logging
|
|
import json
|
|
import requests
|
|
from odoo import api, fields, models, _
|
|
|
|
URL_ROOT = 'https://qyapi.weixin.qq.com'
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class WeComApi(models.Model):
|
|
_name = "wecom.api"
|
|
_description = "企业微信的请求api"
|
|
|
|
name = fields.Char("api名称", required=True)
|
|
category = fields.Selection(
|
|
[
|
|
("1", "基础"),
|
|
("2", "通讯录"),
|
|
("3", "客户联系"),
|
|
("4", "微信客服"),
|
|
("5", "身份验证"),
|
|
("6", "应用管理"),
|
|
("7", "消息推送"),
|
|
("8", "媒体素材"),
|
|
("9", "OA"),
|
|
("10", "效率工具"),
|
|
("11", "企业支付"),
|
|
("12", "企业互联"),
|
|
("13", "企业链,上下游"),
|
|
("14", "会话内容存档"),
|
|
("15", "电子发票"),
|
|
("16", "家校沟通"),
|
|
("17", "家校应用"),
|
|
("18", "政民沟通"),
|
|
],
|
|
string="请求分类",
|
|
default="1",
|
|
)
|
|
function = fields.Char("请求方法", required=True, readonly=True)
|
|
request_url = fields.Char("请求地址", required=True)
|
|
|
|
request_type = fields.Selection(
|
|
[("GET", "GET"), ("POST", "POST")],
|
|
string="请求类型",
|
|
required=True,
|
|
default="GET",
|
|
)
|
|
|
|
sequence = fields.Integer(default=0)
|
|
|
|
_sql_constraints = [
|
|
("function_uniq", "unique (function)", "请求方法唯一 !"),
|
|
]
|
|
|
|
def http_request_url(self, data, access_token=None, app_name=None):
|
|
"""
|
|
api请求方法
|
|
:param data: 请求数据
|
|
:param access_token: 密钥
|
|
:param app_name: 密钥的app名称
|
|
:return: 请求结果
|
|
"""
|
|
self.ensure_one()
|
|
request_url = self.request_url
|
|
# 替换ACCESS_TOKEN
|
|
if "ACCESS_TOKEN" in request_url:
|
|
if not access_token:
|
|
access_token = self.env['auth.oauth.provider'].search([('name', '=', app_name)], limit=1).scope
|
|
request_url = request_url.replace("ACCESS_TOKEN", access_token)
|
|
# 拼接root组装url
|
|
url = URL_ROOT + request_url
|
|
# 根据请求类型请求信息
|
|
if self.request_type == 'POST':
|
|
result = requests.post(url=url, data=json.dumps(data))
|
|
else:
|
|
result = requests.get(url=url, params=data)
|
|
return result
|
|
|
|
def get_app_info(self, app_name):
|
|
"""
|
|
获取应用的信息
|
|
:param app_name:
|
|
:return:
|
|
"""
|
|
wx_work_auth = self.env['auth.oauth.provider'].search([('name', '=', app_name)], limit=1)
|
|
wechat_company_data = wx_work_auth.client_id.split(' ')
|
|
corp_id = wechat_company_data[0]
|
|
agent_id = wechat_company_data[1]
|
|
app_secret = wechat_company_data[2]
|
|
return corp_id, agent_id, app_secret
|
|
|
|
def get_access_token(self, corp_id, app_secret):
|
|
"""
|
|
获取密钥
|
|
:param corp_id:
|
|
:param app_secret:
|
|
:return: access_token
|
|
"""
|
|
wx_work_access_token_api = self.env['wecom.api'].search([('function', '=', 'GET_ACCESS_TOKEN')], limit=1)
|
|
response_result = wx_work_access_token_api.http_request_url({"corpid": corp_id, "corpsecret": app_secret})
|
|
if response_result.status_code != 200:
|
|
_logger.error('获取token异常,异常:{0}'.format(str(response_result.json())))
|
|
# 获取返回的信息
|
|
response = json.loads(response_result.content)
|
|
access_token = response.get("access_token", None)
|
|
return access_token
|
|
|
|
def send_text_message(self, user_id, message):
|
|
"""
|
|
企业微信发送消息
|
|
:param user_id: 企业微信用户id
|
|
:param message: 消息文本
|
|
:return: 发送结果
|
|
"""
|
|
corp_id, agent_id, app_secret = self.get_app_info('Wechat Send')
|
|
access_token = self.get_access_token(corp_id, app_secret)
|
|
if not user_id or not agent_id or not message:
|
|
return False
|
|
data = {
|
|
'touser': user_id,
|
|
'msgtype': 'text',
|
|
'agentid': agent_id,
|
|
'text': {
|
|
'content': message
|
|
}
|
|
}
|
|
wx_work_send_api = self.env['wecom.api'].search([('function', '=', 'SEND_MESSAGE')], limit=1)
|
|
response = wx_work_send_api.http_request_url(data, access_token)
|
|
return response
|