golang Pushover API 集成与消息推送插件库pushover的使用
Golang Pushover API 集成与消息推送插件库pushover的使用
介绍
pushover是一个用Go语言编写的Superblock Pushover API封装库。它提供了一种便捷的方式,只需几行代码就能从Go程序中发送通知。
消息发送
发送简单消息
这是一个向接收者发送通知的简单示例。接收者可以是用户或群组,它们都使用通知令牌。
package main
import (
"log"
"github.com/gregdel/pushover"
)
func main() {
// 使用token创建一个新的pushover应用
app := pushover.New("uQiRzpo4DXghDmr9QzzfQu27cmVRsG")
// 创建一个新的接收者
recipient := pushover.NewRecipient("gznej3rKEVAvPUxu9vvNnqpmZpokzF")
// 创建要发送的消息
message := pushover.NewMessage("Hello !")
// 向接收者发送消息
response, err := app.SendMessage(message, recipient)
if err != nil {
log.Panic(err)
}
// 如果需要可以打印响应
log.Println(response)
}
发送带标题的消息
有一种简单的方法可以创建带标题的消息。可以使用pushover.NewMessageWithTitle而不是pushover.NewMessage。
message := pushover.NewMessageWithTitle("My awesome message", "My title")
发送花式消息
如果您想要更详细的消息,仍然可以这样做。
message := &pushover.Message{
Message: "My awesome message",
Title: "My title",
Priority: pushover.PriorityEmergency,
URL: "http://google.com",
URLTitle: "Google",
Timestamp: time.Now().Unix(),
Retry: 60 * time.Second,
Expire: time.Hour,
DeviceName: "SuperDevice",
CallbackURL: "http://yourapp.com/callback",
Sound: pushover.SoundCosmic,
}
发送带附件的消息
您可以随消息一起发送图片附件。
file, err := os.Open("/some/image.png")
if err != nil {
panic(err)
}
defer file.Close()
message := pushover.NewMessage("Hello !")
if err := message.AddAttachment(file); err != nil {
panic(err)
}
回调和收据
如果您使用紧急通知,则必须指定重试周期和过期延迟。您可以使用消息响应中的令牌获取收据详细信息。
...
response, err := app.SendMessage(message, recipient)
if err != nil {
log.Panic(err)
}
receiptDetails, err := app.GetReceiptDetails(response.Receipt)
if err != nil {
log.Panic(err)
}
fmt.Println("Acknowledged status :", receiptDetails.Acknowledged)
您也可以在过期时间之前取消紧急通知。
response, err := app.CancelEmergencyNotification(response.Receipt)
if err != nil {
log.Panic(err)
}
用户验证
如果您想验证接收者令牌是否有效。
...
recipientDetails, err := app.GetRecipientDetails(recipient)
if err != nil {
log.Panic(err)
}
fmt.Println(recipientDetails)
更多关于golang Pushover API 集成与消息推送插件库pushover的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于golang Pushover API 集成与消息推送插件库pushover的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
Golang Pushover API 集成与消息推送
Pushover 是一个流行的消息推送服务,允许开发者向用户的移动设备和桌面发送通知。下面我将介绍如何在 Go 中集成 Pushover API,并使用 github.com/gregdel/pushover
库来实现消息推送。
安装 Pushover 库
首先安装 Go 的 Pushover 客户端库:
go get github.com/gregdel/pushover
基本使用示例
1. 初始化 Pushover 客户端
package main
import (
"log"
"github.com/gregdel/pushover"
)
func main() {
// 替换为你的 Pushover API Token
appToken := "your-app-token"
// 创建 Pushover 应用客户端
app := pushover.New(appToken)
// 替换为目标用户的 User Key
recipient := pushover.NewRecipient("your-user-key")
// 创建消息
message := &pushover.Message{
Message: "这是一条测试消息",
Title: "Go 推送测试",
Priority: pushover.PriorityNormal,
Sound: pushover.SoundMagic,
}
// 发送消息
response, err := app.SendMessage(message, recipient)
if err != nil {
log.Fatalf("发送消息失败: %v", err)
}
log.Printf("消息发送成功! 请求ID: %s", response.Request)
}
2. 发送带附件的高级消息
func sendMessageWithAttachment() {
app := pushover.New("your-app-token")
recipient := pushover.NewRecipient("your-user-key")
// 创建消息
message := &pushover.Message{
Message: "这条消息包含附件",
Title: "带附件的推送",
Priority: pushover.PriorityHigh,
URL: "https://example.com",
URLTitle: "点击访问示例网站",
}
// 添加附件 (可以是文件路径或 []byte)
attachment, err := pushover.NewAttachmentFromBytes([]byte("附件内容"), "note.txt")
if err != nil {
log.Fatalf("创建附件失败: %v", err)
}
// 发送带附件的消息
response, err := app.SendMessage(message, recipient, attachment)
if err != nil {
log.Fatalf("发送消息失败: %v", err)
}
log.Printf("带附件的消息发送成功! 请求ID: %s", response.Request)
}
3. 发送紧急消息并获取回执
func sendEmergencyMessage() {
app := pushover.New("your-app-token")
recipient := pushover.NewRecipient("your-user-key")
// 创建紧急消息
message := &pushover.Message{
Message: "这是一条紧急消息!",
Title: "紧急通知",
Priority: pushover.PriorityEmergency,
Retry: 60, // 60秒后重试
Expire: 3600, // 1小时后过期
CallbackURL: "https://your-callback-url.com/ack",
}
response, err := app.SendMessage(message, recipient)
if err != nil {
log.Fatalf("发送紧急消息失败: %v", err)
}
log.Printf("紧急消息已发送! 请求ID: %s", response.Request)
// 如果需要检查回执状态
receipt, err := app.GetReceipt(response.Receipt)
if err != nil {
log.Printf("获取回执失败: %v", err)
} else {
log.Printf("回执状态: %s, 最后确认于: %s", receipt.Status, receipt.AcknowledgedAt)
}
}
封装为可重用组件
下面是一个更完整的封装示例,可以作为项目中的通知模块:
package notifier
import (
"errors"
"github.com/gregdel/pushover"
)
type PushoverNotifier struct {
app *pushover.Pushover
recipient *pushover.Recipient
}
func NewPushoverNotifier(appToken, userKey string) *PushoverNotifier {
return &PushoverNotifier{
app: pushover.New(appToken),
recipient: pushover.NewRecipient(userKey),
}
}
func (n *PushoverNotifier) SendNotification(title, message string, priority int) error {
if title == "" || message == "" {
return errors.New("标题和消息内容不能为空")
}
msg := &pushover.Message{
Message: message,
Title: title,
Priority: priority,
}
_, err := n.app.SendMessage(msg, n.recipient)
return err
}
func (n *PushoverNotifier) SendWithOptions(opts NotificationOptions) error {
msg := &pushover.Message{
Message: opts.Message,
Title: opts.Title,
Priority: opts.Priority,
URL: opts.URL,
URLTitle: opts.URLTitle,
Sound: opts.Sound,
Retry: opts.Retry,
Expire: opts.Expire,
CallbackURL: opts.CallbackURL,
}
_, err := n.app.SendMessage(msg, n.recipient)
return err
}
type NotificationOptions struct {
Message string
Title string
Priority int
URL string
URLTitle string
Sound string
Retry int
Expire int
CallbackURL string
}
使用示例
func main() {
notifier := notifier.NewPushoverNotifier("your-app-token", "your-user-key")
// 发送简单通知
err := notifier.SendNotification("系统警报", "CPU使用率超过90%", pushover.PriorityHigh)
if err != nil {
log.Printf("发送通知失败: %v", err)
}
// 发送带选项的通知
opts := notifier.NotificationOptions{
Message: "数据库备份已完成",
Title: "备份成功",
Priority: pushover.PriorityNormal,
URL: "https://dashboard.example.com/backups",
URLTitle: "查看备份详情",
Sound: pushover.SoundCosmic,
}
err = notifier.SendWithOptions(opts)
if err != nil {
log.Printf("发送通知失败: %v", err)
}
}
注意事项
- 你需要先在 Pushover 网站 注册应用并获取 API Token
- 每个用户需要注册 Pushover 并获取他们的 User Key
- 免费账户每月有 10,000 条消息的限制
- 紧急优先级消息会持续提醒用户直到确认
- 考虑在发送失败时实现重试逻辑
以上代码提供了 Pushover API 的基本集成方法,你可以根据项目需求进一步扩展和定制。