dingding.py 1.0 KB

1234567891011121314151617181920212223242526272829
  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. SIM_CARD_EXPIRE_URL = "https://oapi.dingtalk.com/robot/send?access_token=6e0a9a36924477a9bbb60dd4abd2a0c957ff489d4d19621ccf3b533656258a67"
  10. class DingDingRobot(object):
  11. def __init__(self, url = _DINGDING_TALK_URL):
  12. self.url = url
  13. def send_msg(self, msg):
  14. try:
  15. data = {"msgtype": "text", "text": {"content": force_text(msg)}}
  16. headers = {'Content-Type': 'application/json;charset=UTF-8'}
  17. send_data = json.dumps(data).encode('utf-8')
  18. response = requests.post(url = self.url, data = send_data, headers = headers, timeout = 15)
  19. logger.debug(response.text)
  20. except Exception as e:
  21. logger.exception(e)