mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-04-29 08:27:30 +08:00
feat: enhance sentinel and add button support for telegram
This commit is contained in:
@@ -3,9 +3,10 @@ package tools
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"clawgo/pkg/bus"
|
||||
)
|
||||
|
||||
type SendCallback func(channel, chatID, content string) error
|
||||
type SendCallback func(channel, chatID, content string, buttons [][]bus.Button) error
|
||||
|
||||
type MessageTool struct {
|
||||
sendCallback SendCallback
|
||||
@@ -41,6 +42,21 @@ func (t *MessageTool) Parameters() map[string]interface{} {
|
||||
"type": "string",
|
||||
"description": "Optional: target chat/user ID",
|
||||
},
|
||||
"buttons": map[string]interface{}{
|
||||
"type": "array",
|
||||
"description": "Optional: buttons to include in the message (2D array: rows of buttons)",
|
||||
"items": map[string]interface{}{
|
||||
"type": "array",
|
||||
"items": map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"text": map[string]interface{}{"type": "string", "description": "Button text"},
|
||||
"data": map[string]interface{}{"type": "string", "description": "Callback data"},
|
||||
},
|
||||
"required": []string{"text", "data"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"required": []string{"content"},
|
||||
}
|
||||
@@ -79,7 +95,28 @@ func (t *MessageTool) Execute(ctx context.Context, args map[string]interface{})
|
||||
return "Error: Message sending not configured", nil
|
||||
}
|
||||
|
||||
if err := t.sendCallback(channel, chatID, content); err != nil {
|
||||
var buttons [][]bus.Button
|
||||
if btns, ok := args["buttons"].([]interface{}); ok {
|
||||
for _, row := range btns {
|
||||
if rowArr, ok := row.([]interface{}); ok {
|
||||
var buttonRow []bus.Button
|
||||
for _, b := range rowArr {
|
||||
if bMap, ok := b.(map[string]interface{}); ok {
|
||||
text, _ := bMap["text"].(string)
|
||||
data, _ := bMap["data"].(string)
|
||||
if text != "" && data != "" {
|
||||
buttonRow = append(buttonRow, bus.Button{Text: text, Data: data})
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(buttonRow) > 0 {
|
||||
buttons = append(buttons, buttonRow)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := t.sendCallback(channel, chatID, content, buttons); err != nil {
|
||||
return fmt.Sprintf("Error sending message: %v", err), nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user