2015-06-27 17:37:22 +00:00
|
|
|
package telebot
|
|
|
|
|
|
|
|
import (
|
2017-12-26 22:39:15 +00:00
|
|
|
"strconv"
|
2015-06-27 17:37:22 +00:00
|
|
|
"time"
|
2021-10-13 18:12:05 +00:00
|
|
|
"unicode/utf16"
|
2015-06-27 17:37:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Message object represents a message.
|
|
|
|
type Message struct {
|
2015-10-16 16:36:20 +00:00
|
|
|
ID int `json:"message_id"`
|
2015-10-16 22:36:07 +00:00
|
|
|
|
2017-11-17 02:51:03 +00:00
|
|
|
// For message sent to channels, Sender will be nil
|
2017-11-24 15:24:07 +00:00
|
|
|
Sender *User `json:"from"`
|
2015-10-16 22:36:07 +00:00
|
|
|
|
2017-11-18 14:41:23 +00:00
|
|
|
// Unixtime, use Message.Time() to get time.Time
|
2017-11-17 13:10:18 +00:00
|
|
|
Unixtime int64 `json:"date"`
|
|
|
|
|
2017-11-24 15:24:07 +00:00
|
|
|
// Conversation the message belongs to.
|
2017-11-18 14:41:23 +00:00
|
|
|
Chat *Chat `json:"chat"`
|
|
|
|
|
2021-05-23 16:55:49 +00:00
|
|
|
// Sender of the message, sent on behalf of a chat.
|
|
|
|
SenderChat *Chat `json:"sender_chat"`
|
|
|
|
|
2015-06-27 17:37:22 +00:00
|
|
|
// For forwarded messages, sender of the original message.
|
2017-08-25 13:10:18 +00:00
|
|
|
OriginalSender *User `json:"forward_from"`
|
2015-06-27 17:37:22 +00:00
|
|
|
|
2017-11-18 14:41:23 +00:00
|
|
|
// For forwarded messages, chat of the original message when
|
|
|
|
// forwarded from a channel.
|
2017-08-25 13:10:18 +00:00
|
|
|
OriginalChat *Chat `json:"forward_from_chat"`
|
2017-02-25 01:29:09 +00:00
|
|
|
|
2020-06-13 10:10:26 +00:00
|
|
|
// For forwarded messages, identifier of the original message
|
|
|
|
// when forwarded from a channel.
|
|
|
|
OriginalMessageID int `json:"forward_from_message_id"`
|
|
|
|
|
|
|
|
// For forwarded messages, signature of the post author.
|
|
|
|
OriginalSignature string `json:"forward_signature"`
|
|
|
|
|
|
|
|
// For forwarded messages, sender's name from users who
|
|
|
|
// disallow adding a link to their account.
|
|
|
|
OriginalSenderName string `json:"forward_sender_name"`
|
|
|
|
|
2015-06-27 17:37:22 +00:00
|
|
|
// For forwarded messages, unixtime of the original message.
|
|
|
|
OriginalUnixtime int `json:"forward_date"`
|
|
|
|
|
|
|
|
// For replies, ReplyTo represents the original message.
|
2017-11-18 14:41:23 +00:00
|
|
|
//
|
2015-06-27 17:37:22 +00:00
|
|
|
// Note that the Message object in this field will not
|
|
|
|
// contain further ReplyTo fields even if it
|
|
|
|
// itself is a reply.
|
|
|
|
ReplyTo *Message `json:"reply_to_message"`
|
|
|
|
|
2020-06-07 18:52:26 +00:00
|
|
|
// Shows through which bot the message was sent.
|
|
|
|
Via *User `json:"via_bot"`
|
|
|
|
|
2017-11-26 01:26:07 +00:00
|
|
|
// (Optional) Time of last edit in Unix
|
|
|
|
LastEdit int64 `json:"edit_date"`
|
|
|
|
|
|
|
|
// AlbumID is the unique identifier of a media message group
|
|
|
|
// this message belongs to.
|
|
|
|
AlbumID string `json:"media_group_id"`
|
|
|
|
|
|
|
|
// Author signature (in channels).
|
|
|
|
Signature string `json:"author_signature"`
|
|
|
|
|
2017-11-17 13:10:18 +00:00
|
|
|
// For a text message, the actual UTF-8 text of the message.
|
2015-06-27 17:37:22 +00:00
|
|
|
Text string `json:"text"`
|
|
|
|
|
2017-11-27 14:56:22 +00:00
|
|
|
// For registered commands, will contain the string payload:
|
|
|
|
//
|
|
|
|
// Ex: `/command <payload>` or `/command@botname <payload>`
|
|
|
|
Payload string `json:"-"`
|
|
|
|
|
2017-11-18 14:41:23 +00:00
|
|
|
// For text messages, special entities like usernames, URLs, bot commands,
|
|
|
|
// etc. that appear in the text.
|
2021-11-07 16:47:48 +00:00
|
|
|
Entities Entities `json:"entities,omitempty"`
|
2017-11-18 14:41:23 +00:00
|
|
|
|
2017-11-18 13:06:20 +00:00
|
|
|
// Some messages containing media, may as well have a caption.
|
|
|
|
Caption string `json:"caption,omitempty"`
|
|
|
|
|
2017-11-18 14:41:23 +00:00
|
|
|
// For messages with a caption, special entities like usernames, URLs,
|
|
|
|
// bot commands, etc. that appear in the caption.
|
2021-11-07 16:47:48 +00:00
|
|
|
CaptionEntities Entities `json:"caption_entities,omitempty"`
|
2017-11-18 14:41:23 +00:00
|
|
|
|
2015-06-27 17:37:22 +00:00
|
|
|
// For an audio recording, information about it.
|
2017-08-25 13:10:18 +00:00
|
|
|
Audio *Audio `json:"audio"`
|
2015-06-27 17:37:22 +00:00
|
|
|
|
2020-06-09 20:28:28 +00:00
|
|
|
// For a general file, information about it.
|
2017-08-25 13:10:18 +00:00
|
|
|
Document *Document `json:"document"`
|
2015-06-27 17:37:22 +00:00
|
|
|
|
2017-11-18 18:16:16 +00:00
|
|
|
// For a photo, all available sizes (thumbnails).
|
|
|
|
Photo *Photo `json:"photo"`
|
2015-06-27 17:37:22 +00:00
|
|
|
|
|
|
|
// For a sticker, information about it.
|
2017-08-25 13:10:18 +00:00
|
|
|
Sticker *Sticker `json:"sticker"`
|
2015-06-27 17:37:22 +00:00
|
|
|
|
2017-11-17 13:10:18 +00:00
|
|
|
// For a voice message, information about it.
|
|
|
|
Voice *Voice `json:"voice"`
|
|
|
|
|
|
|
|
// For a video note, information about it.
|
|
|
|
VideoNote *VideoNote `json:"video_note"`
|
|
|
|
|
2015-06-27 17:37:22 +00:00
|
|
|
// For a video, information about it.
|
2017-08-25 13:10:18 +00:00
|
|
|
Video *Video `json:"video"`
|
2015-06-27 17:37:22 +00:00
|
|
|
|
2020-02-29 12:34:07 +00:00
|
|
|
// For a animation, information about it.
|
|
|
|
Animation *Animation `json:"animation"`
|
|
|
|
|
2015-06-27 17:37:22 +00:00
|
|
|
// For a contact, contact information itself.
|
2017-08-25 13:10:18 +00:00
|
|
|
Contact *Contact `json:"contact"`
|
2015-06-27 17:37:22 +00:00
|
|
|
|
|
|
|
// For a location, its longitude and latitude.
|
2017-08-25 13:10:18 +00:00
|
|
|
Location *Location `json:"location"`
|
2015-06-27 17:37:22 +00:00
|
|
|
|
2017-11-24 15:24:07 +00:00
|
|
|
// For a venue, information about it.
|
|
|
|
Venue *Venue `json:"venue"`
|
|
|
|
|
2020-04-24 14:52:15 +00:00
|
|
|
// For a poll, information the native poll.
|
|
|
|
Poll *Poll `json:"poll"`
|
|
|
|
|
2021-08-19 13:40:07 +00:00
|
|
|
// For a game, information about it.
|
|
|
|
Game *Game `json:"game"`
|
|
|
|
|
2020-04-26 08:56:23 +00:00
|
|
|
// For a dice, information about it.
|
|
|
|
Dice *Dice `json:"dice"`
|
|
|
|
|
2015-06-27 17:37:22 +00:00
|
|
|
// For a service message, represents a user,
|
|
|
|
// that just got added to chat, this message came from.
|
|
|
|
//
|
|
|
|
// Sender leads to User, capable of invite.
|
|
|
|
//
|
|
|
|
// UserJoined might be the Bot itself.
|
2017-08-25 13:10:18 +00:00
|
|
|
UserJoined *User `json:"new_chat_member"`
|
2015-06-27 17:37:22 +00:00
|
|
|
|
|
|
|
// For a service message, represents a user,
|
|
|
|
// that just left chat, this message came from.
|
|
|
|
//
|
|
|
|
// If user was kicked, Sender leads to a User,
|
|
|
|
// capable of this kick.
|
|
|
|
//
|
|
|
|
// UserLeft might be the Bot itself.
|
2017-08-25 13:10:18 +00:00
|
|
|
UserLeft *User `json:"left_chat_member"`
|
2015-06-27 17:37:22 +00:00
|
|
|
|
|
|
|
// For a service message, represents a new title
|
|
|
|
// for chat this message came from.
|
|
|
|
//
|
|
|
|
// Sender would lead to a User, capable of change.
|
2017-11-26 01:26:07 +00:00
|
|
|
NewGroupTitle string `json:"new_chat_title"`
|
2015-06-27 17:37:22 +00:00
|
|
|
|
|
|
|
// For a service message, represents all available
|
2017-11-25 14:22:13 +00:00
|
|
|
// thumbnails of the new chat photo.
|
2015-06-27 17:37:22 +00:00
|
|
|
//
|
|
|
|
// Sender would lead to a User, capable of change.
|
2017-11-26 01:26:07 +00:00
|
|
|
NewGroupPhoto *Photo `json:"new_chat_photo"`
|
2015-06-27 17:37:22 +00:00
|
|
|
|
2017-11-23 02:13:15 +00:00
|
|
|
// For a service message, new members that were added to
|
|
|
|
// the group or supergroup and information about them
|
|
|
|
// (the bot itself may be one of these members).
|
2017-11-26 01:26:07 +00:00
|
|
|
UsersJoined []User `json:"new_chat_members"`
|
2017-11-23 02:13:15 +00:00
|
|
|
|
2015-06-27 17:37:22 +00:00
|
|
|
// For a service message, true if chat photo just
|
|
|
|
// got removed.
|
|
|
|
//
|
|
|
|
// Sender would lead to a User, capable of change.
|
2017-11-26 01:26:07 +00:00
|
|
|
GroupPhotoDeleted bool `json:"delete_chat_photo"`
|
2015-06-27 17:37:22 +00:00
|
|
|
|
|
|
|
// For a service message, true if group has been created.
|
|
|
|
//
|
2019-12-25 22:55:51 +00:00
|
|
|
// You would receive such a message if you are one of
|
2015-06-27 17:37:22 +00:00
|
|
|
// initial group chat members.
|
|
|
|
//
|
|
|
|
// Sender would lead to creator of the chat.
|
2017-11-24 15:24:07 +00:00
|
|
|
GroupCreated bool `json:"group_chat_created"`
|
2015-12-24 08:52:27 +00:00
|
|
|
|
2020-06-09 20:28:28 +00:00
|
|
|
// For a service message, true if supergroup has been created.
|
2015-12-24 08:52:27 +00:00
|
|
|
//
|
2019-12-25 22:55:51 +00:00
|
|
|
// You would receive such a message if you are one of
|
2015-12-24 08:52:27 +00:00
|
|
|
// initial group chat members.
|
|
|
|
//
|
|
|
|
// Sender would lead to creator of the chat.
|
|
|
|
SuperGroupCreated bool `json:"supergroup_chat_created"`
|
|
|
|
|
|
|
|
// For a service message, true if channel has been created.
|
|
|
|
//
|
2019-12-25 22:55:51 +00:00
|
|
|
// You would receive such a message if you are one of
|
2015-12-24 08:52:27 +00:00
|
|
|
// initial channel administrators.
|
|
|
|
//
|
|
|
|
// Sender would lead to creator of the chat.
|
|
|
|
ChannelCreated bool `json:"channel_chat_created"`
|
|
|
|
|
2020-06-09 20:28:28 +00:00
|
|
|
// For a service message, the destination (supergroup) you
|
2015-12-24 08:52:27 +00:00
|
|
|
// migrated to.
|
|
|
|
//
|
2019-12-25 22:55:51 +00:00
|
|
|
// You would receive such a message when your chat has migrated
|
2020-06-09 20:28:28 +00:00
|
|
|
// to a supergroup.
|
2015-12-24 08:52:27 +00:00
|
|
|
//
|
|
|
|
// Sender would lead to creator of the migration.
|
|
|
|
MigrateTo int64 `json:"migrate_to_chat_id"`
|
|
|
|
|
|
|
|
// For a service message, the Origin (normal group) you migrated
|
|
|
|
// from.
|
|
|
|
//
|
2019-12-25 22:55:51 +00:00
|
|
|
// You would receive such a message when your chat has migrated
|
2020-06-09 20:28:28 +00:00
|
|
|
// to a supergroup.
|
2015-12-24 08:52:27 +00:00
|
|
|
//
|
|
|
|
// Sender would lead to creator of the migration.
|
|
|
|
MigrateFrom int64 `json:"migrate_from_chat_id"`
|
2017-11-26 01:26:07 +00:00
|
|
|
|
|
|
|
// Specified message was pinned. Note that the Message object
|
|
|
|
// in this field will not contain further ReplyTo fields even
|
|
|
|
// if it is itself a reply.
|
|
|
|
PinnedMessage *Message `json:"pinned_message"`
|
2019-11-03 13:31:41 +00:00
|
|
|
|
2020-05-21 08:48:20 +00:00
|
|
|
// Message is an invoice for a payment.
|
|
|
|
Invoice *Invoice `json:"invoice"`
|
|
|
|
|
|
|
|
// Message is a service message about a successful payment.
|
|
|
|
Payment *Payment `json:"successful_payment"`
|
|
|
|
|
2020-04-26 15:48:16 +00:00
|
|
|
// The domain name of the website on which the user has logged in.
|
|
|
|
ConnectedWebsite string `json:"connected_website,omitempty"`
|
|
|
|
|
2021-03-27 09:03:04 +00:00
|
|
|
// For a service message, a voice chat started in the chat.
|
|
|
|
VoiceChatStarted *VoiceChatStarted `json:"voice_chat_started,omitempty"`
|
|
|
|
|
|
|
|
// For a service message, a voice chat ended in the chat.
|
|
|
|
VoiceChatEnded *VoiceChatEnded `json:"voice_chat_ended,omitempty"`
|
|
|
|
|
|
|
|
// For a service message, some users were invited in the voice chat.
|
|
|
|
VoiceChatParticipants *VoiceChatParticipants `json:"voice_chat_participants_invited,omitempty"`
|
2021-05-23 16:55:49 +00:00
|
|
|
|
2021-07-25 12:46:42 +00:00
|
|
|
// For a service message, a voice chat schedule in the chat.
|
|
|
|
VoiceChatScheduled *VoiceChatScheduled `json:"voice_chat_scheduled,omitempty"`
|
|
|
|
|
2021-05-23 16:55:49 +00:00
|
|
|
// For a service message, represents the content of a service message,
|
|
|
|
// sent whenever a user in the chat triggers a proximity alert set by another user.
|
|
|
|
ProximityAlert *ProximityAlert `json:"proximity_alert_triggered,omitempty"`
|
2021-05-23 19:13:39 +00:00
|
|
|
|
|
|
|
// For a service message, represents about a change in auto-delete timer settings.
|
|
|
|
AutoDeleteTimer *AutoDeleteTimer `json:"message_auto_delete_timer_changed,omitempty"`
|
2021-08-19 13:40:07 +00:00
|
|
|
|
|
|
|
// Inline keyboard attached to the message.
|
2021-11-06 18:31:50 +00:00
|
|
|
ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
|
2021-05-23 19:13:39 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 13:10:18 +00:00
|
|
|
// MessageEntity object represents "special" parts of text messages,
|
|
|
|
// including hashtags, usernames, URLs, etc.
|
|
|
|
type MessageEntity struct {
|
|
|
|
// Specifies entity type.
|
|
|
|
Type EntityType `json:"type"`
|
|
|
|
|
|
|
|
// Offset in UTF-16 code units to the start of the entity.
|
|
|
|
Offset int `json:"offset"`
|
|
|
|
|
|
|
|
// Length of the entity in UTF-16 code units.
|
|
|
|
Length int `json:"length"`
|
|
|
|
|
|
|
|
// (Optional) For EntityTextLink entity type only.
|
|
|
|
//
|
|
|
|
// URL will be opened after user taps on the text.
|
|
|
|
URL string `json:"url,omitempty"`
|
|
|
|
|
|
|
|
// (Optional) For EntityTMention entity type only.
|
|
|
|
User *User `json:"user,omitempty"`
|
2020-04-26 15:48:16 +00:00
|
|
|
|
|
|
|
// (Optional) For EntityCodeBlock entity type only.
|
|
|
|
Language string `json:"language,omitempty"`
|
2017-11-17 13:10:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-06 20:57:25 +00:00
|
|
|
// Entities is used to set message's text entities as a send option.
|
|
|
|
type Entities []MessageEntity
|
|
|
|
|
2021-11-07 16:28:21 +00:00
|
|
|
// ProximityAlert sent whenever a user in the chat triggers
|
|
|
|
// a proximity alert set by another user.
|
|
|
|
type ProximityAlert struct {
|
|
|
|
Traveler *User `json:"traveler,omitempty"`
|
|
|
|
Watcher *User `json:"watcher,omitempty"`
|
|
|
|
Distance int `json:"distance"`
|
|
|
|
}
|
|
|
|
|
2021-07-25 13:07:48 +00:00
|
|
|
// AutoDeleteTimer represents a service message about a change in auto-delete timer settings.
|
|
|
|
type AutoDeleteTimer struct {
|
|
|
|
Unixtime int `json:"message_auto_delete_time"`
|
|
|
|
}
|
|
|
|
|
2017-11-19 15:16:39 +00:00
|
|
|
// MessageSig satisfies Editable interface (see Editable.)
|
2017-12-26 22:39:15 +00:00
|
|
|
func (m *Message) MessageSig() (string, int64) {
|
|
|
|
return strconv.Itoa(m.ID), m.Chat.ID
|
2017-11-18 18:47:04 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 09:05:50 +00:00
|
|
|
// Time returns the moment of message creation in local time.
|
2016-01-22 11:38:45 +00:00
|
|
|
func (m *Message) Time() time.Time {
|
2017-11-19 15:16:39 +00:00
|
|
|
return time.Unix(m.Unixtime, 0)
|
2015-06-27 17:37:22 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 01:44:31 +00:00
|
|
|
// LastEdited returns time.Time of last edit.
|
|
|
|
func (m *Message) LastEdited() time.Time {
|
2017-11-19 15:16:39 +00:00
|
|
|
return time.Unix(m.LastEdit, 0)
|
2017-11-19 01:44:31 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 17:37:22 +00:00
|
|
|
// IsForwarded says whether message is forwarded copy of another
|
|
|
|
// message or not.
|
2016-01-22 11:38:45 +00:00
|
|
|
func (m *Message) IsForwarded() bool {
|
2017-12-25 12:34:09 +00:00
|
|
|
return m.OriginalSender != nil || m.OriginalChat != nil
|
2015-06-27 17:37:22 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 13:06:20 +00:00
|
|
|
// IsReply says whether message is a reply to another message.
|
2016-01-22 11:38:45 +00:00
|
|
|
func (m *Message) IsReply() bool {
|
2015-10-13 14:31:51 +00:00
|
|
|
return m.ReplyTo != nil
|
2015-06-27 17:37:22 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 15:16:39 +00:00
|
|
|
// Private returns true, if it's a personal message.
|
2017-11-18 13:06:20 +00:00
|
|
|
func (m *Message) Private() bool {
|
|
|
|
return m.Chat.Type == ChatPrivate
|
|
|
|
}
|
|
|
|
|
2020-06-09 20:28:28 +00:00
|
|
|
// FromGroup returns true, if message came from a group OR a supergroup.
|
2017-11-18 13:06:20 +00:00
|
|
|
func (m *Message) FromGroup() bool {
|
|
|
|
return m.Chat.Type == ChatGroup || m.Chat.Type == ChatSuperGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromChannel returns true, if message came from a channel.
|
|
|
|
func (m *Message) FromChannel() bool {
|
|
|
|
return m.Chat.Type == ChatChannel
|
2015-06-27 17:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsService returns true, if message is a service message,
|
|
|
|
// returns false otherwise.
|
|
|
|
//
|
|
|
|
// Service messages are automatically sent messages, which
|
|
|
|
// typically occur on some global action. For instance, when
|
|
|
|
// anyone leaves the chat or chat title changes.
|
2020-06-19 20:34:37 +00:00
|
|
|
//
|
2016-01-22 11:38:45 +00:00
|
|
|
func (m *Message) IsService() bool {
|
2017-11-18 13:06:20 +00:00
|
|
|
fact := false
|
|
|
|
|
2017-11-24 15:24:07 +00:00
|
|
|
fact = fact || m.UserJoined != nil
|
2017-11-26 01:26:07 +00:00
|
|
|
fact = fact || len(m.UsersJoined) > 0
|
2017-11-24 15:24:07 +00:00
|
|
|
fact = fact || m.UserLeft != nil
|
2017-11-26 01:26:07 +00:00
|
|
|
fact = fact || m.NewGroupTitle != ""
|
|
|
|
fact = fact || m.NewGroupPhoto != nil
|
|
|
|
fact = fact || m.GroupPhotoDeleted
|
2017-11-24 15:24:07 +00:00
|
|
|
fact = fact || m.GroupCreated || m.SuperGroupCreated
|
2017-11-18 14:41:23 +00:00
|
|
|
fact = fact || (m.MigrateTo != m.MigrateFrom)
|
2017-11-18 13:06:20 +00:00
|
|
|
|
|
|
|
return fact
|
2015-06-27 17:37:22 +00:00
|
|
|
}
|
2021-10-13 18:12:05 +00:00
|
|
|
|
|
|
|
// EntityText returns the substring of the message identified by the
|
|
|
|
// given MessageEntity.
|
|
|
|
//
|
|
|
|
// It's safer than manually slicing Text because Telegram uses
|
|
|
|
// UTF-16 indices whereas Go string are []byte.
|
|
|
|
//
|
|
|
|
func (m *Message) EntityText(e MessageEntity) string {
|
|
|
|
text := m.Text
|
|
|
|
if text == "" {
|
|
|
|
text = m.Caption
|
|
|
|
}
|
|
|
|
|
|
|
|
a := utf16.Encode([]rune(text))
|
|
|
|
off, end := e.Offset, e.Offset+e.Length
|
|
|
|
|
|
|
|
if off < 0 || end > len(a) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(utf16.Decode(a[off:end]))
|
|
|
|
}
|