All message types and replying/forwarding from API covered.

pull/3/head
Ilya Kowalewski 9 years ago
parent fa51650dad
commit 43bbb6b396

@ -0,0 +1,164 @@
package telebot
import (
"time"
)
// Message object represents a message.
type Message struct {
Id int `json:"message_id"`
Sender User `json:"from"`
Unixtime int `json:"date"`
// For forwarded messages, sender of the original message.
OriginalSender User `json:"forward_from"`
// For forwarded messages, unixtime of the original message.
OriginalUnixtime int `json:"forward_date"`
// For replies, ReplyTo represents the original message.
// 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"`
// For a text message, the actual UTF-8 text of the message
Text string `json:"text"`
// For an audio recording, information about it.
Audio Audio `json:"audio"`
// For a general file, information about it.
Document Document `json:"document"`
// For a photo, available thumbnails.
Photo []Thumbnail `json:"photo"`
// For a sticker, information about it.
Sticker Sticker `json:"sticker"`
// For a video, information about it.
Video Video `json:"video"`
// For a contact, contact information itself.
Contact Contact `json:"contact"`
// For a location, its longitude and latitude.
Location Location `json:"location"`
// A group chat message belongs to, empty if personal.
Chat User `json:"chat"`
// 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.
UserJoined User `json:"new_chat_participant"`
// 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.
UserLeft User `json:"left_chat_participant"`
// For a service message, represents a new title
// for chat this message came from.
//
// Sender would lead to a User, capable of change.
NewChatTitle string `json:"new_chat_title"`
// For a service message, represents all available
// thumbnails of new chat photo.
//
// Sender would lead to a User, capable of change.
NewChatPhoto []Thumbnail `json:"new_chat_photo"`
// For a service message, true if chat photo just
// got removed.
//
// Sender would lead to a User, capable of change.
ChatPhotoDeleted bool `json:"delete_chat_photo"`
// For a service message, true if group has been created.
//
// You would recieve such a message if you are one of
// initial group chat members.
//
// Sender would lead to creator of the chat.
ChatCreated bool `json:"group_chat_created"`
}
// Time returns the moment of message creation in local time
func (m Message) Time() time.Time {
return time.Unix(int64(m.Unixtime), 0)
}
// IsForwarded says whether message is forwarded copy of another
// message or not.
func (m Message) IsForwarded() bool {
if (m.OriginalSender != User{}) {
return true
}
return false
}
// IsReply says whether message is reply to another message or not.
func (m Message) IsReply() bool {
if (m.ReplyTo != nil) {
return true
}
return false
}
// IsPersonal returns true, if message is a personal message,
// returns false if sent to group chat.
func (m Message) IsPersonal() bool {
if (m.Chat != User{}) {
return true
}
return false
}
// 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.
func (m Message) IsService() bool {
is_service := false
if (m.UserJoined != User{}) {
is_service = true
}
if (m.UserLeft != User{}) {
is_service = true
}
if m.NewChatTitle != "" {
is_service = true
}
if len(m.NewChatPhoto) > 0 {
is_service = true
}
if m.ChatPhotoDeleted {
is_service = true
}
if m.ChatCreated {
is_service = true
}
return is_service
}

@ -4,7 +4,6 @@ import (
"fmt"
"os"
"testing"
"time"
)
func TestTelebot(t *testing.T) {
@ -17,7 +16,7 @@ func TestTelebot(t *testing.T) {
t.Fatal("Could't find TELEBOT_SECRET, aborting.")
}
bot, err := Create(token)
_, err := Create(token)
if err != nil {
t.Fatal(err)
}

@ -11,17 +11,89 @@ type User struct {
Title string `json:"title"`
}
// Message object represents a message.
type Message struct {
Id int `json:"message_id"`
Sender User `json:"from"`
Unixtime int `json:"date"`
Text string `json:"text"`
Chat User `json:"chat"`
}
// Update object represents an incoming update.
type Update struct {
Id int `json:"update_id"`
Payload Message `json:"message"`
}
// File object represents any sort of file. Since you would
// only deal with specific files in the wild, this structure
// doesn't make any sense in specific application.
type File struct {
FileId int `json:"file_id"`
FileSize int `json:"file_size"`
}
// Thumbnail object represents a image/sticker of particular size.
type Thumbnail struct {
File
Width int `json:"width"`
Height int `json:"height"`
}
// Audio object represents an audio file (voice note).
type Audio struct {
File
// Duration of the recording in seconds as defined by sender.
Duration int `json:"duration"`
// MIME type of the file as defined by sender.
Mime string `json:"mime_type"`
}
// Document object represents a general file (as opposed to Photo or Audio).
// Telegram users can send files of any type of up to 1.5 GB in size.
type Document struct {
File
// Document thumbnail as defined by sender.
Preview Thumbnail `json:"thumb"`
// Original filename as defined by sender.
FileName string `json:"file_name"`
// MIME type of the file as defined by sender.
Mime string `json:"mime_type"`
}
// Sticker object represents a WebP image, so-called sticker.
type Sticker struct {
File
Width int `json:"width"`
Height int `json:"height"`
// Sticker thumbnail in .webp or .jpg format.
Preview Thumbnail `json:"thumb"`
}
// Video object represents
type Video struct {
Audio
Width int `json:"width"`
Height int `json:"height"`
// Text description of the video as defined by sender (usually empty).
Caption string `json:"caption"`
// Video thumbnail.
Preview Thumbnail `json:"thumb"`
}
type Contact struct {
PhoneNumber string `json:"phone_number"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
// Contact's username in Telegram (might be empty).
Username string `json:"user_id"`
}
type Location struct {
Longitude float32 `json:"longitude"`
Latitude float32 `json:"latitude"`
}

Loading…
Cancel
Save