Go语言实现钉钉发送通知
作者:蓝叶子Sheep 时间:2024-05-08 10:44:29
具体代码如下所示:
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语言实现钉钉发送通知网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://blog.51cto.com/dellinger/2447790
标签:go,钉钉,通知
0
投稿
猜你喜欢
Python实现html转换为pdf报告(生成pdf报告)功能示例
2023-11-07 02:31:17
树型结构列出指定目录里所有文件的PHP类
2023-11-17 04:49:04
Python对小数进行除法运算的正确方法示例
2021-06-19 08:42:18
python用quad、dblquad实现一维二维积分的实例详解
2022-02-17 05:32:51
探讨如何计算age的sql函数
2024-01-21 12:44:31
实现文字放大效果Javascript源码
2010-03-17 20:46:00
python format 格式化输出方法
2023-12-24 16:26:19
Python爬虫之获取心知天气API实时天气数据并弹窗提醒
2023-04-17 14:40:58
Python切片操作去除字符串首尾的空格
2023-08-08 19:19:21
使用PyCharm安装pytest及requests的问题
2023-01-17 13:10:46
基于python traceback实现异常的获取与处理
2022-04-05 09:59:32
Google谷歌农历鼠年春节的变化
2008-02-11 17:07:00
python使用三角迭代计算圆周率PI的方法
2021-12-29 06:47:02
python实现博客文章爬虫示例
2022-06-30 08:20:40
看看那些名牌LOGO的成长史
2009-03-24 20:37:00
Python 如何实现文件自动去重
2021-07-16 13:50:54
Python 实现3种回归模型(Linear Regression,Lasso,Ridge)的示例
2021-06-17 20:46:53
php在windows环境下获得cpu内存实时使用率(推荐)
2023-11-15 04:44:23
使用Python打造一款间谍程序的流程分析
2021-11-21 08:12:32
layabox2.0命令行在vscode中编译打包问题
2022-08-27 02:23:20