memos/server/telegram.go
Athurg Gooth 8628d1e4b2
feat: add Telegram bot config UI (#1747)
* Add retry wait for telegram.GetUpdates

* Add support to set telegram robot token from UI

* Change validator of UserSettingTelegramUserID

* Add support to set telegram user id from UI

* Fix typescript check

* Add validator for SystemSettingTelegramRobotTokenName

* Optimize error notice while config telegram params

* Change for review

* Fix telegram user id could not be empty

* Fix telegram robot could not be empty

* Fix for eslint (again)

* Update web/src/components/Settings/SystemSection.tsx

---------

Co-authored-by: Athurg Feng <athurg@gooth.org>
Co-authored-by: boojack <stevenlgtm@gmail.com>
2023-05-26 19:16:51 +08:00

108 lines
2.7 KiB
Go

package server
import (
"context"
"encoding/json"
"fmt"
"path"
"strconv"
"github.com/usememos/memos/api"
"github.com/usememos/memos/common"
"github.com/usememos/memos/plugin/telegram"
"github.com/usememos/memos/store"
)
type telegramHandler struct {
store *store.Store
}
func newTelegramHandler(store *store.Store) *telegramHandler {
return &telegramHandler{store: store}
}
func (t *telegramHandler) RobotToken(ctx context.Context) string {
return t.store.GetSystemSettingValueOrDefault(&ctx, api.SystemSettingTelegramRobotTokenName, "")
}
func (t *telegramHandler) MessageHandle(ctx context.Context, message telegram.Message, blobs map[string][]byte) error {
var creatorID int
userSettingList, err := t.store.FindUserSettingList(ctx, &api.UserSettingFind{
Key: api.UserSettingTelegramUserIDKey,
})
if err != nil {
return fmt.Errorf("Fail to find memo user: %s", err)
}
for _, userSetting := range userSettingList {
var value string
if err := json.Unmarshal([]byte(userSetting.Value), &value); err != nil {
continue
}
if value == strconv.Itoa(message.From.ID) {
creatorID = userSetting.UserID
}
}
if creatorID == 0 {
return fmt.Errorf("Please set your telegram userid %d in UserSetting of Memos", message.From.ID)
}
// create memo
memoCreate := api.CreateMemoRequest{
CreatorID: creatorID,
Visibility: api.Private,
}
if message.Text != nil {
memoCreate.Content = *message.Text
}
if blobs != nil && message.Caption != nil {
memoCreate.Content = *message.Caption
}
memoMessage, err := t.store.CreateMemo(ctx, convertCreateMemoRequestToMemoMessage(&memoCreate))
if err != nil {
return fmt.Errorf("failed to CreateMemo: %s", err)
}
if err := createMemoCreateActivity(ctx, t.store, memoMessage); err != nil {
return fmt.Errorf("failed to createMemoCreateActivity: %s", err)
}
// create resources
for filename, blob := range blobs {
// TODO support more
mime := "application/octet-stream"
switch path.Ext(filename) {
case ".jpg":
mime = "image/jpeg"
case ".png":
mime = "image/png"
}
resourceCreate := api.ResourceCreate{
CreatorID: creatorID,
Filename: filename,
Type: mime,
Size: int64(len(blob)),
Blob: blob,
PublicID: common.GenUUID(),
}
resource, err := t.store.CreateResource(ctx, &resourceCreate)
if err != nil {
return fmt.Errorf("failed to CreateResource: %s", err)
}
if err := createResourceCreateActivity(ctx, t.store, resource); err != nil {
return fmt.Errorf("failed to createResourceCreateActivity: %s", err)
}
_, err = t.store.UpsertMemoResource(ctx, &api.MemoResourceUpsert{
MemoID: memoMessage.ID,
ResourceID: resource.ID,
})
if err != nil {
return fmt.Errorf("failed to UpsertMemoResource: %s", err)
}
}
return nil
}