欢迎来到代码驿站!

Golang

当前位置:首页 > 脚本语言 > Golang

Go语言实现钉钉发送通知

时间:2020-12-20 11:53:54|栏目:Golang|点击:

具体代码如下所示:

package main
import (
  "bufio"
  "io"
  "fmt"
  "io/ioutil"
  "os/exec"
  "net/http"
  "os"
  "strings"
)
func GetKey() (timestamp string ,sign string, err error){
  cmd := exec.Command("/bin/python", "/application/scripts/hezhong_host_status/aaa.py")
  stdout , err := cmd.StdoutPipe()
  if err != nil {
   fmt.Println("Error:can not obtain stdout pipe for command:%s \n", err)
   return timestamp , sign, err
  }
  if err := cmd.Start() ; err != nil {
   fmt.Println("Error:The command is err ",err)
   return timestamp, sign,err
  }
  var Key []string
  //读取所有输出
  bytes := bufio.NewReader(stdout)
  for {
   line ,err:= bytes.ReadString('\n')
   if err == io.EOF{
     break
   }
   if err != nil {
     fmt.Println("Read Err:",err)
   }
   Key = append(Key,line)
  }
  if err := cmd.Wait();err!= nil{
   fmt.Println("Wait",err.Error())
   return timestamp, sign ,err
  }
  timestamp = Key[0]
  sign = Key[1]
  return timestamp ,sign ,nil
}
func SendDingMsg(msg string) {
  //请求地址模板
  timestamp , sign , err := GetKey()
  if err != nil {
   fmt.Println("Get Key Err:",err)
  }
  timestamp = strings.TrimRight(timestamp, "\n")
  sign = strings.TrimRight(sign, "\n")
  //timestamp := time.Now().UnixNano() / 1e6
  //timestamp := int64(1572870805748)
  //sign := "oJ5SHD3IwCWVIcL78k3pgX0tfQnjDfcDZMPXAI%2BvrFE%3D"
  webHook := "https://oapi.dingtalk.com/robot/send?access_token=628d11124aef5f9efe2a8c8a6b5afa2b67ab01dxxxxxxxxxxxxxxxxxxxxx&"+ "timestamp=" + timestamp + "&sign=" + sign
  content := `{"msgtype": "text",
   "text": {"content": "`+ msg + `"},
        "at": {
           "atMobiles": [
             "18301371817"
           ],
           "isAtAll": true
        }
  }`
  //创建一个请求
  req, err := http.NewRequest("POST", webHook, strings.NewReader(content))
  if err != nil {
   fmt.Println(err)
  }
  client := &http.Client{}
  //设置请求头
  req.Header.Set("Content-Type", "application/json")
  req.Header.Set("User-agent","firefox")
  //发送请求
  resp, err := client.Do(req)
  //关闭请求
  defer resp.Body.Close()
  fmt.Println(resp.StatusCode)
  body,_ := ioutil.ReadAll(resp.Body)
  fmt.Println(string(body))
  if err != nil {
   fmt.Println("handle error")
  }
}
func main(){
  SendDingMsg(os.Args[1])
}

辅助Python脚本:

#!python 2.7
import time
import hmac
import hashlib
import base64
import urllib
timestamp = long(round(time.time() * 1000))
secret = 'this is secret'
secret_enc = bytes(secret).encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = bytes(string_to_sign).encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.quote_plus(base64.b64encode(hmac_code))
print(timestamp)
print(sign)

总结

上一篇:go语言检测文件是否存在的方法

栏    目:Golang

下一篇:Go routine调度详解

本文标题:Go语言实现钉钉发送通知

本文地址:http://www.codeinn.net/misctech/34570.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有