明霞山资源网 Design By www.htccd.com

这个是作者自己封装的一个钉钉机器人的包,目前只支持发文本格式、链接格式、markdown格式的消息,我们可以在很多场景用到这个,比如告警通知等

安装

pip install DingDingBot

使用方法

from DingDingBot.DDBOT import DingDing
# 初始话DingDingBOt webhook是钉钉机器人所必须的
dd = DingDing(webhook='https://oapi.dingtalk.com/robot/send"# 测试数据\n" + "> testone", Title='测试数据'))

源码

#!/usr/bin/python
# -*- coding: UTF-8 -*-

'''
  @@@@@@@@   @@@@@@@@@   @@@@@@@@@  @@@@@@@@@   @@@@@@@@@@@@
  @@   @@  @@   @@  @@   @@  @@   @@     @@
  @@    @@ @@    @@  @@  @@  @@    @@    @@
  @@    @@ @@    @@  @@  @@   @@    @@    @@
  @@    @@ @@    @@  @@ @@   @@    @@    @@
  @@   @@  @@   @@  @@ @@    @@    @@    @@
  @@   @@  @@   @@   @@ @@   @@    @@    @@
  @@  @@   @@  @@   @@  @@   @@    @@    @@
  @@  @@   @@  @@    @@ @@    @@   @@     @@
  @@ @@    @@ @@     @@      @@@@@@@@@     @@

'''

import requests, json


class DingDing():
  """
  # 钉钉官方文档
  Refer to official documentation: https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
  """
  # 初始化
  def __init__(self, webhook):
    self.webhook = webhook
    self.session = requests.session()
    self.session.headers = {"Content-Type": "application/json;charset=utf-8"}

  def Send_Text_Msg(self, Content: str, atMobiles: list = [], isAtAll: bool = False) -> dict:
    """
    :param content: 要发送的内容
    :param atMobiles: @指定的人,这里必须是列表,且参数为手机号
    :param isAtAll: @全体成员
    :return:
    """
    try:
      data = {
        "msgtype": "text",
        "text": {
          "content": Content
        },
        "at": {
          "atMobiles": atMobiles,
          "isAtAll": isAtAll
        }
      }
      response = self.session.post(self.webhook, data=json.dumps(data))
      if response.status_code == '200':
        result = {"status": True, "message": "Message has been sent"}
        return result
      else:
        return response.text
    except Exception as error:
      result = {"status": False, "message": f"Failed to send message,Error stack:{error}"}
      return result

  def Send_Link_Msg(self, Content: str, Title: str, MsgUrl: str, PicUrl: str = ''):
    """
    :param Content: 链接的内容
    :param title: 链接的标题
    :param MsgUrl: 待跳转页面的url
    :param PicUrl: 消息所展示的图片
    :return:
    """
    try:
      data = {
        "msgtype": "link",
        "link": {
          "text": Content,
          "title": Title,
          "picUrl": PicUrl,
          "messageUrl": MsgUrl
        }
      }
      response = self.session.post(self.webhook, data=json.dumps(data))
      if response.status_code == '200':
        result = {"status": True, "message": "Message has been sent"}
        return result
      else:
        return response.text
    except Exception as error:
      result = {"status": False, "message": f"Failed to send message,Error stack:{error}"}
      return result

  def Send_MardDown_Msg(self, Content: str, Title: str, atMobiles: list = [], isAtAll: bool = False):
    """
    :param Content: Markdown格式的文本,仅支持下面的格式
    '''
    标题
      # 一级标题
      ## 二级标题
      ### 三级标题
      #### 四级标题
      ##### 五级标题
      ###### 六级标题

      引用
      > A man who stands for nothing will fall for anything.

      文字加粗、斜体
      **bold**
      *italic*

      链接
      [this is a link](http://name.com)

      图片
      ![](http://name.com/pic.jpg)

      无序列表
      - item1
      - item2

      有序列表
      1. item1
      2. item2
    '''
    :param Title: 这个Markdown的标题
    :param atMobiles: @指定的人,这里必须是列表,且参数为手机号
    :param isAtAll: @全体成员
    :return:
    """
    try:
      data = {
        "msgtype": "markdown",
        "markdown": {
          "title": Title,
          "text": Content
        },
        "at": {
          "atMobiles": atMobiles,
          "isAtAll": isAtAll
        }
      }
      response = self.session.post(self.webhook, data=json.dumps(data))
      if response.status_code == '200':
        result = {"status": True, "message": "Message has been sent"}
        return result
      else:
        return response.text
    except Exception as error:
      result = {"status": False, "message": f"Failed to send message,Error stack:{error}"}
      return result

标签:
Python,DingDingBot,Python钉钉第三方包

明霞山资源网 Design By www.htccd.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
明霞山资源网 Design By www.htccd.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?