1. #coding:utf-8
    2. import requests
    3. import json
    4. def dingmessage():
    5. # 请求的URLWebHook地址
    6. webhook = "https://oapi.dingtalk.com/robot/send?access_token=????"
    7. #构建请求头部
    8. header = {
    9. "Content-Type": "application/json",
    10. "Charset": "UTF-8"
    11. }
    12. #构建请求数据
    13. # tex = "#上班注意安全,不要迟到"
    14. message = {
    15. # Text 格式 ...
    16. # "msgtype": "markdown",
    17. # "markdown": {
    18. # "content": tex
    19. # },
    20. # "at": {
    21. # "isAtAll": True
    22. # }
    23. # {
    24. # markdown 格式 ...
    25. "msgtype": "markdown",
    26. "markdown": {
    27. "title":"杭州天气",
    28. "text": "#### 杭州天气 @156xxxx8827\n" +
    29. "> 9度,西北风1级,空气良89,相对温度73%\n\n" +
    30. "> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)\n" +
    31. "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n"
    32. },
    33. "at": {
    34. "atMobiles": [
    35. "156xxxx8827",
    36. "189xxxx8325"
    37. ],
    38. "isAtAll": True
    39. }
    40. }
    41. #对请求的数据进行json封装
    42. message_json = json.dumps(message)
    43. #发送请求
    44. info = requests.post(url=webhook,data=message_json,headers=header)
    45. #打印返回的结果
    46. print(info.text)
    47. if __name__=="__main__":
    48. dingmessage()