Documentation
¶
Index ¶
Constants ¶
View Source
const ( // DefaultBaseURL PushPlus 发送接口默认地址 DefaultBaseURL = "https://www.pushplus.plus/send" // DefaultTimeout 默认请求超时时间 DefaultTimeout = 10 * time.Second // ChannelWeChat 微信公众号渠道 ChannelWeChat = "wechat" // ChannelWebhook webhook 渠道 ChannelWebhook = "webhook" // ChannelCP 企业微信应用渠道 ChannelCP = "cp" // ChannelMail 邮箱渠道 ChannelMail = "mail" // ChannelSMS 短信渠道 ChannelSMS = "sms" // ChannelVoice 语音渠道 ChannelVoice = "voice" // TemplateHTML HTML 模板 TemplateHTML = "html" // TemplateJSON JSON 模板 TemplateJSON = "json" // TemplateMarkdown Markdown 模板 TemplateMarkdown = "markdown" // TemplateTxt 纯文本模板 TemplateTxt = "txt" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client PushPlus 推送客户端,创建后字段只读,可被多个 goroutine 并发调用。
func New ¶
func New(conf jcbaseGo.PushPlusStruct) *Client
New 创建 PushPlus 推送客户端。
参数:
- conf: 推送配置。Token 必填;Topic、Channel、Template、Timeout、BaseURL 为空时分别回落为无群组、wechat、html、10s、DefaultBaseURL。
返回值:
- *Client: 可复用的客户端实例,内部持有一个带超时的 http.Client。
异常:
- 不返回错误;Token 为空的问题在调用 Send / SendContext 时才会暴露。
使用示例:
client := pushplus.New(jcbaseGo.PushPlusStruct{
Token: "your-token",
Topic: "daily-report",
})
resp, err := client.Send(pushplus.Message{
Title: "部署完成",
Content: "v1.0.0 已上线",
})
func (*Client) Send ¶
Send 使用默认上下文发送一条推送消息。
参数:
- msg: 推送消息,为空字段回落到客户端默认值。
返回值:
- *Response: 接口返回的业务信息;业务失败时也会返回,便于读取 code 与 msg。
- error: 请求失败、响应解析失败或业务 code 非 200 时返回;业务失败可通过 errors.As 提取 *APIError。
异常:
- 不 panic;所有失败均以 error 返回。
使用示例:
resp, err := client.Send(pushplus.Message{
Title: "告警",
Content: "磁盘使用率超过 90%",
})
注意:
- code=200 仅表示 PushPlus 已受理,实际投递是异步的,不代表已送达。
func (*Client) SendContext ¶
SendContext 在指定上下文中发送一条推送消息,可通过 ctx 取消请求。
参数:
- ctx: 请求上下文,不能为 nil。
- msg: 推送消息,为空字段回落到客户端默认值。
返回值:
- *Response: 接口返回的业务信息;业务失败时也会返回,便于读取 code 与 msg。
- error: 上下文取消、请求失败、响应解析失败或业务 code 非 200 时返回; 业务失败可通过 errors.As 提取 *APIError。
异常:
- 不 panic;ctx 为 nil 时由 net/http 触发 panic,调用方须自行保证非 nil。
使用示例:
ctx, cancel := context.WithTimeout(c.Request.Context(), 3*time.Second)
defer cancel()
_, err := client.SendContext(ctx, pushplus.Message{Content: "任务完成"})
type Message ¶
type Message struct {
Title string // 消息标题
Content string // 消息正文,格式由 Template 决定
Topic string // 群组编码,为空时使用客户端默认群组
Channel string // 发送渠道,为空时使用客户端默认渠道
Template string // 消息模板,为空时使用客户端默认模板
Webhook string // webhook 渠道的接收地址
CallbackURL string // 推送结果回调地址
}
Message 单条推送消息,字段为空时回落到客户端默认值
Click to show internal directories.
Click to hide internal directories.