Merge pull request #220 from mitinarseny/v2

Avoid using fmt.Sprintf for better performance
pull/225/head
Ian Byrd 5 years ago committed by GitHub
commit abf4d26318
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,7 +3,6 @@ package telebot
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"mime/multipart" "mime/multipart"
@ -19,7 +18,7 @@ import (
// Raw lets you call any method of Bot API manually. // Raw lets you call any method of Bot API manually.
func (b *Bot) Raw(method string, payload interface{}) ([]byte, error) { func (b *Bot) Raw(method string, payload interface{}) ([]byte, error) {
url := fmt.Sprintf("%s/bot%s/%s", b.URL, b.Token, method) url := b.URL+ "/bot" + b.Token+"/"+ method
var buf bytes.Buffer var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(payload); err != nil { if err := json.NewEncoder(&buf).Encode(payload); err != nil {
@ -118,7 +117,7 @@ func (b *Bot) sendFiles(
return nil, wrapSystem(err) return nil, wrapSystem(err)
} }
url := fmt.Sprintf("%s/bot%s/%s", b.URL, b.Token, method) url := b.URL+ "/bot" + b.Token+"/"+ method
req, err := http.NewRequest("POST", url, body) req, err := http.NewRequest("POST", url, body)
if err != nil { if err != nil {
return nil, wrapSystem(err) return nil, wrapSystem(err)

@ -545,7 +545,7 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
} else if f.FileURL != "" { } else if f.FileURL != "" {
mediaRepr = f.FileURL mediaRepr = f.FileURL
} else if f.OnDisk() || f.FileReader != nil { } else if f.OnDisk() || f.FileReader != nil {
mediaRepr = fmt.Sprintf("attach://%d", i) mediaRepr = "attach://" + strconv.Itoa(i)
files[strconv.Itoa(i)] = *f files[strconv.Itoa(i)] = *f
} else { } else {
return nil, errors.Errorf( return nil, errors.Errorf(
@ -1041,8 +1041,7 @@ func (b *Bot) GetFile(file *File) (io.ReadCloser, error) {
return nil, err return nil, err
} }
url := fmt.Sprintf("%s/file/bot%s/%s", url := b.URL + "/file/bot" + b.Token + "/"+ f.FilePath
b.URL, b.Token, f.FilePath)
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
@ -1062,7 +1061,7 @@ func (b *Bot) StopLiveLocation(message Editable, options ...interface{}) (*Messa
messageID, chatID := message.MessageSig() messageID, chatID := message.MessageSig()
params := map[string]string{ params := map[string]string{
"chat_id": fmt.Sprintf("%d", chatID), "chat_id": strconv.FormatInt(chatID, 10),
"message_id": messageID, "message_id": messageID,
} }
@ -1355,7 +1354,7 @@ func (b *Bot) FileURLByID(fileID string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
return fmt.Sprintf("%s/file/bot%s/%s", b.URL, b.Token, f.FilePath), nil return b.URL + "/file/bot" + b.Token + "/"+ f.FilePath, nil
} }
// UploadStickerFile returns uploaded File on success. // UploadStickerFile returns uploaded File on success.

@ -214,7 +214,7 @@ func (x *Location) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error
"chat_id": to.Recipient(), "chat_id": to.Recipient(),
"latitude": fmt.Sprintf("%f", x.Lat), "latitude": fmt.Sprintf("%f", x.Lat),
"longitude": fmt.Sprintf("%f", x.Lng), "longitude": fmt.Sprintf("%f", x.Lng),
"live_period": fmt.Sprintf("%d", x.LivePeriod), "live_period": strconv.Itoa(x.LivePeriod),
} }
embedSendOptions(params, opt) embedSendOptions(params, opt)

@ -73,13 +73,13 @@ func (h *Webhook) getFiles() map[string]File {
func (h *Webhook) getParams() map[string]string { func (h *Webhook) getParams() map[string]string {
param := make(map[string]string) param := make(map[string]string)
if h.TLS != nil { if h.TLS != nil {
param["url"] = fmt.Sprintf("https://%s", h.Listen) param["url"] = "https://" + h.Listen
} else { } else {
// this will not work with telegram, they want TLS // this will not work with telegram, they want TLS
// but i allow this because telegram will send an error // but i allow this because telegram will send an error
// when you register this hook. in their docs they write // when you register this hook. in their docs they write
// that port 80/http is allowed ... // that port 80/http is allowed ...
param["url"] = fmt.Sprintf("http://%s", h.Listen) param["url"] = "http://" + h.Listen
} }
if h.Endpoint != nil { if h.Endpoint != nil {
param["url"] = h.Endpoint.PublicURL param["url"] = h.Endpoint.PublicURL

Loading…
Cancel
Save