|
| 1 | +package actions |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/go-telegram/bot" |
| 9 | + "github.com/go-telegram/bot/models" |
| 10 | + "github.com/mudler/LocalAGI/core/types" |
| 11 | + "github.com/mudler/LocalAGI/pkg/config" |
| 12 | + "github.com/mudler/LocalAGI/pkg/xstrings" |
| 13 | + "github.com/sashabaranov/go-openai/jsonschema" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + MetadataTelegramMessageSent = "telegram_message_sent" |
| 18 | + telegramMaxMessageLength = 3000 |
| 19 | +) |
| 20 | + |
| 21 | +type SendTelegramMessageRunner struct { |
| 22 | + token string |
| 23 | + chatID int64 |
| 24 | + bot *bot.Bot |
| 25 | +} |
| 26 | + |
| 27 | +func NewSendTelegramMessageRunner(config map[string]string) *SendTelegramMessageRunner { |
| 28 | + token := config["token"] |
| 29 | + if token == "" { |
| 30 | + return nil |
| 31 | + } |
| 32 | + |
| 33 | + // Parse chat ID from config if present |
| 34 | + var chatID int64 |
| 35 | + if configChatID := config["chat_id"]; configChatID != "" { |
| 36 | + var err error |
| 37 | + chatID, err = strconv.ParseInt(configChatID, 10, 64) |
| 38 | + if err != nil { |
| 39 | + return nil |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + b, err := bot.New(token) |
| 44 | + if err != nil { |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + return &SendTelegramMessageRunner{ |
| 49 | + token: token, |
| 50 | + chatID: chatID, |
| 51 | + bot: b, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +type TelegramMessageParams struct { |
| 56 | + ChatID int64 `json:"chat_id"` |
| 57 | + Message string `json:"message"` |
| 58 | +} |
| 59 | + |
| 60 | +func (s *SendTelegramMessageRunner) Run(ctx context.Context, params types.ActionParams) (types.ActionResult, error) { |
| 61 | + var messageParams TelegramMessageParams |
| 62 | + err := params.Unmarshal(&messageParams) |
| 63 | + if err != nil { |
| 64 | + return types.ActionResult{}, fmt.Errorf("failed to unmarshal params: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + if s.chatID != 0 { |
| 68 | + messageParams.ChatID = s.chatID |
| 69 | + } |
| 70 | + |
| 71 | + if messageParams.ChatID == 0 { |
| 72 | + return types.ActionResult{}, fmt.Errorf("chat_id is required either in config or parameters") |
| 73 | + } |
| 74 | + |
| 75 | + if messageParams.Message == "" { |
| 76 | + return types.ActionResult{}, fmt.Errorf("message is required") |
| 77 | + } |
| 78 | + |
| 79 | + // Split the message if it's too long |
| 80 | + messages := xstrings.SplitParagraph(messageParams.Message, telegramMaxMessageLength) |
| 81 | + |
| 82 | + if len(messages) == 0 { |
| 83 | + return types.ActionResult{}, fmt.Errorf("empty message after splitting") |
| 84 | + } |
| 85 | + |
| 86 | + // Send each message part |
| 87 | + for i, msg := range messages { |
| 88 | + _, err = s.bot.SendMessage(ctx, &bot.SendMessageParams{ |
| 89 | + ChatID: messageParams.ChatID, |
| 90 | + Text: msg, |
| 91 | + ParseMode: models.ParseModeMarkdown, |
| 92 | + }) |
| 93 | + if err != nil { |
| 94 | + return types.ActionResult{}, fmt.Errorf("failed to send telegram message part %d: %w", i+1, err) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return types.ActionResult{ |
| 99 | + Result: fmt.Sprintf("Message sent successfully to chat ID %d in %d parts", messageParams.ChatID, len(messages)), |
| 100 | + Metadata: map[string]interface{}{ |
| 101 | + MetadataTelegramMessageSent: true, |
| 102 | + }, |
| 103 | + }, nil |
| 104 | +} |
| 105 | + |
| 106 | +func (s *SendTelegramMessageRunner) Definition() types.ActionDefinition { |
| 107 | + if s.chatID != 0 { |
| 108 | + return types.ActionDefinition{ |
| 109 | + Name: "send_telegram_message", |
| 110 | + Description: "Send a message to a Telegram user or group", |
| 111 | + Properties: map[string]jsonschema.Definition{ |
| 112 | + "message": { |
| 113 | + Type: jsonschema.String, |
| 114 | + Description: "The message to send", |
| 115 | + }, |
| 116 | + }, |
| 117 | + Required: []string{"message"}, |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return types.ActionDefinition{ |
| 122 | + Name: "send_telegram_message", |
| 123 | + Description: "Send a message to a Telegram user or group", |
| 124 | + Properties: map[string]jsonschema.Definition{ |
| 125 | + "chat_id": { |
| 126 | + Type: jsonschema.Number, |
| 127 | + Description: "The Telegram chat ID to send the message to (optional if configured in config)", |
| 128 | + }, |
| 129 | + "message": { |
| 130 | + Type: jsonschema.String, |
| 131 | + Description: "The message to send", |
| 132 | + }, |
| 133 | + }, |
| 134 | + Required: []string{"message", "chat_id"}, |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func (s *SendTelegramMessageRunner) Plannable() bool { |
| 139 | + return true |
| 140 | +} |
| 141 | + |
| 142 | +// SendTelegramMessageConfigMeta returns the metadata for Send Telegram Message action configuration fields |
| 143 | +func SendTelegramMessageConfigMeta() []config.Field { |
| 144 | + return []config.Field{ |
| 145 | + { |
| 146 | + Name: "token", |
| 147 | + Label: "Telegram Token", |
| 148 | + Type: config.FieldTypeText, |
| 149 | + Required: true, |
| 150 | + HelpText: "Telegram bot token for sending messages", |
| 151 | + }, |
| 152 | + { |
| 153 | + Name: "chat_id", |
| 154 | + Label: "Default Chat ID", |
| 155 | + Type: config.FieldTypeText, |
| 156 | + Required: false, |
| 157 | + HelpText: "Default Telegram chat ID to send messages to (can be overridden in parameters)", |
| 158 | + }, |
| 159 | + } |
| 160 | +} |
0 commit comments