Documentation ¶
Overview ¶
Package telebot provides a handy wrapper for interactions with Telegram bots.
Here is an example of helloworld bot implementation:
import ( "time" "github.com/tucnak/telebot" ) func main() { bot, err := telebot.NewBot("SECRET_TOKEN") if err != nil { return } messages := make(chan telebot.Message) bot.Listen(messages, 1*time.Second) for message := range messages { if message.Text == "/hi" { bot.SendMessage(message.Chat, "Hello, "+message.Sender.FirstName+"!", nil) } } }
Index ¶
- Constants
- type Audio
- type Bot
- func (b Bot) ForwardMessage(recipient Recipient, message Message) error
- func (b Bot) Listen(subscription chan<- Message, timeout time.Duration)
- func (b Bot) SendAudio(recipient Recipient, audio *Audio, options *SendOptions) error
- func (b Bot) SendChatAction(recipient Recipient, action string) error
- func (b Bot) SendDocument(recipient Recipient, doc *Document, options *SendOptions) error
- func (b Bot) SendLocation(recipient Recipient, geo *Location, options *SendOptions) error
- func (b Bot) SendMessage(recipient Recipient, message string, options *SendOptions) error
- func (b Bot) SendPhoto(recipient Recipient, photo *Photo, options *SendOptions) error
- func (b *Bot) SendSticker(recipient Recipient, sticker *Sticker, options *SendOptions) error
- func (b Bot) SendVideo(recipient Recipient, video *Video, options *SendOptions) error
- type Chat
- type Contact
- type Document
- type File
- type Location
- type Message
- type ParseMode
- type Photo
- type Recipient
- type ReplyMarkup
- type SendOptions
- type Sticker
- type Thumbnail
- type Update
- type User
- type Video
Constants ¶
const ( Typing = "typing" UploadingPhoto = "upload_photo" UploadingVideo = "upload_video" UploadingAudio = "upload_audio" UploadingDocument = "upload_document" RecordingVideo = "record_video" RecordingAudio = "record_audio" FindingLocation = "find_location" )
A bunch of available chat actions.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Audio ¶
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"` }
Audio object represents an audio file (voice note).
type Bot ¶
Bot represents a separate Telegram bot instance.
func NewBot ¶
NewBot does try to build a Bot with token `token`, which is a secret API key assigned to particular bot.
func (Bot) ForwardMessage ¶
ForwardMessage forwards a message to recipient.
func (Bot) Listen ¶
Listen periodically looks for updates and delivers new messages to subscription channel.
func (Bot) SendAudio ¶
func (b Bot) SendAudio(recipient Recipient, audio *Audio, options *SendOptions) error
SendAudio sends an audio object to recipient.
On success, audio object would be aliased to its copy on the Telegram servers, so sending the same audio object again, won't issue a new upload, but would make a use of existing file on Telegram servers.
func (Bot) SendChatAction ¶
SendChatAction updates a chat action for recipient.
Chat action is a status message that recipient would see where you typically see "Harry is typing" status message. The only difference is that bots' chat actions live only for 5 seconds and die just once the client recieves a message from the bot.
Currently, Telegram supports only a narrow range of possible actions, these are aligned as constants of this package.
func (Bot) SendDocument ¶
func (b Bot) SendDocument(recipient Recipient, doc *Document, options *SendOptions) error
SendDocument sends a general document object to recipient.
On success, document object would be aliased to its copy on the Telegram servers, so sending the same document object again, won't issue a new upload, but would make a use of existing file on Telegram servers.
func (Bot) SendLocation ¶
func (b Bot) SendLocation(recipient Recipient, geo *Location, options *SendOptions) error
SendLocation sends a general document object to recipient.
On success, video object would be aliased to its copy on the Telegram servers, so sending the same video object again, won't issue a new upload, but would make a use of existing file on Telegram servers.
func (Bot) SendMessage ¶
func (b Bot) SendMessage(recipient Recipient, message string, options *SendOptions) error
SendMessage sends a text message to recipient.
func (Bot) SendPhoto ¶
func (b Bot) SendPhoto(recipient Recipient, photo *Photo, options *SendOptions) error
SendPhoto sends a photo object to recipient.
On success, photo object would be aliased to its copy on the Telegram servers, so sending the same photo object again, won't issue a new upload, but would make a use of existing file on Telegram servers.
func (*Bot) SendSticker ¶
func (b *Bot) SendSticker(recipient Recipient, sticker *Sticker, options *SendOptions) error
SendSticker sends a general document object to recipient.
On success, sticker object would be aliased to its copy on the Telegram servers, so sending the same sticker object again, won't issue a new upload, but would make a use of existing file on Telegram servers.
func (Bot) SendVideo ¶
func (b Bot) SendVideo(recipient Recipient, video *Video, options *SendOptions) error
SendVideo sends a general document object to recipient.
On success, video object would be aliased to its copy on the Telegram servers, so sending the same video object again, won't issue a new upload, but would make a use of existing file on Telegram servers.
type Chat ¶
type Chat struct { ID int `json:"id"` Type string `json:"type"` Title string `json:"title"` FirstName string `json:"first_name"` LastName string `json:"last_name"` Username string `json:"username"` }
Chat object represents a Telegram user, bot or group chat. Title for channels and group chats Type of chat, can be either “private”, or “group”, or “channel”
func (Chat) IsGroupChat ¶
IsGroupChat returns true if chat object represents a group chat.
type Contact ¶
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"` }
Contact object represents a contact to Telegram user
type Document ¶
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"` }
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 File ¶
type File struct { FileID string `json:"file_id"` FileSize int `json:"file_size"` // contains filtered or unexported fields }
File object represents any sort of file.
func NewFile ¶
NewFile attempts to create a File object, leading to a real file on the file system, that could be uploaded later.
Notice that NewFile doesn't upload file, but only creates a descriptor for it.
type Message ¶
type Message struct { ID int `json:"message_id"` // For message sent to channels, Sender may be empty 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 Chat `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"` }
Message object represents a message.
func (Message) IsForwarded ¶
IsForwarded says whether message is forwarded copy of another message or not.
func (Message) IsPersonal ¶
IsPersonal returns true, if message is a personal message, returns false if sent to group chat.
func (Message) IsService ¶
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.
type ParseMode ¶
type ParseMode string
ParseMode determines the way client applications treat the text of the message
type Recipient ¶
type Recipient interface { // Basically, ID of the endpoint. Destination() int }
Recipient is basically any possible endpoint you can send messages to. It's usually a distinct user or a chat.
type ReplyMarkup ¶
type ReplyMarkup struct { // ForceReply forces Telegram clients to display // a reply interface to the user (act as if the user // has selected the bot‘s message and tapped "Reply"). ForceReply bool `json:"force_reply,omitempty"` // CustomKeyboard is Array of button rows, each represented by an Array of Strings. // // Note: you don't need to set HideCustomKeyboard field to show custom keyboard. CustomKeyboard [][]string `json:"keyboard,omitempty"` // Requests clients to resize the keyboard vertically for optimal fit // (e.g., make the keyboard smaller if there are just two rows of buttons). // Defaults to false, in which case the custom keyboard is always of the // same height as the app's standard keyboard. ResizeKeyboard bool `json:"resize_keyboard,omitempty"` // Requests clients to hide the keyboard as soon as it's been used. Defaults to false. OneTimeKeyboard bool `json:"one_time_keyboard,omitempty"` // Requests clients to hide the custom keyboard. // // Note: You dont need to set CustomKeyboard field to hide custom keyboard. HideCustomKeyboard bool `json:"hide_keyboard,omitempty"` // Use this param if you want to force reply from // specific users only. // // Targets: // 1) Users that are @mentioned in the text of the Message object; // 2) If the bot's message is a reply (has SendOptions.ReplyTo), // sender of the original message. Selective bool `json:"selective,omitempty"` }
type SendOptions ¶
type SendOptions struct { // If the message is a reply, original message. ReplyTo Message // See ReplyMarkup struct definition. ReplyMarkup ReplyMarkup // For text messages, disables previews for links in this message. DisableWebPagePreview bool // ParseMode controls how client apps render your message. ParseMode ParseMode }
SendOptions represents a set of custom options that could be appled to messages sent.
type Sticker ¶
type Sticker struct { File Width int `json:"width"` Height int `json:"height"` // Sticker thumbnail in .webp or .jpg format. Preview Thumbnail `json:"thumb"` }
Sticker object represents a WebP image, so-called sticker.