Skip to content

Commit

Permalink
Tiny code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tucnak committed Nov 16, 2015
1 parent 4b7ee3b commit 651cd22
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
22 changes: 13 additions & 9 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"strconv"
)

func sendCommand(method string, token string, params url.Values) ([]byte, error) {
func sendCommand(method, token string, params url.Values) ([]byte, error) {
url := fmt.Sprintf("https://api.telegram.org/bot%s/%s?%s",
token, method, params.Encode())

Expand Down Expand Up @@ -75,7 +75,7 @@ func sendFile(method, token, name, path string, params url.Values) ([]byte, erro
}

if resp.StatusCode == http.StatusInternalServerError {
return []byte{}, fmt.Errorf("Telegram API returned 500 internal server error")
return []byte{}, fmt.Errorf("telegram: internal server error")
}

json, err := ioutil.ReadAll(resp.Body)
Expand Down Expand Up @@ -103,10 +103,15 @@ func embedSendOptions(params *url.Values, options *SendOptions) {
params.Set("parse_mode", string(options.ParseMode))
}

// process reply_markup
if options.ReplyMarkup.ForceReply || options.ReplyMarkup.CustomKeyboard != nil || options.ReplyMarkup.HideCustomKeyboard {
replyMarkup, _ := json.Marshal(options.ReplyMarkup)
params.Set("reply_markup", string(replyMarkup))
// Processing force_reply:
{
forceReply := options.ReplyMarkup.ForceReply
customKeyboard := (options.ReplyMarkup.CustomKeyboard != nil)
hiddenKeyboard := options.ReplyMarkup.HideCustomKeyboard
if forceReply || customKeyboard || hiddenKeyboard {
replyMarkup, _ := json.Marshal(options.ReplyMarkup)
params.Set("reply_markup", string(replyMarkup))
}
}
}

Expand Down Expand Up @@ -134,7 +139,7 @@ func getMe(token string) (User, error) {
return User{}, fmt.Errorf("telebot: %s", botInfo.Description)
}

func getUpdates(token string, offset int, timeout int) (updates []Update, err error) {
func getUpdates(token string, offset, timeout int) (upd []Update, err error) {
params := url.Values{}
params.Set("offset", strconv.Itoa(offset))
params.Set("timeout", strconv.Itoa(timeout))
Expand All @@ -159,6 +164,5 @@ func getUpdates(token string, offset int, timeout int) (updates []Update, err er
return
}

updates = updatesRecieved.Result
return
return updatesRecieved.Result, nil
}
40 changes: 24 additions & 16 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package telebot
import (
"encoding/json"
"fmt"
"log"
"net/url"
"strconv"
"time"
Expand Down Expand Up @@ -31,24 +32,31 @@ func NewBot(token string) (*Bot, error) {
}

// Listen periodically looks for updates and delivers new messages
// to subscription channel.
func (b Bot) Listen(subscription chan<- Message, timeout time.Duration) {

// to the subscription channel.
func (b *Bot) Listen(subscription chan<- Message, timeout time.Duration) {
go func() {
latestUpdate := 0
for {
if updates, err := getUpdates(b.Token, latestUpdate+1, int(timeout/time.Second)); err == nil {
for _, update := range updates {
latestUpdate = update.ID
subscription <- update.Payload
}
updates, err := getUpdates(b.Token,
latestUpdate+1,
int(timeout/time.Second),
)

if err != nil {
log.Println("failed to get updates:", err)
continue
}

for _, update := range updates {
latestUpdate = update.ID
subscription <- update.Payload
}
}
}()
}

// SendMessage sends a text message to recipient.
func (b Bot) SendMessage(recipient Recipient, message string, options *SendOptions) error {
func (b *Bot) SendMessage(recipient Recipient, message string, options *SendOptions) error {
params := url.Values{}
params.Set("chat_id", strconv.Itoa(recipient.Destination()))
params.Set("text", message)
Expand Down Expand Up @@ -80,7 +88,7 @@ func (b Bot) SendMessage(recipient Recipient, message string, options *SendOptio
}

// ForwardMessage forwards a message to recipient.
func (b Bot) ForwardMessage(recipient Recipient, message Message) error {
func (b *Bot) ForwardMessage(recipient Recipient, message Message) error {
params := url.Values{}
params.Set("chat_id", strconv.Itoa(recipient.Destination()))
params.Set("from_chat_id", strconv.Itoa(message.Origin().ID))
Expand Down Expand Up @@ -114,7 +122,7 @@ func (b Bot) ForwardMessage(recipient Recipient, message Message) error {
// 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 (b Bot) SendPhoto(recipient Recipient, photo *Photo, options *SendOptions) error {
func (b *Bot) SendPhoto(recipient Recipient, photo *Photo, options *SendOptions) error {
params := url.Values{}
params.Set("chat_id", strconv.Itoa(recipient.Destination()))
params.Set("caption", photo.Caption)
Expand Down Expand Up @@ -167,7 +175,7 @@ func (b Bot) SendPhoto(recipient Recipient, photo *Photo, options *SendOptions)
// 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 Recipient, audio *Audio, options *SendOptions) error {
func (b *Bot) SendAudio(recipient Recipient, audio *Audio, options *SendOptions) error {
params := url.Values{}
params.Set("chat_id", strconv.Itoa(recipient.Destination()))

Expand Down Expand Up @@ -218,7 +226,7 @@ func (b Bot) SendAudio(recipient Recipient, audio *Audio, options *SendOptions)
// 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 Recipient, doc *Document, options *SendOptions) error {
func (b *Bot) SendDocument(recipient Recipient, doc *Document, options *SendOptions) error {
params := url.Values{}
params.Set("chat_id", strconv.Itoa(recipient.Destination()))

Expand Down Expand Up @@ -320,7 +328,7 @@ func (b *Bot) SendSticker(recipient Recipient, sticker *Sticker, options *SendOp
// 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 Recipient, video *Video, options *SendOptions) error {
func (b *Bot) SendVideo(recipient Recipient, video *Video, options *SendOptions) error {
params := url.Values{}
params.Set("chat_id", strconv.Itoa(recipient.Destination()))

Expand Down Expand Up @@ -371,7 +379,7 @@ func (b Bot) SendVideo(recipient Recipient, video *Video, options *SendOptions)
// 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 Recipient, geo *Location, options *SendOptions) error {
func (b *Bot) SendLocation(recipient Recipient, geo *Location, options *SendOptions) error {
params := url.Values{}
params.Set("chat_id", strconv.Itoa(recipient.Destination()))
params.Set("latitude", fmt.Sprintf("%f", geo.Latitude))
Expand Down Expand Up @@ -414,7 +422,7 @@ func (b Bot) SendLocation(recipient Recipient, geo *Location, options *SendOptio
//
// Currently, Telegram supports only a narrow range of possible
// actions, these are aligned as constants of this package.
func (b Bot) SendChatAction(recipient Recipient, action string) error {
func (b *Bot) SendChatAction(recipient Recipient, action string) error {
params := url.Values{}
params.Set("chat_id", strconv.Itoa(recipient.Destination()))
params.Set("action", action)
Expand Down

0 comments on commit 651cd22

Please sign in to comment.