WeChatFerry/clients/gohttp/httpd/wcfrest/receiver.go

59 lines
1.1 KiB
Go
Raw Normal View History

2023-12-29 21:28:18 +08:00
package wcfrest
import (
"errors"
"github.com/opentdp/go-helper/logman"
"github.com/opentdp/go-helper/request"
"github.com/opentdp/wechat-rest/wcferry"
)
2024-01-12 09:02:19 +08:00
var urlReceiverStat = false
var urlReceiverList = map[string]bool{}
2023-12-29 21:28:18 +08:00
2024-01-12 09:02:19 +08:00
func (wc *Controller) enableUrlReceiver(url string) error {
2023-12-29 21:28:18 +08:00
2024-01-12 09:02:19 +08:00
if !urlReceiverStat {
2023-12-29 21:28:18 +08:00
err := wc.EnrollReceiver(true, func(msg *wcferry.WxMsg) {
2024-01-12 09:02:19 +08:00
ret := wcferry.ParseWxMsg(msg)
for url := range urlReceiverList {
2024-01-10 09:59:47 +08:00
logman.Info("forward msg", "url", url, "Id", ret.Id)
go request.JsonPost(url, ret, request.H{})
2023-12-29 21:28:18 +08:00
}
})
if err != nil {
return err
}
}
2024-01-12 09:02:19 +08:00
if _, ok := urlReceiverList[url]; ok {
2023-12-29 21:28:18 +08:00
return errors.New("url already exists")
}
2024-01-12 09:02:19 +08:00
urlReceiverStat = true
urlReceiverList[url] = true
2023-12-29 21:28:18 +08:00
return nil
}
2024-01-12 09:02:19 +08:00
func (wc *Controller) disableUrlReceiver(url string) error {
2023-12-29 21:28:18 +08:00
2024-01-12 09:02:19 +08:00
if _, ok := urlReceiverList[url]; !ok {
2023-12-29 21:28:18 +08:00
return errors.New("url not exists")
}
2024-01-12 09:02:19 +08:00
delete(urlReceiverList, url)
2023-12-29 21:28:18 +08:00
2024-01-12 09:02:19 +08:00
if len(urlReceiverList) == 0 {
2023-12-29 21:28:18 +08:00
if err := wc.DisableReceiver(false); err != nil {
return err
}
2024-01-12 09:02:19 +08:00
urlReceiverStat = false
2023-12-29 21:28:18 +08:00
}
return nil
}