2015-06-26 07:34:10 +00:00
|
|
|
package telebot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-06-27 15:44:12 +00:00
|
|
|
// Bot represents a separate Telegram bot instance.
|
2015-06-26 07:34:10 +00:00
|
|
|
type Bot struct {
|
|
|
|
Token string
|
|
|
|
|
|
|
|
// Bot as `User` on API level.
|
|
|
|
Identity User
|
|
|
|
}
|
|
|
|
|
2015-06-27 15:44:12 +00:00
|
|
|
// Listen periodically looks for updates and delivers new messages
|
|
|
|
// to subscription channel.
|
2015-06-27 10:30:25 +00:00
|
|
|
func (b *Bot) Listen(subscription chan<- Message, interval time.Duration) {
|
|
|
|
updates := make(chan Update)
|
2015-06-26 07:34:10 +00:00
|
|
|
pulse := time.NewTicker(interval)
|
2015-06-27 10:30:25 +00:00
|
|
|
latest_update := 0
|
|
|
|
|
2015-06-26 07:34:10 +00:00
|
|
|
go func() {
|
|
|
|
for range pulse.C {
|
|
|
|
go api_getUpdates(b.Token,
|
|
|
|
latest_update+1,
|
|
|
|
updates)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-06-27 10:30:25 +00:00
|
|
|
go func() {
|
|
|
|
for update := range updates {
|
|
|
|
if update.Id > latest_update {
|
|
|
|
latest_update = update.Id
|
|
|
|
}
|
2015-06-26 07:34:10 +00:00
|
|
|
|
2015-06-27 10:30:25 +00:00
|
|
|
subscription <- update.Payload
|
2015-06-26 07:34:10 +00:00
|
|
|
}
|
2015-06-27 10:30:25 +00:00
|
|
|
}()
|
2015-06-26 07:34:10 +00:00
|
|
|
}
|
2015-06-26 16:12:54 +00:00
|
|
|
|
2015-07-02 11:04:45 +00:00
|
|
|
// SendMessage sends a text message to recipient.
|
|
|
|
func (b *Bot) SendMessage(recipient User, message string) error {
|
|
|
|
return api_sendMessage(b.Token, recipient, message)
|
2015-06-26 16:12:54 +00:00
|
|
|
}
|
2015-07-02 09:05:50 +00:00
|
|
|
|
2015-07-02 11:04:45 +00:00
|
|
|
// ForwardMessage forwards a message to recipient.
|
|
|
|
func (b *Bot) ForwardMessage(recipient User, message Message) error {
|
|
|
|
return api_forwardMessage(b.Token, recipient, message)
|
2015-07-02 09:05:50 +00:00
|
|
|
}
|
2015-07-02 18:39:39 +00:00
|
|
|
|
|
|
|
// SendPhoto sends a photo object to recipient.
|
2015-07-03 12:27:18 +00:00
|
|
|
//
|
|
|
|
// 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.
|
2015-07-02 18:39:39 +00:00
|
|
|
func (b *Bot) SendPhoto(recipient User, photo *Photo) error {
|
|
|
|
return api_sendPhoto(b.Token, recipient, photo)
|
|
|
|
}
|
2015-07-03 12:27:18 +00:00
|
|
|
|
2015-07-03 16:25:04 +00:00
|
|
|
// SendAudio sends an audio object to recipient.
|
2015-07-03 12:27:18 +00:00
|
|
|
//
|
|
|
|
// 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 (b *Bot) SendAudio(recipient User, audio *Audio) error {
|
|
|
|
return api_sendAudio(b.Token, recipient, audio)
|
|
|
|
}
|
2015-07-03 16:25:04 +00:00
|
|
|
|
|
|
|
// 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 (b *Bot) SendDocument(recipient User, doc *Document) error {
|
|
|
|
return api_sendDocument(b.Token, recipient, doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (b *Bot) SendSticker(recipient User, sticker *Sticker) error {
|
|
|
|
return api_sendSticker(b.Token, recipient, sticker)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (b *Bot) SendVideo(recipient User, video *Video) error {
|
|
|
|
return api_sendVideo(b.Token, recipient, video)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (b *Bot) SendLocation(recipient User, geo *Location) error {
|
|
|
|
return api_sendLocation(b.Token, recipient, geo)
|
|
|
|
}
|