M
M
Miraid2022-01-10 17:33:10
go
Miraid, 2022-01-10 17:33:10

How to pull out the id and text of a message in a telegram on go?

I need to extract the id and text of a telegram message that starts with "/save" or any other command. preferably with this library - https://github.com/go-telegram-bot-api/telegram-bot-api

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2022-01-10
@Miraid

Here are all the fields of the Message structure https://pkg.go.dev/github.com/go-telegram-bot-api/...
An example of how you can get the data you need

package main

import (
  "log"

  tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

func main() {
  bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
  if err != nil {
    log.Panic(err)
  }

  u := tgbotapi.NewUpdate(0)
  u.Timeout = 60

  updates := bot.GetUpdatesChan(u)

  for update := range updates {
            if update.Message == nil { // не обрабатываем если нет сообщения
                continue
            }

            // ID сообщения, int
            log.Printf("message id: %s\n", update.Message.MessageID)

            // так вы можете получить текст сообщения полный (тип string)
            log.Printf("message: %s\n", update.Message.Text)

            if update.Message.IsCommand() {
                // так вы получаете команду
                log.Printf("command: %s\n", update.Message.Command())

                // так вы получаете аргументы (параметры) команды, string
                log.Printf("command: %s\n", update.Message.CommandArguments())
            }
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question