Skip to main content

NewMessage Overview

The NewMessage object is the primary structure for incoming message updates in Gogram. It provides a rich set of methods to interact with the message, its sender, and the chat it belongs to.

Structure

type NewMessage struct {
    Action         MessageAction
    Channel        *Channel
    Chat           *ChatObj
    Client         *Client
    File           *CustomFile
    ID             int32
    Message        *MessageObj
    OriginalUpdate Message
    Peer           InputPeer
    Sender         *UserObj
    SenderChat     *Channel
}

Basic Properties

Message Information

ID
int32
Message ID
msgID := m.ID
Text()
string
Returns the plain text of the message.
text := m.Text()
Date()
int32
Unix timestamp of when the message was sent.
timestamp := m.Date()

Sender Information

SenderID()
int64
Returns the ID of the user or chat that sent the message.
senderID := m.SenderID()
GetSender()
(*UserObj, error)
Fetches the full sender object.
sender, err := m.GetSender()

Chat Information

ChatID()
int64
Returns the ID of the chat where the message was sent.
chatID := m.ChatID()
ChatType()
string
Returns the type of chat: user, chat, or channel.

Interactive Methods

Sending Replies

// Reply directly to the message
m.Reply("Text reply")

// Send a message to the same chat without replying
m.Respond("Simple response")

// Reply with media
m.ReplyMedia(&telegram.InputMediaPhoto{File: "path.jpg"})

Media Access

MethodDescription
.Photo()Returns photo object or nil.
.Video()Returns video object or nil.
.Document()Returns document object or nil.
.Sticker()Returns sticker object or nil.
.Voice()Returns voice note object or nil.
.MediaType()Returns string type of media (e.g., “photo”).

Downloading Media

// Download to default directory
path, err := m.Download()

// Download with progress tracking
path, err := m.Download(&telegram.DownloadOptions{
    ProgressCallback: func(current, total int64) {
        fmt.Printf("%d/%d\n", current, total)
    },
})

Advanced Features

Conversations

Handle multi-step user input directly within the handler.
response, err := m.Ask("What is your name?")
if err == nil {
    m.Reply("Hello, " + response.Text())
}

Checks and Predicates

if m.IsPrivate() {
    // Handling for private chats
}

if m.IsCommand() {
    cmd := m.GetCommand() // e.g., "/start"
}

if m.IsReply() {
    original, _ := m.GetReplyMessage()
}