mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
feat: notify by telegram while new memo create by HTTP (#2215)
* Inject telegram bot into API service * Add support for send telegram message * Send notification by telegram while new memo post
This commit is contained in:
@ -11,8 +11,10 @@ import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/usememos/memos/api/auth"
|
||||
"github.com/usememos/memos/common/log"
|
||||
"github.com/usememos/memos/common/util"
|
||||
"github.com/usememos/memos/store"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Visibility is the type of a visibility.
|
||||
@ -351,6 +353,38 @@ func (s *APIV1Service) CreateMemo(c echo.Context) error {
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo response").SetInternal(err)
|
||||
}
|
||||
|
||||
// send notification by telegram bot if memo is not Private
|
||||
if memoResponse.Visibility != Private {
|
||||
// fetch all telegram UserID
|
||||
userSettings, err := s.Store.ListUserSettings(ctx, &store.FindUserSetting{Key: UserSettingTelegramUserIDKey.String()})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to ListUserSettings").SetInternal(err)
|
||||
}
|
||||
for _, userSetting := range userSettings {
|
||||
// parse telegram UserID setting value into a int64
|
||||
var tgUserIDStr string
|
||||
err := json.Unmarshal([]byte(userSetting.Value), &tgUserIDStr)
|
||||
if err != nil {
|
||||
log.Error("failed to parse Telegram UserID", zap.Error(err))
|
||||
continue
|
||||
}
|
||||
|
||||
tgUserID, err := strconv.ParseInt(tgUserIDStr, 10, 64)
|
||||
if err != nil {
|
||||
log.Error("failed to parse Telegram UserID", zap.Error(err))
|
||||
continue
|
||||
}
|
||||
|
||||
// send notification to telegram
|
||||
content := memoResponse.CreatorName + " Says:\n\n" + memoResponse.Content
|
||||
_, err = s.telegramBot.SendMessage(ctx, tgUserID, content)
|
||||
if err != nil {
|
||||
log.Error("Failed to send Telegram notification", zap.Error(err))
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
return c.JSON(http.StatusOK, memoResponse)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user