zoukankan      html  css  js  c++  java
  • 企业微信报警脚本

    申请公众号

    1、进入公众号申请平台,选择企业微信,按照步骤填写信息,注册完毕后登陆企业微信,点击应用管理,选择创建应用

    2、所有圈起来的都是必填项,

    3、创建完成后,会自动跳转到当前所创建应用的管理界面,AgentIDSecret需要记录下来,额外还要记录企业ID(点击我的企业下面有企业ID)

    4、上面步骤企业微信号全部都已经完成了,接下来要进行脚本编写

    脚本代码

    package main
    
    import (
    	"bytes"
    	"encoding/json"
    	"flag"
    	"fmt"
    	"io/ioutil"
    	"net/http"
    	"os"
    )
    
    
    // 企业微信报警
    
    var (
    	h bool
    	user string
    	msg string
    )
    
    func init(){
    	flag.BoolVar(&h, "h", false, "this help")
    	flag.StringVar(&user, "user", "You want to send a message to that enterprise WeChat user or group", "")
    	flag.StringVar(&msg, "msg", "", "The content of the message you want to send")
    }
    
    type wc struct {
    	agentId int
    	companyId string
    	secretId string
    	wc_url string
    }
    func newWc(agentId int,companyId,secretId,wc_url string) *wc {
    	wl := &wc{
    		agentId:  agentId,
    		companyId: companyId,
    		secretId:  secretId,
    		wc_url:    wc_url,
    	}
    	return wl
    }
    
    func (w *wc) getToken()interface{}{
    	client := &http.Client{}
    	url := fmt.Sprintf("%s?corpid=%s&corpsecret=%s",w.wc_url,w.companyId,w.secretId)
    	data,err := http.NewRequest("GET",url,nil)
    	if err != nil{
    		panic("获取token失败")
    	}
    	resp,err := client.Do(data)
    
    	body,err := ioutil.ReadAll(resp.Body)
    	// 处理获取到的token
    	var jsonObj interface{}
    	err = json.Unmarshal(body, &jsonObj)
    	if err != nil {
    		panic("转换json格式失败")
    	}
    	token := jsonObj.(map[string]interface{})["access_token"]
    	return token
    }
    
    func (w *wc) sendMsg(to_user,msg string){
    	token := w.getToken()
    	msgsend_url :="https://qyapi.weixin.qq.com/cgi-bin/message/send"
    	url := fmt.Sprintf("%s?access_token=%v",msgsend_url,token)
    	fmt.Println("获取到token",token)
    
    	// 携带token并发送消息至对应的企业微信
    	data := fmt.Sprintf(`{
    		"touser":"%v",
    		"msgtype":"text",
    		"agentid":%v,
    		"text":{
    			"content": "%v" 
    		}
    		}`, to_user,w.agentId,msg)
    	var jsonStr = []byte(data)
    	req,err := http.NewRequest("POST",url,bytes.NewBuffer(jsonStr))
    	req.Header.Set("Content-Type", "application/json")
    
    	if err != nil{
    		panic("发送消息失败")
    	}
    	client := &http.Client{}
    	resp, err := client.Do(req)
    	body,err := ioutil.ReadAll(resp.Body)
    	var jsonObj interface{}
    	err = json.Unmarshal(body, &jsonObj)
    	if err != nil {
    		panic("转换json格式失败")
    	}
    	fmt.Println(string(body))
    	ok := jsonObj.(map[string]interface{})["errmsg"]
    	code := jsonObj.(map[string]interface{})["errcode"]
    	//fmt.Printf("%T,%T",ok,code)
    	if ok == "ok" && code == float64(0) {
    		fmt.Println("消息发送成功")
    	}else {
    		fmt.Println("消息发送失败")
    	}
    
    }
    
    func main(){
    	flag.Parse()
    	if user == "" || msg == ""{
    		fmt.Println("参数不能为空")
    		os.Exit(1)
    	}
    	agentId := 12312312
    	companyId := "xxx"
    	secretId := "xxxx"
    	wx_url := "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
    	p := newWc(agentId,companyId,secretId,wx_url)
    	p.sendMsg(user,msg)
    }
    

    使用脚本

    1.先编译

    go build
    

    2.再使用

    ./脚本名 -user 发送给那个用户 -msg 发送什么消息
    
  • 相关阅读:
    持续集成之Jenkins+sonar自动化部署04
    自动化部署03
    自动化部署02
    自动化部署01
    持续集成之代码质量管理-Sonar
    gitlab-ce安装01
    在Centos7.3安装Zabbix3.4.4服务端
    自动化运维之日志系统上线规范(十)
    自动化运维之日志系统Logstash实践(九)
    自动化运维之日志系统Logstash解耦实践(八)
  • 原文地址:https://www.cnblogs.com/jasonminghao/p/12349861.html
Copyright © 2011-2022 走看看