Ejemplo n.º 1
0
class WxEntry(EntryBase):
    def __init__(self):

        self.wxclient = Client('appid_xxxxxxxxxxxxxxx',
                               'appsecret_xxxxxxxxxxxxxx')
        self.robot = None
        self.subscribe_auto_msg = None

        super(WxEntry, self).__init__()

    def send_text(self, openid, text):
        try:
            self.wxclient.send_text_message(openid, text)
        except ClientException as e:
            raise exceptions.UserError(u'发送失败 %s' % e)

    def chat_send(self, uuid, msg):
        openid = self.get_openid_from_uuid(uuid)
        if openid:
            self.send_text(openid, msg)

    def upload_media(self, media_type, media_file):
        try:
            return self.wxclient.upload_media(media_type, media_file)
        except ClientException as e:
            raise exceptions.UserError(u'image上传失败 %s' % e)

    def send_image_message(self, openid, media_id):
        try:
            self.wxclient.send_image_message(openid, media_id)
        except ClientException as e:
            raise exceptions.UserError(u'发送image失败 %s' % e)

    def send_image(self, uuid, media_id):
        openid = self.get_openid_from_uuid(uuid)
        if openid:
            self.send_image_message(openid, media_id)

    def send_voice(self, uuid, media_id):
        openid = self.get_openid_from_uuid(uuid)
        if openid:
            try:
                self.wxclient.send_voice_message(openid, media_id)
            except ClientException as e:
                raise exceptions.UserError(u'发送voice失败 %s' % e)

    def create_reply(self, ret_msg, message):
        if type(ret_msg) == dict:
            if ret_msg.get('media_type') == 'news':
                self.wxclient.send_news_message(message.source,
                                                ret_msg['media_id'])
            return None
        else:
            return create_reply(ret_msg, message=message)

    def init(self, env):
        dbname = env.cr.dbname
        global WxEnvDict
        if dbname in WxEnvDict:
            del WxEnvDict[dbname]
        WxEnvDict[dbname] = self

        try:
            config = env['wx.config'].sudo().get_cur()
            action = config.action
        except:
            import traceback
            traceback.print_exc()
            action = None
        if action:
            self.subscribe_auto_msg = config.action.get_wx_reply()

        Param = env['ir.config_parameter'].sudo()
        self.wx_token = Param.get_param('wx_token') or ''
        self.wx_appid = Param.get_param('wx_appid') or ''
        self.wx_AppSecret = Param.get_param('wx_AppSecret') or ''

        #robot.config["TOKEN"] = self.wx_token
        self.wxclient.appid = self.wx_appid
        self.wxclient.appsecret = self.wx_AppSecret

        try:
            # 刷新 AccessToken
            self.wxclient._token = None
            _ = self.wxclient.token
        except:
            import traceback
            traceback.print_exc()
            _logger.error(u'初始化微信客户端token失败,请在微信对接配置中填写好相关信息!')

        session_storage = MemoryStorage()
        robot = WeRoBot(token=self.wx_token,
                        enable_session=True,
                        logger=_logger,
                        session_storage=session_storage)
        enable_pretty_logging(robot.logger)
        self.robot = robot

        try:
            users = env['wx.user'].sudo().search([('last_uuid', '!=', None)])
            for obj in users:
                if obj.last_uuid_time:
                    self.recover_uuid(
                        obj.openid, obj.last_uuid,
                        fields.Datetime.from_string(obj.last_uuid_time))
        except:
            env.cr.rollback()
            import traceback
            traceback.print_exc()

        print('wx client init: %s %s' % (self.OPENID_UUID, self.UUID_OPENID))
Ejemplo n.º 2
0
# Signature 验证不通过时显示的错误页面
@wx_robot.error_page
def make_error_page(url):
    return "<h1>喵喵喵 %s 不是给麻瓜访问的快走开</h1>" % url


# Handler
# #################################################################
@wx_robot.handler
def hello(message, session):
    print("handler:", type(message), type(session))
    print(message.__type__)
    return '不支持消息类型:{}'.format(type(message))


if __name__ == '__main__':
    # Client
    # #################################################################
    c = Client(
        config={
            "APP_ID": "wx8df663f95f624ddc",
            "APP_SECRET": "9af6b8495185cfc6c4b5cea7e8bf7f07"
        })

    res = c.get_followers(first_user_id=None)
    print(res)
    c.send_text_message(user_id="oBCxywNx_nthmopfu914jLSVuobU", content="你好")

    pass
Ejemplo n.º 3
0
class WxEntry(object):
    def __init__(self):

        self.wxclient = Client('appid_xxxxxxxxxxxxxxx',
                               'appsecret_xxxxxxxxxxxxxx')

        self.UUID_OPENID = {}

        # 微信用户客服消息的会话缓存
        self.OPENID_UUID = {}

        self.robot = None

    def send_text(self, openid, text):
        try:
            self.wxclient.send_text_message(openid, text)
        except ClientException as e:
            raise exceptions.UserError(u'发送失败 %s' % e)

    def chat_send(self, db, uuid, msg):
        #_dict = self.UUID_OPENID.get(db,None)
        if self.UUID_OPENID:
            openid = self.UUID_OPENID.get(uuid, None)
            if openid:
                self.send_text(openid, msg)
        return -1

    def init(self, env):
        dbname = env.cr.dbname
        global WxEnvDict
        if dbname in WxEnvDict:
            del WxEnvDict[dbname]
        WxEnvDict[dbname] = self

        Param = env['ir.config_parameter'].sudo()
        self.wx_token = Param.get_param('wx_token') or ''
        self.wx_appid = Param.get_param('wx_appid') or ''
        self.wx_AppSecret = Param.get_param('wx_AppSecret') or ''

        #robot.config["TOKEN"] = self.wx_token
        self.wxclient.appid = self.wx_appid
        self.wxclient.appsecret = self.wx_AppSecret

        try:
            # 刷新 AccessToken
            self.wxclient._token = None
            _ = self.wxclient.token
        except:
            import traceback
            traceback.print_exc()
            _logger.error(u'初始化微信客户端token失败,请在微信对接配置中填写好相关信息!')

        session_storage = MemoryStorage()
        robot = WeRoBot(token=self.wx_token,
                        enable_session=True,
                        logger=_logger,
                        session_storage=session_storage)
        enable_pretty_logging(robot.logger)
        self.robot = robot

        users = env['wx.user'].sudo().search([('last_uuid', '!=', None)])
        for obj in users:
            self.OPENID_UUID[obj.openid] = obj.last_uuid
            self.UUID_OPENID[obj.last_uuid] = obj.openid
        print('wx client init: %s %s' % (self.OPENID_UUID, self.UUID_OPENID))