FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

futuretea/go-wecom-bot: 企业微信群机器人API golang sdk · GitHub

Repository files navigation

go-wecom-bot

A WeCom (WeChat Work) bot client library for Go, using go-http-client for HTTP operations.

API Documentation

Installation

go get github.com/futuretea/go-wecom-bot

Quick Start

package main

import (
    "log"
    "github.com/futuretea/go-wecom-bot"
    "github.com/futuretea/go-wecom-bot/text"
)

func main() {
    // Create bot instance
    bot := wecombot.New("your-bot-key")

    // Send text message
    msg := text.New("Hello, WeCom Bot!")
    if err := bot.Send(msg); err != nil {
        log.Fatal(err)
    }
}

Message Types

Text Message

import "github.com/futuretea/go-wecom-bot/text"

// Simple text
msg := text.New("Hello, World!")

// Mention users
msg.WithMention("userid1", "userid2", "all")

// Mention by mobile
msg.WithMentionMobile("13800138000")

// Chaining
msg := text.New("Hello @all").
    WithMention("all").
    WithMentionMobile("13800138000")

Markdown Message

import "github.com/futuretea/go-wecom-bot/markdown"

content := `## System Notification
**Time**: 2024-01-15 10:30:00
**Level**: Warning
[View Details](https://example.com)`

msg := markdown.New(content)

Image Message

import "github.com/futuretea/go-wecom-bot/image"

// base64: base64 encoded image content
// md5: md5 hash of the original image content
msg := image.New(base64EncodedString, md5Hash)

News Message

import "github.com/futuretea/go-wecom-bot/news"

msg := news.New().
    AddArticle("Title 1", "Description 1", "https://example.com/1", "https://example.com/pic1.jpg").
    AddArticle("Title 2", "Description 2", "https://example.com/2", "https://example.com/pic2.jpg")

Template Card Message

Text Notice

import "github.com/futuretea/go-wecom-bot/templatecard"

card := templatecard.NewTextNotice().
    WithSource("https://example.com/icon.png", "WeCom", templatecard.SourceDescColorBlue).
    WithMainTitle("Onboarding Approval", "You have a pending approval").
    WithEmphasisContent("100", "Pending Count").
    WithSubTitle("Please process in time").
    WithCardAction(templatecard.ActionTypeURL, "https://work.weixin.qq.com").
    AddHorizontalContent("Applicant", "John Doe", templatecard.HorizontalContentTypeText).
    AddJump(templatecard.JumpTypeURL, "View Details", "https://work.weixin.qq.com")

News Notice

import "github.com/futuretea/go-wecom-bot/templatecard"

card := templatecard.NewNewsNotice().
    WithSource("https://example.com/icon.png", "WeCom", templatecard.SourceDescColorGreen).
    WithMainTitle("WeCom Update", "New template card feature").
    WithCardImage("https://example.com/news.jpg", 1.3).
    WithCardAction(templatecard.ActionTypeURL, "https://work.weixin.qq.com")

Convenience Methods

// Quick text
bot.SendText("Hello!")

// Quick markdown
bot.SendMarkdown("## Title\nContent")

// Quick image
bot.SendImage(base64, md5)

// Quick news
bot.SendNews(news.Article{Title: "...", Description: "...", URL: "...", PicURL: "..."})

// Quick template card
bot.SendTemplateCard(card)

File Upload

media, err := bot.UploadMedia("filename.pdf", fileBytes)
if err != nil {
    log.Fatal(err)
}
// Use media.MediaID to reference the file in template cards

Advanced Configuration

Custom HTTP Client

import (
    httpclient "github.com/futuretea/go-http-client"
)

config := &httpclient.Config{
    BaseURL: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send",
    Timeout: 10 * time.Second,
}

client := httpclient.NewClient(config,
    httpclient.WithRetry(3, 200*time.Millisecond, 10*time.Second),
)

bot := wecombot.New("your-key", wecombot.WithHTTPClient(client))

Debug Mode

Enable HTTP request/response logging for troubleshooting:

bot := wecombot.New("your-key", wecombot.WithDebug())

Output example:

> POST /cgi-bin/webhook/send?key=xxx HTTP/1.1
> Content-Type: application/json
>
{"msgtype":"text","text":{"content":"Hello"}}

< HTTP/1.1 200 OK
< Content-Type: application/json
<
{"errcode":0,"errmsg":"ok"}

Custom Send Webhook URL

WithBaseURL keeps the existing behavior: it sets the complete send webhook endpoint only.

bot := wecombot.New("your-key",
    wecombot.WithBaseURL("https://custom-proxy.example.com/webhook"),
)

Custom API Root URL

Use WithAPIBaseURL when an internal proxy exposes both WeCom webhook APIs under one root. The library appends the send and upload paths, preserving any path prefix in the root.

bot := wecombot.New("your-key",
    wecombot.WithAPIBaseURL("https://custom-proxy.example.com/wecom"),
)

WithAPIBaseURL controls both message sends and file uploads. Existing WithHTTPClient behavior remains unchanged: it applies to message sends only.

Project Structure

go-wecom-bot/
├── bot.go              # Main Bot client
├── types/              # Common types
├── text/               # Text message
├── markdown/           # Markdown message
├── image/              # Image message
├── news/               # News message
└── templatecard/       # Template card message

License

MIT License

About

企业微信群机器人API golang sdk

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages


Back | FazBrowse Home | New Git URL