DDR爱好者之家 Design By 杰米

具体代码如下所示:

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"+ "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语言实现钉钉发送通知,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。