Beispiel #1
0
    def send(self, ev):
        for user in ev['users']:
            corp_id = user.get('wechat_corpid') or self.conf['corpid']
            secret = user.get('wechat_secret') or self.conf['secret']
            agent_id = user.get('wechat_agentid') or self.conf['agentid']

            touser = user.get('wechat', '')
            toparty = user.get('wechat_party', '')

            if not (touser or toparty):
                continue

            text = ev['text']
            text = text if len(text) < 500 else "[消息太长了,无法从微信发出]"
            msg = '%s[P%s] %s\n' % (
                status2emoji(ev['status']),
                ev['level'],
                ev['title'],
            ) + text

            payload = {
                "touser": touser,
                "toparty": toparty,
                "msgtype": "text",
                "agentid": agent_id,
                "text": {
                    "content": msg,
                },
                "safe": 0,
            }

            self.logger.info('Sending wechat message to %s %s', touser,
                             toparty)

            for _ in range(2):
                token = self.get_access_token(corp_id, secret)
                if not token:
                    return

                resp = requests.post(
                    'https://qyapi.weixin.qq.com/cgi-bin/message/send',
                    params={'access_token': token},
                    headers={'Content-Type': 'application/json'},
                    timeout=10,
                    data=json.dumps(payload),
                )

                if not resp.ok:
                    raise Exception(resp.content)

                ret = resp.json()
                if ret['errcode'] == 40014:
                    self.clear_access_token(corp_id, secret)
                    continue
                elif ret['errcode'] != 0:
                    raise Exception(resp.content)

                break
            else:
                raise Exception('Too many retries')
Beispiel #2
0
def wechat_qy(conf, user, event):
    corp_id  = user.get('wechat_corpid') or conf['corpid']
    secret   = user.get('wechat_secret') or conf['secret']
    agent_id = user.get('wechat_agentid') or conf['agentid']

    touser = user.get('wechat', '')
    toparty = user.get('wechat_party', '')

    if not (touser or toparty):
        return

    msg = u'%s[P%s] %s\n' % (
        status2emoji(event['status']),
        event['level'],
        event['title'],
    ) + event['text']

    payload = {
        "touser": touser,
        "toparty": toparty,
        "msgtype": "text",
        "agentid": agent_id,
        "text": {
            "content": msg,
        },
        "safe": 0,
    }

    log.info('Sending wechat message to %s %s', touser, toparty)

    for _ in xrange(2):
        token = get_access_token(corp_id, secret)
        if not token:
            return

        resp = requests.post(
            'https://qyapi.weixin.qq.com/cgi-bin/message/send',
            params={'access_token': token},
            headers={'Content-Type': 'application/json'},
            timeout=10,
            data=json.dumps(payload).decode('unicode-escape').encode('utf-8'),
        )

        if not resp.ok:
            raise Exception(resp.content)

        ret = resp.json()
        if ret['errcode'] == 40014:
            clear_access_token(corp_id, secret)
            continue
        elif ret['errcode'] != 0:
            raise Exception(resp.content)

        break
    else:
        raise Exception('Too many retries')
Beispiel #3
0
def wechat_qy(conf, user, event):
    corp_id = user.get('wechat_corpid') or conf['corpid']
    secret = user.get('wechat_secret') or conf['secret']
    agent_id = user.get('wechat_agentid') or conf['agentid']

    touser = user.get('wechat', '')
    toparty = user.get('wechat_party', '')

    if not (touser or toparty):
        return

    msg = u'%s[P%s]\n%s\n' % (
        status2emoji(event['status']),
        event['level'],
        event['title'],
    ) + event['text']

    payload = {
        "touser": touser,
        "toparty": toparty,
        "msgtype": "text",
        "agentid": agent_id,
        "text": {
            "content": msg,
        },
        "safe": 0,
    }

    log.info('Sending wechat message to %s %s', touser, toparty)

    for _ in xrange(2):
        token = get_access_token(corp_id, secret)
        if not token:
            return

        resp = requests.post(
            'https://qyapi.weixin.qq.com/cgi-bin/message/send',
            params={'access_token': token},
            headers={'Content-Type': 'application/json'},
            timeout=10,
            data=json.dumps(payload).decode('unicode-escape').encode('utf-8'),
        )

        if not resp.ok:
            raise Exception(resp.content)

        ret = resp.json()
        if ret['errcode'] == 40014:
            clear_access_token(corp_id, secret)
            continue
        elif ret['errcode'] != 0:
            raise Exception(resp.content)

        break
    else:
        raise Exception('Too many retries')
Beispiel #4
0
    def send(self, user, event):
        if not user.get('email'):
            return

        subject = u'%s[P%s]%s' % (
            status2emoji(event['status']),
            event['level'],
            event['title'],
        )

        send_mail(
            self.conf['send_from'], user['email'],
            subject, event['text'],
            server=self.conf['server'],
            username=self.conf['username'],
            password=self.conf['password'],
        )
Beispiel #5
0
def smtp(conf, user, event):
    if not user['email']:
        return

    subject = u'%s[P%s]%s' % (
        status2emoji(event['status']),
        event['level'],
        event['title'],
    )

    send_mail(
        conf['send_from'], user['email'],
        subject, event['text'],
        server=conf['server'],
        username=conf['username'],
        password=conf['password'],
    )
Beispiel #6
0
def yunpian_sms(conf, user, event):
    if not user.get('phone'):
        return

    msg = u'【%s】%s[P%s] %s\n' % (
        conf['signature'],
        status2emoji(event['status']),
        event['level'],
        event['title'],
    ) + event['text']

    rst = requests.post('http://yunpian.com/v1/sms/send.json', data={
        'apikey': conf['api_key'],
        'mobile': user['phone'],
        'text': msg,
    }).json()

    if rst['code'] != 0:
        raise Exception(rst['detail'])
Beispiel #7
0
def yunpian_sms(conf, user, event):
    if not user.get('phone'):
        return

    msg = u'【%s】%s[P%s]\n%s\n' % (
        conf['signature'],
        status2emoji(event['status']),
        event['level'],
        event['title'],
    ) + event['text']

    rst = requests.post('http://yunpian.com/v1/sms/send.json',
                        data={
                            'apikey': conf['api_key'],
                            'mobile': user['phone'],
                            'text': msg,
                        }).json()

    if rst['code'] != 0:
        raise Exception(rst['detail'])
Beispiel #8
0
    def send(self, users, event):
        for user in users:
            if not user.get('email'):
                continue

            subject = u'%s[P%s]%s' % (
                status2emoji(event['status']),
                event['level'],
                event['title'],
            )

            send_mail(
                self.conf['send_from'],
                user['email'],
                subject,
                event['text'],
                server=self.conf['server'],
                ssl=self.conf.get('ssl', False),
                username=self.conf['username'],
                password=self.conf['password'],
            )
Beispiel #9
0
    def send(self, ev):
        for user in ev['users']:
            if not user.get('phone'):
                continue

            msg = '【%s】%s[P%s] %s\n' % (
                self.conf['signature'],
                status2emoji(ev['status']),
                ev['level'],
                ev['title'],
            ) + ev['text']

            rst = requests.post('http://yunpian.com/v1/sms/send.json',
                                data={
                                    'apikey': self.conf['api_key'],
                                    'mobile': user['phone'],
                                    'text': msg,
                                }).json()

            if rst['code'] != 0:
                raise Exception(rst['detail'])