【Go】-调用企微机器人

没有什么好讲的,直接贴代码,消息格式我用的markdown。

sendMsg.go:

go 复制代码
package main

import (
	"flag"
	"fmt"
	"github.com/goccy/go-json"
	"github.com/tidwall/gjson"
	"io/ioutil"
	"net/http"
	"strings"
)

func main() {

	// params
	var msg string
	flag.StringVar(&msg, "msg", "null", "空消息")
	flag.Parse()
	type Params struct {
		Msgtype  string `json:"msgtype"`
		Markdown struct {
			Content string `json:"content"`
		} `json:"markdown"`
	}
	params := Params{
		Msgtype: "markdown",
		Markdown: struct {
			Content string `json:"content"`
		}{
			Content: msg,
		},
	}
	//fmt.Printf("%v\n", params)
	json_date, err := json.Marshal(&params)
	if err != nil {
		fmt.Sprintf("序列化失败:%s", err)
		fmt.Println()
	}
	//fmt.Printf("序列化后:%v\n", string(json_date))

	// POST
	var wx_send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxx"
	//fmt.Println(wx_send_url)
	response, err := http.Post(wx_send_url, "text/html;application/json", strings.NewReader(string(json_date)))
	if err != nil {
		fmt.Println(err)
	}

	// Get errcode
	body, err := ioutil.ReadAll(response.Body)
	defer response.Body.Close()
	errcode := gjson.Get(string(body), "errcode").String()
	if errcode != "0" {
		fmt.Println("fail")
		//fmt.Println(json.Unmarshal([]byte(str), &body))
	} else {
		fmt.Println("success")
	}
}

交叉编译参数的设置:

win 编译生成Linux下的可执行文件:

go 复制代码
go env -w CGO_ENABLED=0
go env -w GOOS=linux
go env -w GOARCH=amd64

编译完记得改回来:

go 复制代码
go env -w CGO_ENABLED=0
go env -w GOOS=windows
go env -w GOARCH=amd64
相关推荐
捧 花32 分钟前
Go Web 开发流程
开发语言·后端·golang·restful·web·分层设计
南猿北者33 分钟前
go语言基础语法
开发语言·后端·golang
万俟淋曦2 小时前
【论文速递】2025年第34周(Aug-17-23)(Robotics/Embodied AI/LLM)
人工智能·深度学习·机器学习·ai·机器人·论文·具身智能
芝麻开门-新起点2 小时前
具身机器人的软件系统架构
系统架构·机器人
落霞的思绪3 小时前
基于Go开发的矢量瓦片服务器——pg_tileserv
开发语言·后端·golang
卿雪3 小时前
Redis 数据持久化:RDB和 AOF 有什么区别?
java·数据库·redis·python·mysql·缓存·golang
巴塞罗那的风3 小时前
经典Agent架构实战之反思模型(Reflection)
后端·语言模型·golang
modest_laowang3 小时前
推前映射(Push forward)和拉回映射(Pull back)
机器人·微分几何
万俟淋曦3 小时前
【论文速递】2025年第32周(Aug-03-09)(Robotics/Embodied AI/LLM)
人工智能·深度学习·机器人·大模型·论文·robotics·具身智能
倔强菜鸟3 小时前
2025.11.21-GO语言入门(一)
开发语言·后端·golang