公共方法

限定ip或者关键字发送群消息

function F_SEND_MARKDOWN
{
    local title='标题提示'
    local header=${1:-''}
    local content=${2:-''}
    local send_content=$(echo $content | sed 's#\(\\n\)#\1> - #g')
    local boby="$(
        cat <<EOF
{"msgtype": "markdown","markdown": {"title":"${title}","text": "### <font color=#E65100>${header}</font> \n---\n > - ${send_content} \n > - 项目:${DINGDING_KEYWORD}"},}
EOF
    )"
    echo "=========钉钉消息发送状态========="
    if [ -n "${title}" -a -n "${DINGDING_KEYWORD}" -a -n "${DINGDING_WEBHOOK}" ]; then
        /usr/bin/curl --data-ascii "${boby}" ${DINGDING_WEBHOOK} -H 'Content-Type: application/json'
    fi
    echo -e "\n=========消息状态打印完毕========="
}

DINGDING_WEBHOOK="https://oapi.dingtalk.com/robot/send?access_token=xxxxx"
DINGDING_KEYWORD="wtf"
F_SEND_MARKDOWN "标题" "下发的内容"

eg:自己琢磨mk语法 可定制 具体可看Git文档点击这!!

发送文本信息 python

pip install DingtalkChatbot 先安装完这玩意

#!/usr/bin/python3
import getopt
import sys
from dingtalkchatbot.chatbot import DingtalkChatbot


def usage():
    print('--- 使用错误: {} -s <secret> -w <webhook> -t <title> -v <verbose>'.format(sys.argv[0]))
    print('--- 或者: {} --title=<title> --verbose=<verbose>'.format(sys.argv[0]))


def format_text(title, verbose):
    text = '#### {title}\n\n'.format(title=title)
    if '客户端' in title or '开服时间' in title:
        split_verbose = verbose.split('\\n')
        for result in split_verbose:
            if result:
                text += "> - {}\n".format(result)
    else:
        text = '### {title}\n\n> - {verbose}'.format(title=title, verbose=verbose)
    return text


def run():
    title = ''
    verbose = ''
    secret = ''
    webhook = ''
    try:
        options, args = getopt.getopt(sys.argv[1:], "ht:v:s:w:", longopts=['help', 'secret=', 'webhook=',
                                                                           'title=', 'verbose='])
    except getopt.GetoptError:
        usage()
        sys.exit(3)
    if not (options or args):
        usage()
        sys.exit(3)
    for opt, arg in options:
        if opt in ('-h', '--help'):
            usage()
        if opt in ('-t', '--title'):
            title = arg
        if opt in ('-v', '--verbose'):
            verbose = arg
        if opt in ('-s', '--secret'):
            secret = arg
        if opt in ('-w', '--webhook'):
            webhook = arg

    if secret and webhook:
        robot = DingtalkChatbot(webhook, secret)
        text = format_text(title, verbose)
        robot.send_markdown(title='运维操作提示', text=text)


if __name__ == '__main__':
    run()

eg:使用

python3 /data/ops/script/ding_robot.py -t "标题!" -v "内容 -s "${ROBOT_SECRET:-}" -w "${ROBOT_WEBHOOK:-}"

待续.....

版权声明:如无特殊说明,文章均为本站原创,转载请注明出处

本文链接:http://kkxl95.cn/article/1607595864/