api_edit_message.go 907 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package telegram
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/url"
  7. "strconv"
  8. )
  9. // EditMessage make an editMessageText api request.
  10. func (b *Bot) EditMessage(ctx context.Context, chatID, messageID int, text string, inlineKeyboards [][]InlineKeyboardButton) (*Message, error) {
  11. formData := url.Values{
  12. "message_id": {strconv.Itoa(messageID)},
  13. "chat_id": {strconv.Itoa(chatID)},
  14. "text": {text},
  15. }
  16. if len(inlineKeyboards) > 0 {
  17. var markup struct {
  18. InlineKeyboard [][]InlineKeyboardButton `json:"inline_keyboard"`
  19. }
  20. markup.InlineKeyboard = inlineKeyboards
  21. data, err := json.Marshal(markup)
  22. if err != nil {
  23. return nil, fmt.Errorf("fail to encode inlineKeyboard: %s", err)
  24. }
  25. formData.Set("reply_markup", string(data))
  26. }
  27. var result Message
  28. err := b.postForm(ctx, "/editMessageText", formData, &result)
  29. if err != nil {
  30. return nil, err
  31. }
  32. return &result, nil
  33. }