mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
feat: implement telegram bot plugin (#1740)
This commit is contained in:
24
plugin/telegram/api_edit_message.go
Normal file
24
plugin/telegram/api_edit_message.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// EditMessage make an editMessageText api request.
|
||||
func (r *Robot) EditMessage(ctx context.Context, chatID, messageID int, text string) (*Message, error) {
|
||||
formData := url.Values{
|
||||
"message_id": {strconv.Itoa(messageID)},
|
||||
"chat_id": {strconv.Itoa(chatID)},
|
||||
"text": {text},
|
||||
}
|
||||
|
||||
var result Message
|
||||
err := r.postForm(ctx, "/editMessageText", formData, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
21
plugin/telegram/api_get_file.go
Normal file
21
plugin/telegram/api_get_file.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// GetFile get download info of File by fileID from Telegram.
|
||||
func (r *Robot) GetFile(ctx context.Context, fileID string) (*File, error) {
|
||||
formData := url.Values{
|
||||
"file_id": {fileID},
|
||||
}
|
||||
|
||||
var result File
|
||||
err := r.postForm(ctx, "/getFile", formData, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
23
plugin/telegram/api_get_updates.go
Normal file
23
plugin/telegram/api_get_updates.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// GetUpdates make a getUpdates api request.
|
||||
func (r *Robot) GetUpdates(ctx context.Context, offset int) ([]Update, error) {
|
||||
formData := url.Values{
|
||||
"timeout": {"60"},
|
||||
"offset": {strconv.Itoa(offset)},
|
||||
}
|
||||
|
||||
var result []Update
|
||||
err := r.postForm(ctx, "/getUpdates", formData, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
24
plugin/telegram/api_send_message.go
Normal file
24
plugin/telegram/api_send_message.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// SendReplyMessage make a sendMessage api request.
|
||||
func (r *Robot) SendReplyMessage(ctx context.Context, chatID, replyID int, text string) (*Message, error) {
|
||||
formData := url.Values{
|
||||
"reply_to_message_id": {strconv.Itoa(replyID)},
|
||||
"chat_id": {strconv.Itoa(chatID)},
|
||||
"text": {text},
|
||||
}
|
||||
|
||||
var result Message
|
||||
err := r.postForm(ctx, "/sendMessage", formData, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
9
plugin/telegram/chat.go
Normal file
9
plugin/telegram/chat.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package telegram
|
||||
|
||||
type Chat struct {
|
||||
ID int `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Type string `json:"type"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
}
|
44
plugin/telegram/download.go
Normal file
44
plugin/telegram/download.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// downloadFileId download file with fileID, return the filepath and blob.
|
||||
func (r *Robot) downloadFileID(ctx context.Context, fileID string) (string, []byte, error) {
|
||||
file, err := r.GetFile(ctx, fileID)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
blob, err := r.downloadFilepath(ctx, file.FilePath)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
return file.FilePath, blob, nil
|
||||
}
|
||||
|
||||
// downloadFilepath download file with filepath, you can get filepath by calling GetFile.
|
||||
func (r *Robot) downloadFilepath(ctx context.Context, filePath string) ([]byte, error) {
|
||||
token := r.handler.RobotToken(ctx)
|
||||
if token == "" {
|
||||
return nil, ErrNoToken
|
||||
}
|
||||
|
||||
uri := "https://api.telegram.org/file/bot" + token + "/" + filePath
|
||||
resp, err := http.Get(uri)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fail to http.Get: %s", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fail to io.ReadAll: %s", err)
|
||||
}
|
||||
|
||||
return body, nil
|
||||
}
|
8
plugin/telegram/file.go
Normal file
8
plugin/telegram/file.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package telegram
|
||||
|
||||
type File struct {
|
||||
FileID string `json:"file_id"`
|
||||
FileUniqueID string `json:"file_unique_id"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
FilePath string `json:"file_path"`
|
||||
}
|
104
plugin/telegram/handle.go
Normal file
104
plugin/telegram/handle.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/usememos/memos/common/log"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// notice message send to telegram.
|
||||
const (
|
||||
workingMessage = "Working on send your memo..."
|
||||
successMessage = "Success"
|
||||
)
|
||||
|
||||
// handleSingleMessage handle a message not belongs to group.
|
||||
func (r *Robot) handleSingleMessage(ctx context.Context, message Message) error {
|
||||
reply, err := r.SendReplyMessage(ctx, message.Chat.ID, message.MessageID, workingMessage)
|
||||
if err != nil {
|
||||
return fmt.Errorf("fail to SendReplyMessage: %s", err)
|
||||
}
|
||||
|
||||
var blobs map[string][]byte
|
||||
|
||||
// download blob if need
|
||||
if len(message.Photo) > 0 {
|
||||
filepath, blob, err := r.downloadFileID(ctx, message.GetMaxPhotoFileID())
|
||||
if err != nil {
|
||||
log.Error("fail to downloadFileID", zap.Error(err))
|
||||
_, err = r.EditMessage(ctx, message.Chat.ID, reply.MessageID, err.Error())
|
||||
if err != nil {
|
||||
return fmt.Errorf("fail to EditMessage: %s", err)
|
||||
}
|
||||
return fmt.Errorf("fail to downloadFileID: %s", err)
|
||||
}
|
||||
blobs = map[string][]byte{filepath: blob}
|
||||
}
|
||||
|
||||
err = r.handler.MessageHandle(ctx, message, blobs)
|
||||
if err != nil {
|
||||
if _, err := r.EditMessage(ctx, message.Chat.ID, reply.MessageID, err.Error()); err != nil {
|
||||
return fmt.Errorf("fail to EditMessage: %s", err)
|
||||
}
|
||||
return fmt.Errorf("fail to MessageHandle: %s", err)
|
||||
}
|
||||
|
||||
if _, err := r.EditMessage(ctx, message.Chat.ID, reply.MessageID, successMessage); err != nil {
|
||||
return fmt.Errorf("fail to EditMessage: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleGroupMessages handle a message belongs to group.
|
||||
func (r *Robot) handleGroupMessages(ctx context.Context, groupMessages []Message) error {
|
||||
captions := make(map[string]string, len(groupMessages))
|
||||
messages := make(map[string]Message, len(groupMessages))
|
||||
blobs := make(map[string]map[string][]byte, len(groupMessages))
|
||||
|
||||
// Group all captions, blobs and messages
|
||||
for _, message := range groupMessages {
|
||||
groupID := *message.MediaGroupID
|
||||
|
||||
messages[groupID] = message
|
||||
|
||||
if message.Caption != nil {
|
||||
captions[groupID] += *message.Caption
|
||||
}
|
||||
|
||||
filepath, blob, err := r.downloadFileID(ctx, message.GetMaxPhotoFileID())
|
||||
if err != nil {
|
||||
return fmt.Errorf("fail to downloadFileID")
|
||||
}
|
||||
if _, found := blobs[groupID]; !found {
|
||||
blobs[groupID] = make(map[string][]byte)
|
||||
}
|
||||
blobs[groupID][filepath] = blob
|
||||
}
|
||||
|
||||
// Handle each group message
|
||||
for groupID, message := range messages {
|
||||
reply, err := r.SendReplyMessage(ctx, message.Chat.ID, message.MessageID, workingMessage)
|
||||
if err != nil {
|
||||
return fmt.Errorf("fail to SendReplyMessage: %s", err)
|
||||
}
|
||||
|
||||
// replace Caption with all Caption in the group
|
||||
caption := captions[groupID]
|
||||
message.Caption = &caption
|
||||
if err := r.handler.MessageHandle(ctx, message, blobs[groupID]); err != nil {
|
||||
if _, err = r.EditMessage(ctx, message.Chat.ID, reply.MessageID, err.Error()); err != nil {
|
||||
return fmt.Errorf("fail to EditMessage: %s", err)
|
||||
}
|
||||
return fmt.Errorf("fail to MessageHandle: %s", err)
|
||||
}
|
||||
|
||||
if _, err := r.EditMessage(ctx, message.Chat.ID, reply.MessageID, successMessage); err != nil {
|
||||
return fmt.Errorf("fail to EditMessage: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
24
plugin/telegram/message.go
Normal file
24
plugin/telegram/message.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package telegram
|
||||
|
||||
type Message struct {
|
||||
MessageID int `json:"message_id"`
|
||||
From User `json:"from"`
|
||||
Date int `json:"date"`
|
||||
Text *string `json:"text"`
|
||||
Chat *Chat `json:"chat"`
|
||||
MediaGroupID *string `json:"media_group_id"`
|
||||
Photo []PhotoSize `json:"photo"`
|
||||
Caption *string `json:"caption"`
|
||||
}
|
||||
|
||||
func (m Message) GetMaxPhotoFileID() string {
|
||||
var fileSize int64
|
||||
var photoSize PhotoSize
|
||||
for _, p := range m.Photo {
|
||||
if p.FileSize > fileSize {
|
||||
photoSize = p
|
||||
}
|
||||
}
|
||||
|
||||
return photoSize.FileID
|
||||
}
|
9
plugin/telegram/photosize.go
Normal file
9
plugin/telegram/photosize.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package telegram
|
||||
|
||||
type PhotoSize struct {
|
||||
FileID string `json:"file_id"`
|
||||
FileUniqueID string `json:"file_unique_id"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
}
|
52
plugin/telegram/request.go
Normal file
52
plugin/telegram/request.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
var ErrNoToken = errors.New("token is empty")
|
||||
|
||||
func (r *Robot) postForm(ctx context.Context, apiPath string, formData url.Values, result any) error {
|
||||
token := r.handler.RobotToken(ctx)
|
||||
if token == "" {
|
||||
return ErrNoToken
|
||||
}
|
||||
|
||||
uri := "https://api.telegram.org/bot" + token + apiPath
|
||||
resp, err := http.PostForm(uri, formData)
|
||||
if err != nil {
|
||||
return fmt.Errorf("fail to http.PostForm: %s", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("fail to ioutil.ReadAll: %s", err)
|
||||
}
|
||||
|
||||
var respInfo struct {
|
||||
Ok bool `json:"ok"`
|
||||
ErrorCode int `json:"error_code"`
|
||||
Description string `json:"description"`
|
||||
Result any `json:"result"`
|
||||
}
|
||||
|
||||
respInfo.Result = result
|
||||
|
||||
err = json.Unmarshal(body, &respInfo)
|
||||
if err != nil {
|
||||
return fmt.Errorf("fail to json.Unmarshal: %s", err)
|
||||
}
|
||||
|
||||
if !respInfo.Ok {
|
||||
return fmt.Errorf("api error: [%d]%s", respInfo.ErrorCode, respInfo.Description)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
79
plugin/telegram/robot.go
Normal file
79
plugin/telegram/robot.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/usememos/memos/common/log"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type Handler interface {
|
||||
RobotToken(ctx context.Context) string
|
||||
MessageHandle(ctx context.Context, message Message, blobs map[string][]byte) error
|
||||
}
|
||||
|
||||
type Robot struct {
|
||||
handler Handler
|
||||
}
|
||||
|
||||
// NewRobotWithHandler create a telegram robot with specified handler.
|
||||
func NewRobotWithHandler(h Handler) *Robot {
|
||||
return &Robot{handler: h}
|
||||
}
|
||||
|
||||
const noTokenWait = 30 * time.Second
|
||||
|
||||
// Start start an infinity call of getUpdates from Telegram, call r.MessageHandle while get new message updates.
|
||||
func (r *Robot) Start(ctx context.Context) {
|
||||
var offset int
|
||||
|
||||
for {
|
||||
updates, err := r.GetUpdates(ctx, offset)
|
||||
if err == ErrNoToken {
|
||||
time.Sleep(noTokenWait)
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
log.Warn("fail to telegram.GetUpdates", zap.Error(err))
|
||||
continue
|
||||
}
|
||||
|
||||
groupMessages := make([]Message, 0, len(updates))
|
||||
|
||||
for _, update := range updates {
|
||||
offset = update.UpdateID + 1
|
||||
if update.Message == nil {
|
||||
continue
|
||||
}
|
||||
message := *update.Message
|
||||
|
||||
// skip message other than text or photo
|
||||
if message.Text == nil && message.Photo == nil {
|
||||
_, err := r.SendReplyMessage(ctx, message.Chat.ID, message.MessageID, "Only text or photo message be supported")
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("fail to telegram.SendReplyMessage for messageID=%d", message.MessageID), zap.Error(err))
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Group message need do more
|
||||
if message.MediaGroupID != nil {
|
||||
groupMessages = append(groupMessages, message)
|
||||
continue
|
||||
}
|
||||
|
||||
err = r.handleSingleMessage(ctx, message)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("fail to handleSingleMessage for messageID=%d", message.MessageID), zap.Error(err))
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
err = r.handleGroupMessages(ctx, groupMessages)
|
||||
if err != nil {
|
||||
log.Error("fail to handle plain text message", zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
6
plugin/telegram/update.go
Normal file
6
plugin/telegram/update.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package telegram
|
||||
|
||||
type Update struct {
|
||||
UpdateID int `json:"update_id"`
|
||||
Message *Message `json:"message"`
|
||||
}
|
5
plugin/telegram/user.go
Normal file
5
plugin/telegram/user.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package telegram
|
||||
|
||||
type User struct {
|
||||
ID int `json:"id"`
|
||||
}
|
Reference in New Issue
Block a user