serverchan

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2021 License: MIT Imports: 8 Imported by: 1

README

go-serverchan

  • Unofficial serverchan client library in Golang.
ServerChan

Attention:

不要在text参数中传递引号、点、花括号等字符。因为微信的接口不支持一系列的特殊字符,但没有详细列表,所以我只简单的过滤掉了一些。 如果需要发送特殊字符,请放到 desp字段中。

每人每天发送上限500条,相同内容5分钟内不能重复发送,不同内容一分钟只能发送30条。主要是防止程序出错的情况。 注意,因为之前频繁的死循环程序导致费用飙升,现在每天调用接口超过1000次的用户将被系统自动拉黑。

Usage
package main

import (
	"context"
	"errors"
	"time"

	serverchan "github.com/Aoi-hosizora/go-serverchan"
)

func main() {
	client := serverchan.NewClient()

	err := client.Send("xxx", "test title", "test message")
	if err != nil {
		// ...
	}

	ok, err := client.CheckSckey("xxx", "check sckey")
	if err != nil {
		// ...
	} else if !ok {
		// ...
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()
	err = client.SendWithContext(ctx, "xxx", "test title", "test message")
	if errors.Is(err, context.DeadlineExceeded) {
		// ...
	}
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyTitle       = errors.New("serverchan: empty title")              // Empty title.
	ErrBadPushToken     = errors.New("serverchan: bad push token")           // Bad push token.
	ErrDuplicateMessage = errors.New("serverchan: duplicate message")        // Duplicate message.
	ErrNotSuccess       = errors.New("serverchan: respond with not success") // Not success, used when respond non-json or non-zero errno.
)

Functions

This section is empty.

Types

type Client

type Client struct{}

Client represents a serverchan client. Please visit http://sc.ftqq.com/3.version for details.

func NewClient

func NewClient() *Client

NewClient creates a default Client.

Example
client := serverchan.NewClient()

_ = client.Send(sckey, "test title", "test message")
_, _ = client.CheckSckey("xxx", "check sckey")
Output:

func (*Client) CheckSckey

func (c *Client) CheckSckey(sckey, title string) (bool, error)

CheckSckey sends a test message to serverchan to check if the sckey is valid.

Example
client := serverchan.NewClient()

ok, err := client.CheckSckey(sckey, "message for checking sckey")
if err != nil {
	log.Println("Failed to send message:", err)
} else if !ok {
	log.Println("Invalid sckey")
} else {
	log.Println("Valid sckey")
}
Output:

func (*Client) CheckSckeyWithContext added in v1.0.1

func (c *Client) CheckSckeyWithContext(ctx context.Context, sckey, title string) (bool, error)

CheckSckeyWithContext sends a test message to serverchan to check if the sckey is valid (with context).

Example
client := serverchan.NewClient()

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

ok, err := client.CheckSckeyWithContext(ctx, sckey, "message for checking sckey")
if errors.Is(err, context.DeadlineExceeded) {
	log.Println("Failed to send message: timeout")
} else if err != nil {
	log.Println("Failed to send message:", err)
} else if !ok {
	log.Println("Invalid sckey")
} else {
	log.Println("Valid sckey")
}
Output:

func (*Client) Send

func (c *Client) Send(sckey, title, message string) error

Send sends a message to serverchan using given sckey.

Example
client := serverchan.NewClient()

err := client.Send(sckey, "test title", "# test message\n## sub title\n+ item1\n+ [item2](https://www.google.co.jp)")
if err != nil {
	log.Println("Failed to send message:", err)
} else {
	log.Println("Success to send message")
}
Output:

func (*Client) SendWithContext added in v1.0.1

func (c *Client) SendWithContext(ctx context.Context, sckey, title, message string) error

SendWithContext sends a message to serverchan using given sckey (with context).

Example
client := serverchan.NewClient()

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

err := client.SendWithContext(ctx, sckey, "test title", "test message")
if errors.Is(err, context.DeadlineExceeded) {
	log.Println("Failed to send message: timeout")
} else if err != nil {
	log.Println("Failed to send message:", err)
} else {
	log.Println("Success to send message")
}
Output:

Jump to

Keyboard shortcuts

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