wecombot

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2026 License: MIT Imports: 17 Imported by: 0

README

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 Webhook URL
bot := wecombot.New("your-key",
    wecombot.WithBaseURL("https://custom-proxy.example.com/webhook"),
)

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

Documentation

Overview

Package wecombot provides a WeCom (WeChat Work) bot client. See: https://developer.work.weixin.qq.com/document/path/99110

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bot

type Bot struct {
	// contains filtered or unexported fields
}

Bot is a WeCom bot client.

func New

func New(key string, opts ...Option) *Bot

New creates a new Bot with the given key.

func (*Bot) Send

func (b *Bot) Send(msg interface{}) error

Send sends a message to the WeCom bot.

Example (Image)
package main

import (
	"log"

	wecombot "github.com/futuretea/go-wecom-bot"
	"github.com/futuretea/go-wecom-bot/image"
)

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

	// base64: base64 encoded image content
	// md5: md5 hash of original image
	msg := image.New("base64encodedstring", "md5hash")

	if err := bot.Send(msg); err != nil {
		log.Printf("Failed to send: %v", err)
	}
}
Example (Markdown)
package main

import (
	"log"

	wecombot "github.com/futuretea/go-wecom-bot"
	"github.com/futuretea/go-wecom-bot/markdown"
)

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

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

	if err := bot.Send(markdown.New(content)); err != nil {
		log.Printf("Failed to send: %v", err)
	}
}
Example (News)
package main

import (
	"log"

	wecombot "github.com/futuretea/go-wecom-bot"
	"github.com/futuretea/go-wecom-bot/news"
)

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

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

	if err := bot.Send(msg); err != nil {
		log.Printf("Failed to send: %v", err)
	}
}
Example (TemplateCard_newsNotice)
package main

import (
	"log"

	wecombot "github.com/futuretea/go-wecom-bot"
	"github.com/futuretea/go-wecom-bot/templatecard"
)

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

	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")

	if err := bot.Send(card); err != nil {
		log.Printf("Failed to send: %v", err)
	}
}
Example (TemplateCard_textNotice)
package main

import (
	"log"

	wecombot "github.com/futuretea/go-wecom-bot"
	"github.com/futuretea/go-wecom-bot/templatecard"
)

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

	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")

	if err := bot.Send(card); err != nil {
		log.Printf("Failed to send: %v", err)
	}
}
Example (Text)
package main

import (
	"log"

	wecombot "github.com/futuretea/go-wecom-bot"
	"github.com/futuretea/go-wecom-bot/text"
)

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

	msg := text.New("Hello, World!")
	if err := bot.Send(msg); err != nil {
		log.Printf("Failed to send: %v", err)
	}
}
Example (TextWithMention)
package main

import (
	"log"

	wecombot "github.com/futuretea/go-wecom-bot"
	"github.com/futuretea/go-wecom-bot/text"
)

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

	msg := text.New("Hello, @all!").
		WithMention("all", "userid1").
		WithMentionMobile("13800138000")

	if err := bot.Send(msg); err != nil {
		log.Printf("Failed to send: %v", err)
	}
}

func (*Bot) SendImage

func (b *Bot) SendImage(base64, md5 string) error

SendImage sends an image message.

func (*Bot) SendMarkdown

func (b *Bot) SendMarkdown(content string) error

SendMarkdown sends a markdown message.

Example
package main

import (
	"log"

	wecombot "github.com/futuretea/go-wecom-bot"
)

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

	if err := bot.SendMarkdown("## Alert\nSystem is **down**!"); err != nil {
		log.Printf("Failed to send: %v", err)
	}
}

func (*Bot) SendNews

func (b *Bot) SendNews(articles ...news.Article) error

SendNews sends a news message.

func (*Bot) SendTemplateCard

func (b *Bot) SendTemplateCard(card *templatecard.Card) error

SendTemplateCard sends a template card.

func (*Bot) SendText

func (b *Bot) SendText(content string) error

SendText sends a text message.

Example
package main

import (
	"log"

	wecombot "github.com/futuretea/go-wecom-bot"
)

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

	if err := bot.SendText("Hello, this is quick text!"); err != nil {
		log.Printf("Failed to send: %v", err)
	}
}

func (*Bot) UploadMedia

func (b *Bot) UploadMedia(filename string, data []byte) (*types.UploadedMedia, error)

UploadMedia uploads a file to WeCom server.

type Option

type Option func(*Bot)

Option configures a Bot.

func WithBaseURL

func WithBaseURL(url string) Option

WithBaseURL sets a custom webhook base URL for testing or proxy.

func WithDebug

func WithDebug() Option

WithDebug enables HTTP request/response debug logging. Useful for development and troubleshooting.

func WithHTTPClient

func WithHTTPClient(client httpclient.Client) Option

WithHTTPClient sets a custom HTTP client.

Directories

Path Synopsis
Package image provides image message support for WeCom bot.
Package image provides image message support for WeCom bot.
Package markdown provides markdown message support for WeCom bot.
Package markdown provides markdown message support for WeCom bot.
Package news provides news message support for WeCom bot.
Package news provides news message support for WeCom bot.
Package templatecard provides template card message support for WeCom bot.
Package templatecard provides template card message support for WeCom bot.
Package text provides text message support for WeCom bot.
Package text provides text message support for WeCom bot.
Package types provides common types and constants for WeCom bot messages.
Package types provides common types and constants for WeCom bot messages.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL