feat(contactLogic): 添加通过HTTP API发送Telegram消息的备用方案
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 6m2s
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 6m2s
当TelegramBot未初始化时,改用HTTP API发送消息。这提高了功能的可靠性,确保在没有直接bot实例时仍能发送通知。
This commit is contained in:
parent
e42a5b80bf
commit
b3edd7e2a6
@ -3,6 +3,7 @@ package common
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -29,9 +30,6 @@ func NewContactLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ContactLo
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *ContactLogic) SubmitContact(req *types.ContactRequest) error {
|
func (l *ContactLogic) SubmitContact(req *types.ContactRequest) error {
|
||||||
if l.svcCtx.TelegramBot == nil {
|
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "telegram bot not initialized")
|
|
||||||
}
|
|
||||||
chatIDStr := l.svcCtx.Config.Telegram.GroupChatID
|
chatIDStr := l.svcCtx.Config.Telegram.GroupChatID
|
||||||
if chatIDStr == "" {
|
if chatIDStr == "" {
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "telegram group chat id not configured")
|
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "telegram group chat id not configured")
|
||||||
@ -55,13 +53,33 @@ func (l *ContactLogic) SubmitContact(req *types.ContactRequest) error {
|
|||||||
notes = escapeMarkdown(notes)
|
notes = escapeMarkdown(notes)
|
||||||
|
|
||||||
text := fmt.Sprintf("新的联系/合作信息\n称呼:%s\n邮箱:%s\n其他联系方式:%s\n优势/备注:%s", name, email, other, notes)
|
text := fmt.Sprintf("新的联系/合作信息\n称呼:%s\n邮箱:%s\n其他联系方式:%s\n优势/备注:%s", name, email, other, notes)
|
||||||
msg := tgbotapi.NewMessage(chatID, text)
|
if l.svcCtx.TelegramBot != nil {
|
||||||
msg.ParseMode = "markdown"
|
msg := tgbotapi.NewMessage(chatID, text)
|
||||||
_, err = l.svcCtx.TelegramBot.Send(msg)
|
msg.ParseMode = "markdown"
|
||||||
|
_, err = l.svcCtx.TelegramBot.Send(msg)
|
||||||
|
if err != nil {
|
||||||
|
l.Errorw("send telegram message failed", logger.Field("error", err.Error()))
|
||||||
|
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "send telegram message failed: %v", err.Error())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
token := l.svcCtx.Config.Telegram.BotToken
|
||||||
|
if strings.TrimSpace(token) == "" {
|
||||||
|
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "telegram bot not initialized")
|
||||||
|
}
|
||||||
|
reqHttp, _ := http.NewRequest("GET", "https://api.telegram.org/bot"+token+"/sendMessage", nil)
|
||||||
|
q := reqHttp.URL.Query()
|
||||||
|
q.Add("chat_id", chatIDStr)
|
||||||
|
q.Add("text", text)
|
||||||
|
q.Add("parse_mode", "markdown")
|
||||||
|
reqHttp.URL.RawQuery = q.Encode()
|
||||||
|
resp, err := http.DefaultClient.Do(reqHttp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Errorw("send telegram message failed", logger.Field("error", err.Error()))
|
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "send telegram message failed: %v", err.Error())
|
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "send telegram message failed: %v", err.Error())
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "send telegram message failed: http %d", resp.StatusCode)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user