Skip to main content

Overview

NewMessage represents an incoming message update. It’s the most commonly used update type and provides rich methods for interacting with messages.

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
Get message text
text := m.Text()
Date()
int32
Message timestamp (Unix time)
timestamp := m.Date()

Sender Information

SenderID()
int64
Get sender’s user ID
senderID := m.SenderID()
GetSender()
(*UserObj, error)
Get full sender user object
sender, err := m.GetSender()
fmt.Println(sender.FirstName)
Sender
*UserObj
Direct access to sender object (may be nil)
if m.Sender != nil {
    fmt.Println(m.Sender.FirstName)
}

Chat Information

ChatID()
int64
Get chat ID (works for users, groups, channels)
chatID := m.ChatID()
ChannelID()
int64
Get channel ID with -100 prefix
channelID := m.ChannelID()
ChatType()
string
Get chat type: user, chat, channel
if m.ChatType() == telegram.EntityUser {
    // Private chat
}

Reply Methods

Sending Replies

// Reply to the message
m.Reply("Hello there!")

// With options
m.Reply("Hello!", telegram.SendOptions{
    ParseMode: "HTML",
})

Album Methods

// Reply with album
m.ReplyAlbum([]telegram.InputMedia{
    &telegram.InputMediaPhoto{File: "photo1.jpg"},
    &telegram.InputMediaPhoto{File: "photo2.jpg"},
})

// Send album without replying
m.RespondAlbum([]telegram.InputMedia{...})

Message Actions

Edit & Delete

// Edit message text
m.Edit("Updated text")

// Edit with options
m.Edit("Updated", telegram.SendOptions{
    ParseMode: "Markdown",
})

Forward & Pin

// Forward to another chat
m.ForwardTo(targetChatID)

// With options
m.ForwardTo(targetChatID, &telegram.ForwardOptions{
    Silent: true,
})

Message Checks

Chat Type Checks

  • Private
  • Group
  • Channel
if m.IsPrivate() {
    m.Reply("This is a private chat")
}

Message Type Checks

  • Command
  • Reply
  • Forward
  • Media
if m.IsCommand() {
    cmd := m.GetCommand()
    fmt.Println("Command:", cmd)
}

Media Access

Getting Media

photo := m.Photo()
if photo != nil {
    path, _ := m.Download()
}

Special Media Types

contact := m.Contact()
if contact != nil {
    fmt.Println(contact.PhoneNumber)
}

Media Type Detection

mediaType := m.MediaType()

switch mediaType {
case "photo":
    path, _ := m.Download()
case "video":
    m.Reply("Got your video!")
case "document":
    m.Reply("Received document")
case "contact":
    m.Reply("Thanks for sharing contact")
case "geo", "geo_live":
    m.Reply("Location received")
default:
    m.Reply("Unsupported media type")
}

Reply Information

Getting Reply Details

replyID := m.ReplyToMsgID()
// or
replyID := m.ReplyID()

Text Processing

Text Extraction

// Plain text
text := m.Text()

Advanced Features

Conversations

// Ask and wait for response
response, err := m.Ask("What's your name?")
if err == nil {
    m.Reply("Nice to meet you, " + response.Text())
}

Downloading Media

// Download with default options
path, err := m.Download()

// Download to specific directory
path, err := m.Download(&telegram.DownloadOptions{
    Output: "downloads/",
})

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

Utilities

// Get message link
link := m.Link()
// Public: https://t.me/username/123
// Private: https://t.me/c/channelID/123

Complete Example

client.On("cmd:start", func(m *telegram.NewMessage) error {
   if m.IsMedia() {
       m.Reply("Thanks for the media!")
   } else {
       m.Reply("Hello! Send me a photo or video.")
   }
    return nil
}, telegram.FilterPrivate)

Next Steps