zoukankan      html  css  js  c++  java
  • python 小脚本升级-- 钉钉群聊天机器人

     

     

     

    一则小脚本(工作中用) 在这篇文章中写的监控的脚本,发送监控的时候 是利用的邮箱,其实在实际,邮箱查收有着不方便性,于是乎升级,

       我们工作中,经常用钉钉,那么如果要是能用到钉钉多好,这样我们的监控成功后直接发送给我们的钉钉,简单方便

    在钉钉开发文档有这么一个地方 ,自定义机器人  

    这个应该能够满足我们的办公要求吧,先来实验实验,打开钉钉,选择机器人管理。

    使用自定义的机器人,点击

     选择群,选择生成的机器人

     然后创建,

    这样我们的机器人创建成功,我们去看着文档的要求去,可惜官方没有python版本,我这里就用python大胆去调试去测试,代码如下

    import  requests,json
    url='h#机器人对应的Webhook地址b'
    pagrem={
    "msgtype": "text",
        "text": {
            "content": "服务器出错了!"
         },
    }
    headers={
    'Content-Type': 'application/json'
    }
    f=requests.post(url,data=json.dumps(pagrem),headers=headers)

    这个时候我去我创建机器人的群去看看我的机器人是否完成了这项工作

    完成,完成,功能是妥妥的没有问题了,

    那么接下来就是来升级我之前写的脚本了。

    # -*- coding: utf-8 -*-
    # @Date    : 2017-09-25 08:50:17
    # @Author  : lileilei 
    import requests ,json,datetime,time
    def send_message(content):
        url = '#机器人对应的Webhook地址'
        pagrem = {
            "msgtype": "text",
            "text": {
                "content": content
            },
            "isAtAll": True
        }
        headers = {
            'Content-Type': 'application/json'
        }
        f = requests.post(url, data=json.dumps(pagrem), headers=headers)
    def appindex():
        url='#监控url'
        m=requests.get(url)
        if int(m.status_code) !=200:
            send_message(content="app首页不能正常加载!时间是:%s"%datetime.datetime.now())
            print(u' app shouye is failed')
        else:
            print(u'app首页可以正常访问')
    def index():
        url='#监控url''
        index=requests.get(url)
        if int(index.status_code) !=200:
            send_message(content="官网首页不能正常加载!时间是:%s"%datetime.datetime.now())
            print(u'guanwangshouye is failed')
        else:
        
            print(u'官网首页可以正常访问')
    def applogin():
        url='#监控url''
        data={
        参数
        }
        applogin=requests.post(url,data)
        if int(applogin.status_code) !=200:
            send_message(content="APP登录不能正常加载!时间是:%s" % datetime.datetime.now())
            print(u' app login is can not  to login')
        else:
            print(u'app登录可以正常访问')
    if __name__ == '__main__':
        while True:
            try:
                applogin()
                index()
                appindex()
                time.sleep(60)
            except Exception as e:
                print(e)
                continue

    然后,去调试去吧,

    测试完毕,脚本可以更新了,。

    这样一则小的python发送钉钉群的监控脚本就这么完工了。其实其他的用法也是类比。

  • 相关阅读:
    《ElasticSearch6.x实战教程》之准备工作、基本术语
    《ElasticSearch6.x实战教程》正式推出
    【好书推荐】《剑指Offer》之硬技能(编程题12~16)
    synchronized凭什么锁得住?
    synchronized到底锁住的是谁?
    Java面试宝典(2020版)
    50道Redis面试题及答案整理,史上最全!
    MySQL面试题及答案整理,史上最全!
    Github 上优秀的 Java 项目推荐
    100道MySQL常见面试题总结
  • 原文地址:https://www.cnblogs.com/leiziv5/p/8135082.html
Copyright © 2011-2022 走看看