1
0
Fork 0

🏗️ Use fasthttp.URI instead url.URL

This commit is contained in:
Maxim Lebedev 2018-10-12 16:14:58 +05:00
parent fe4fd382d5
commit feafdf6c07
No known key found for this signature in database
GPG Key ID: F8978F46FF0FFA4F
6 changed files with 48 additions and 63 deletions

View File

@ -2,14 +2,23 @@ package test
import (
"os"
"path"
"testing"
"gitlab.com/toby3d/telegram"
http "github.com/valyala/fasthttp"
tg "gitlab.com/toby3d/telegram"
)
var bot = new(telegram.Bot)
var bot = new(tg.Bot)
func TestMain(m *testing.M) {
photoURL = http.AcquireURI()
defer http.ReleaseURI(photoURL)
photoURL.SetScheme("https")
photoURL.SetHost("simg3.gelbooru.com")
photoURL.SetPath(path.Join("images", "46", "24", "46246c1b8c4fcc37050085a850c165c4.jpg"))
bot.AccessToken = os.Getenv("BOT_ACCESS_TOKEN")
os.Exit(m.Run())
}

View File

@ -1,10 +1,10 @@
package test
import (
"net/url"
"testing"
"gitlab.com/toby3d/telegram"
http "github.com/valyala/fasthttp"
tg "gitlab.com/toby3d/telegram"
)
const (
@ -18,18 +18,13 @@ const (
)
var (
photoURL = url.URL{
Scheme: "https",
Host: "simg3.gelbooru.com",
Path: "/images/46/24/46246c1b8c4fcc37050085a850c165c4.jpg",
}
photoURL *http.URI
messageID int
)
func TestSendPhoto(t *testing.T) {
resp, err := bot.SendPhoto(
telegram.NewPhoto(chatID, photoFileID),
tg.NewPhoto(chatID, photoFileID),
)
if err != nil {
t.Error(err.Error())
@ -43,7 +38,7 @@ func TestSendPhoto(t *testing.T) {
func TestSendDocument(t *testing.T) {
resp, err := bot.SendDocument(
telegram.NewDocument(chatID, documentFileID),
tg.NewDocument(chatID, documentFileID),
)
if err != nil {
t.Error(err.Error())
@ -55,10 +50,10 @@ func TestSendDocument(t *testing.T) {
func TestSendMediaGroup(t *testing.T) {
resp, err := bot.SendMediaGroup(
telegram.NewMediaGroup(
tg.NewMediaGroup(
chatID,
telegram.NewInputMediaPhoto(photoFileID),
telegram.NewInputMediaPhoto(photoURL.String()),
tg.NewInputMediaPhoto(photoFileID),
tg.NewInputMediaPhoto(photoURL.String()),
),
)
if err != nil {
@ -71,7 +66,7 @@ func TestSendMediaGroup(t *testing.T) {
func TestSendLocation(t *testing.T) {
resp, err := bot.SendLocation(
telegram.NewLocation(chatID, 36.724510, 139.268181),
tg.NewLocation(chatID, 36.724510, 139.268181),
)
if err != nil {
t.Error(err.Error())
@ -83,7 +78,7 @@ func TestSendLocation(t *testing.T) {
func TestSendVenue(t *testing.T) {
resp, err := bot.SendVenue(
telegram.NewVenue(chatID, 36.724510, 139.268181, "Japan", "Japan"),
tg.NewVenue(chatID, 36.724510, 139.268181, "Japan", "Japan"),
)
if err != nil {
t.Error(err.Error())
@ -95,7 +90,7 @@ func TestSendVenue(t *testing.T) {
func TestSendContact(t *testing.T) {
resp, err := bot.SendContact(
telegram.NewContact(chatID, "+42410", "Telegram"),
tg.NewContact(chatID, "+42410", "Telegram"),
)
if err != nil {
t.Error(err.Error())

View File

@ -3,10 +3,8 @@ package telegram
import (
"bytes"
"errors"
"fmt"
"io"
"mime/multipart"
"net/url"
"os"
"path"
"strconv"
@ -27,7 +25,7 @@ media, etc.):
each file object has a file_id field, simply pass this file_id as a parameter instead of uploading.
There are no limits for files sent this way.
2. Provide Telegram with an *url.URL for the file to be sent. Telegram will download and send the
2. Provide Telegram with an *fasthttp.URI for the file to be sent. Telegram will download and send the
file. 5 MB max size for photos and 20 MB max for other types of content.
3. Post the file using multipart/form-data in the usual way that files are uploaded via the
@ -46,7 +44,7 @@ as a photo, a photo can't be sent as a document, etc.
Sending by URL
* When sending by *url.URL the target file must have the correct MIME type (e.g., audio/mpeg for
* When sending by *fasthttp.URI the target file must have the correct MIME type (e.g., audio/mpeg for
sendAudio, etc.).
* In sendDocument, sending by URL will currently only work for gif, pdf and zip files.
@ -56,7 +54,7 @@ voice notes will be sent as files.
* Other configurations may work but we can't guarantee that they will.
*/
func (bot *Bot) Upload(method, key, name string, file InputFile, args fmt.Stringer) (response *Response, err error) {
func (bot *Bot) Upload(method, key, name string, file InputFile, args *http.Args) (response *Response, err error) {
buffer := bytes.NewBuffer(nil)
multi := multipart.NewWriter(buffer)
@ -65,16 +63,9 @@ func (bot *Bot) Upload(method, key, name string, file InputFile, args fmt.String
requestURI.SetHost("api.telegram.org")
requestURI.SetPath(path.Join("bot"+bot.AccessToken, method))
query, err := url.ParseQuery(args.String())
if err != nil {
return
}
for key, val := range query {
if err = multi.WriteField(key, val[0]); err != nil {
return nil, err
}
}
args.VisitAll(func(key, value []byte) {
multi.WriteField(string(key), string(value))
})
if err = createFileField(multi, file, key, name); err != nil {
return
@ -123,7 +114,7 @@ func createFileField(w *multipart.Writer, file interface{}, key, val string) err
switch src := file.(type) {
case string: // Send FileID of file on disk path
err = createFileFieldString(w, key, src)
case *url.URL: // Send by URL
case *http.URI: // Send by URL
err = w.WriteField(key, src.String())
case []byte: // Upload new
err = createFileFieldRaw(w, key, val, bytes.NewReader(src))

View File

@ -1,8 +1,6 @@
package telegram
import (
http "github.com/valyala/fasthttp"
)
import http "github.com/valyala/fasthttp"
// NewForceReply calls the response interface to the message.
func NewForceReply() *ForceReply {

View File

@ -1,7 +1,6 @@
package telegram
import (
"net/url"
"path"
"strings"
@ -141,25 +140,24 @@ func (b *Bot) NewFileURL(filePath string) *http.URI {
}
// NewRedirectURL creates new url.URL for redirecting from one chat to another.
func (b *Bot) NewRedirectURL(param string, group bool) *url.URL {
func (b *Bot) NewRedirectURL(param string, group bool) *http.URI {
if b == nil || b.User == nil || b.User.Username == "" {
return nil
}
link := &url.URL{
Scheme: "https",
Host: "t.me",
Path: b.User.Username,
}
link := http.AcquireURI()
link.SetScheme("https")
link.SetHost("t.me")
link.SetPath(b.User.Username)
q := link.Query()
q := link.QueryArgs()
key := "start"
if group {
key += "group"
}
q.Add(key, param)
q.Set(key, param)
link.RawQuery = q.Encode()
link.SetQueryStringBytes(q.QueryString())
return link
}

View File

@ -1,12 +1,13 @@
package telegram
import (
"net/url"
"strings"
http "github.com/valyala/fasthttp"
)
// ParseURL selects URL from entered text of message and parse it as url.URL.
func (e *MessageEntity) ParseURL(messageText string) *url.URL {
// ParseURL selects URL from entered text of message and parse it as fasthttp.URI.
func (e *MessageEntity) ParseURL(messageText string) *http.URI {
if e == nil || !e.IsURL() || messageText == "" {
return nil
}
@ -18,13 +19,8 @@ func (e *MessageEntity) ParseURL(messageText string) *url.URL {
return nil
}
link, err := url.Parse(string(text[from:to]))
if err == nil && link.Scheme == "" {
link, err = url.Parse("http://" + link.String())
}
if err != nil {
return nil
}
link := http.AcquireURI()
link.Update(string(text[from:to]))
return link
}
@ -84,16 +80,14 @@ func (e *MessageEntity) IsURL() bool {
return e != nil && strings.EqualFold(e.Type, EntityURL)
}
// TextLink parse current text link entity as url.URL.
func (e *MessageEntity) TextLink() *url.URL {
// TextLink parse current text link entity as fasthttp.URI.
func (e *MessageEntity) TextLink() *http.URI {
if e == nil {
return nil
}
link, err := url.Parse(e.URL)
if err != nil {
return nil
}
link := http.AcquireURI()
link.Update(e.URL)
return link
}