Golang给微信个人账号发送消息的方法

最近在用Golang开发一个需要对接微信个人账号的项目,想实现自动发送消息的功能。请问有没有成熟的方案或者开源库可以实现这个需求?需要支持文本消息和可能的图文消息发送,最好能提供完整的代码示例。另外想了解微信官方是否允许这种自动化操作,会不会被封号?如果通过网页版微信实现的话,稳定性如何?

2 回复

目前Golang无法直接给微信个人账号发送消息,因为微信官方未开放个人账号的API。但可通过以下间接方法实现:

  1. 企业微信机器人(推荐) 通过企业微信创建群聊机器人,使用Golang调用webhook发送消息:

    package main
    import (
        "bytes"
        "encoding/json"
        "net/http"
    )
    func sendWechatWork(msg string) {
        url := "企业微信机器人webhook地址"
        body, _ := json.Marshal(map[string]interface{}{
            "msgtype": "text",
            "text":    map[string]string{"content": msg},
        })
        http.Post(url, "application/json", bytes.NewBuffer(body))
    }
    
  2. Server酱等第三方服务 通过Golang调用第三方推送服务的API,将消息转发到微信。

  3. 微信测试号(需关注公众号) 通过微信公众平台测试号,用户关注后可通过模板消息接收通知。

注意:所有方法均需用户主动配置(如添加机器人/关注公众号),无法直接向任意微信用户发送消息。建议优先使用企业微信方案,稳定且免费。

更多关于Golang给微信个人账号发送消息的方法的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


在Golang中给微信个人账号发送消息,通常有几种方法:

1. 使用企业微信API(推荐)

通过企业微信应用向微信个人账号发送消息(需用户关注企业微信)

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "net/http"
)

type WeComMessage struct {
    ToUser  string `json:"touser"`
    MsgType string `json:"msgtype"`
    Text    struct {
        Content string `json:"content"`
    } `json:"text"`
    AgentID int `json:"agentid"`
}

func SendWeComMessage(corpID, corpSecret, toUser, message string, agentID int) error {
    // 1. 获取access_token
    tokenURL := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s", corpID, corpSecret)
    
    resp, err := http.Get(tokenURL)
    if err != nil {
        return err
    }
    defer resp.Body.Close()
    
    var tokenResp struct {
        AccessToken string `json:"access_token"`
    }
    if err := json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil {
        return err
    }
    
    // 2. 发送消息
    msg := WeComMessage{
        ToUser:  toUser,
        MsgType: "text",
        AgentID: agentID,
    }
    msg.Text.Content = message
    
    msgData, _ := json.Marshal(msg)
    sendURL := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s", tokenResp.AccessToken)
    
    _, err = http.Post(sendURL, "application/json", bytes.NewBuffer(msgData))
    return err
}

func main() {
    corpID := "your_corp_id"
    corpSecret := "your_corp_secret"
    toUser := "user_wxid"
    message := "Hello from Golang!"
    agentID := 1000001
    
    err := SendWeComMessage(corpID, corpSecret, toUser, message, agentID)
    if err != nil {
        fmt.Println("发送失败:", err)
    } else {
        fmt.Println("发送成功")
    }
}

2. 使用Server酱等第三方服务

func SendServerChanMessage(sckey, title, message string) error {
    url := fmt.Sprintf("https://sctapi.ftqq.com/%s.send", sckey)
    
    data := map[string]string{
        "title": title,
        "desp":  message,
    }
    
    jsonData, _ := json.Marshal(data)
    _, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
    return err
}

3. 使用微信测试号

通过微信公众平台测试号发送模板消息

注意事项:

  1. 企业微信方法需要:

    • 注册企业微信
    • 创建应用
    • 用户关注企业微信
    • 获取corp_id、secret、agent_id
  2. 第三方服务通常有发送频率限制

  3. 直接给个人微信发送消息官方没有开放API,需要通过上述间接方式实现

建议使用企业微信方案,相对稳定可靠,且支持多种消息类型(文本、图片、文件等)。

回到顶部