A little more documentation, code cleanup

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

@ -1,6 +1,8 @@
# Telebot
>Telebot is a convenient wrapper to Telegram Bots API, written in Golang.
[![GoDoc](https://godoc.org/github.com/tucnak/telebot?status.svg)](https://godoc.org/github.com/tucnak/telebot)
Bots are special Telegram accounts designed to handle messages automatically. Users can interact with bots by sending them command messages in private or group chats. These accounts serve as an interface for code running somewhere on your server.
Telebot offers a convenient wrapper to Bots API, so you shouldn't even care about networking at all.

@ -4,6 +4,7 @@ import (
"time"
)
// Bot represents a separate Telegram bot instance.
type Bot struct {
Token string
@ -11,6 +12,8 @@ type Bot struct {
Identity User
}
// Listen periodically looks for updates and delivers new messages
// to subscription channel.
func (b *Bot) Listen(subscription chan<- Message, interval time.Duration) {
updates := make(chan Update)
pulse := time.NewTicker(interval)
@ -35,6 +38,7 @@ func (b *Bot) Listen(subscription chan<- Message, interval time.Duration) {
}()
}
// SendMessage sends a text message to recipient.
func (b *Bot) SendMessage(recipient User, message string) {
go api_sendMessage(b.Token, recipient, message)
}

@ -1,14 +1,18 @@
package telebot
// Login error, which basically occurs on broken token.
// AuthError occurs if the token appears to be invalid.
type AuthError struct {
Payload string
}
// FetchError occurs when something goes wrong
// while fetching updates.
type FetchError struct {
Payload string
}
// SendError occurs when something goes wrong
// while posting images, documents, etc.
type SendError struct {
Payload string
}

@ -1,11 +0,0 @@
package telebot
type Message struct {
Id int `json:"message_id"`
Sender User `json:"from"`
Unixtime int `json:"date"`
Text string `json:"text"`
// Equal to Sender for PM
Chat User `json:"chat"`
}

@ -1,8 +1,34 @@
// 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.Create("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+"!")
// }
// }
// }
//
package telebot
// Attempts to construct a Bot with `token` given.
// Create does try to build a Bot with token `token`, which
// is a secret API key assigned to particular bot.
func Create(token string) (Bot, error) {
user, err := api_getMe(token)
if err != nil {

@ -22,7 +22,9 @@ func TestTelebot(t *testing.T) {
t.Fatal(err)
}
messages := make(chan Message)
// TODO: Uncomment when Telegram fixes behavior for self-messaging
/*messages := make(chan Message)
intelligence := "welcome to the jungle"
@ -39,5 +41,5 @@ func TestTelebot(t *testing.T) {
case <-time.After(5 * time.Second):
t.Error("Self-handshake test took too long, aborting.")
}
}*/
}

@ -0,0 +1,27 @@
package telebot
// User object represents a Telegram user, bot or group chat.
type User struct {
Id int `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Username string `json:"username"`
// Title differs a group chat apart from users and bots.
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"`
}

@ -1,6 +0,0 @@
package telebot
type Update struct {
Id int `json:"update_id"`
Payload Message `json:"message"`
}

@ -1,13 +0,0 @@
package telebot
type User struct {
Id int
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Username string
// In case of group chat, Title will indicate
// whether it's a chat or user: if Title is empty
// it's a user, otherwise it's not.
Title string
}
Loading…
Cancel
Save