dingding.py 807 B

123456789101112131415161718192021222324
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import logging
  4. import simplejson as json
  5. import requests
  6. from django.utils.encoding import force_text
  7. logger = logging.getLogger(__name__)
  8. _DINGDING_TALK_URL = 'https://oapi.dingtalk.com/robot/send?access_token=acb44458217bd584bd3c29129c7ac6e59b6484314b441374b8b79dab9181f7ee'
  9. class DingDingRobot(object):
  10. def __init__(self, url=_DINGDING_TALK_URL):
  11. self.url = url
  12. def send_msg(self, msg):
  13. data = {"msgtype": "text", "text": {"content": force_text(msg)}}
  14. headers = {'Content-Type': 'application/json;charset=UTF-8'}
  15. send_data = json.dumps(data).encode('utf-8')
  16. response = requests.post(url=self.url, data=send_data, headers=headers, timeout=15)
  17. logger.debug(response.text)