🚧 Save work-in-progress state
This commit is contained in:
parent
c8f8335ced
commit
3b840126b2
|
@ -358,7 +358,7 @@ func (bot *Bot) NewLongPollingChannel(params *GetUpdatesParameters) UpdatesChann
|
||||||
return channel
|
return channel
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWebhookChannel(set, listen, serve string, params *GetUpdatesParameters) UpdatesChannel {
|
func NewWebhookChannel(listen, serve string, params *GetUpdatesParameters) UpdatesChannel {
|
||||||
if params == nil {
|
if params == nil {
|
||||||
params = &GetUpdatesParameters{
|
params = &GetUpdatesParameters{
|
||||||
Offset: 0,
|
Offset: 0,
|
||||||
|
@ -377,7 +377,7 @@ func NewWebhookChannel(set, listen, serve string, params *GetUpdatesParameters)
|
||||||
channel <- update
|
channel <- update
|
||||||
}
|
}
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Fatalln(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
39
request.go
Normal file
39
request.go
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
package telegram
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
json "github.com/pquerna/ffjson/ffjson"
|
||||||
|
http "github.com/valyala/fasthttp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (bot *Bot) request(dst []byte, method string, args *http.Args) (*Response, error) {
|
||||||
|
requestURI := fmt.Sprintf(APIEndpoint, bot.AccessToken, method)
|
||||||
|
if args != nil {
|
||||||
|
requestURI += fmt.Sprint("?", args.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
var req http.Request
|
||||||
|
var resp http.Response
|
||||||
|
|
||||||
|
req.Header.SetMethod("POST")
|
||||||
|
req.Header.SetContentType("application/json")
|
||||||
|
req.SetRequestURI(requestURI)
|
||||||
|
req.SetBody(dst)
|
||||||
|
|
||||||
|
if err := http.Do(&req, &resp); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var data Response
|
||||||
|
if err := json.Unmarshal(resp.Body(), &data); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !data.Ok {
|
||||||
|
return nil, errors.New(data.Description)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &data, nil
|
||||||
|
}
|
98
send_sticker.go
Normal file
98
send_sticker.go
Normal file
|
@ -0,0 +1,98 @@
|
||||||
|
package telegram
|
||||||
|
|
||||||
|
/*
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"mime/multipart"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
json "github.com/pquerna/ffjson/ffjson"
|
||||||
|
http "github.com/valyala/fasthttp"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SendStickerParameters struct {
|
||||||
|
// Unique identifier for the target chat
|
||||||
|
ChatID int64 `json:"chat_id"`
|
||||||
|
|
||||||
|
// Sticker to send. Pass a file_id as String to send a file that exists on
|
||||||
|
// the Telegram servers (recommended), pass an HTTP URL as a String for
|
||||||
|
// Telegram to get a .webp file from the Internet, or upload a new one using
|
||||||
|
// multipart/form-data.
|
||||||
|
Sticker *InputFile `json:"sticker"`
|
||||||
|
|
||||||
|
// Sends the message silently. Users will receive a notification with no
|
||||||
|
// sound.
|
||||||
|
DisableNotification bool `json:"disable_notification,omitempty"`
|
||||||
|
|
||||||
|
// If the message is a reply, ID of the original message
|
||||||
|
ReplyToMessageID int `json:"reply_to_message_id,omitempty"`
|
||||||
|
|
||||||
|
// Additional interface options. A JSON-serialized object for an inline
|
||||||
|
// keyboard, custom reply keyboard, instructions to remove reply keyboard or
|
||||||
|
// to force a reply from the user.
|
||||||
|
ReplyMarkup *ReplyMarkup `json:"reply_markup,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSticker(chatID int64, sticker interface{}) *SendStickerParameters {
|
||||||
|
params := &SendStickerParameters{ChatID: chatID}
|
||||||
|
|
||||||
|
if sticker != nil {
|
||||||
|
var input InputFile = file
|
||||||
|
params.Sticker = &input
|
||||||
|
}
|
||||||
|
|
||||||
|
return params
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendSticker send .webp stickers. On success, the sent Message is returned.
|
||||||
|
func (bot *Bot) SendSticker(params *SendStickerParameters) (*Message, error) {
|
||||||
|
var args http.Args
|
||||||
|
args.Add("chat_id", params.ChatID)
|
||||||
|
args.Add("disable_notification", strconv.FormatBool(params.DisableNotification))
|
||||||
|
|
||||||
|
if params.ReplyToMessageID > 0 {
|
||||||
|
args.Add("reply_to_message_id", strconv.Itoa(params.ReplyToMessageID))
|
||||||
|
}
|
||||||
|
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
multi := multipart.NewWriter(&buffer)
|
||||||
|
|
||||||
|
sticker := *params.Sticker
|
||||||
|
switch file := sticker.(type) {
|
||||||
|
case string:
|
||||||
|
f, err := os.Open(file)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
formFile, err := multi.CreateFormFile("sticker", f.Name())
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if _, err = io.Copy(formFile, f); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
multi.Close()
|
||||||
|
case []byte:
|
||||||
|
file
|
||||||
|
bytes.NewReader(file).
|
||||||
|
default:
|
||||||
|
return false, errors.New("use string only (for current version of go-telegram)")
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := bot.upload(buffer.Bytes(), multi.Boundary(), "setWebhook", &args)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var data bool
|
||||||
|
err = json.Unmarshal(*resp.Result, &data)
|
||||||
|
return data, err
|
||||||
|
}
|
||||||
|
*/
|
|
@ -1,12 +1,7 @@
|
||||||
package telegram
|
package telegram
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"mime/multipart"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -73,33 +68,7 @@ func (bot *Bot) SetWebhook(params *SetWebhookParameters) (bool, error) {
|
||||||
args.Add("max_connections", strconv.Itoa(params.MaxConnections))
|
args.Add("max_connections", strconv.Itoa(params.MaxConnections))
|
||||||
}
|
}
|
||||||
|
|
||||||
var buffer bytes.Buffer
|
resp, err := bot.upload(*params.Certificate, "certificate", "cert.pem", "setWebhook", &args)
|
||||||
multi := multipart.NewWriter(&buffer)
|
|
||||||
|
|
||||||
if params.Certificate != nil {
|
|
||||||
cert := *params.Certificate
|
|
||||||
switch file := cert.(type) {
|
|
||||||
case string:
|
|
||||||
f, err := os.Open(file)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
formFile, err := multi.CreateFormFile("certificate", f.Name())
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
if _, err = io.Copy(formFile, f); err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
multi.Close()
|
|
||||||
default:
|
|
||||||
return false, errors.New("use string only (for current version of go-telegram)")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := bot.upload(buffer.Bytes(), multi.Boundary(), "setWebhook", &args)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
69
telegram.go
69
telegram.go
|
@ -1,76 +1,7 @@
|
||||||
// Version of the bot API: 3.4 (October 11, 2017)
|
// Version of the bot API: 3.4 (October 11, 2017)
|
||||||
package telegram
|
package telegram
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
json "github.com/pquerna/ffjson/ffjson"
|
|
||||||
http "github.com/valyala/fasthttp"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
APIEndpoint = "https://api.telegram.org/bot%s/%s"
|
APIEndpoint = "https://api.telegram.org/bot%s/%s"
|
||||||
FileEndpoind = "https://api.telegram.org/file/bot%s/%s"
|
FileEndpoind = "https://api.telegram.org/file/bot%s/%s"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (bot *Bot) request(dst []byte, method string, args *http.Args) (*Response, error) {
|
|
||||||
requestURI := fmt.Sprintf(APIEndpoint, bot.AccessToken, method)
|
|
||||||
if args != nil {
|
|
||||||
requestURI += fmt.Sprint("?", args.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
var req http.Request
|
|
||||||
var resp http.Response
|
|
||||||
|
|
||||||
req.Header.SetMethod("POST")
|
|
||||||
req.Header.SetContentType("application/json")
|
|
||||||
req.SetRequestURI(requestURI)
|
|
||||||
req.SetBody(dst)
|
|
||||||
|
|
||||||
if err := http.Do(&req, &resp); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var data Response
|
|
||||||
if err := json.Unmarshal(resp.Body(), &data); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !data.Ok {
|
|
||||||
return nil, errors.New(data.Description)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &data, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bot *Bot) upload(dst []byte, boundary, method string, args *http.Args) (*Response, error) {
|
|
||||||
requestURI := fmt.Sprintf(APIEndpoint, bot.AccessToken, method)
|
|
||||||
if args != nil {
|
|
||||||
requestURI += fmt.Sprint("?", args.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
var req http.Request
|
|
||||||
var resp http.Response
|
|
||||||
|
|
||||||
req.Header.SetMethod("POST")
|
|
||||||
req.Header.SetContentType("multipart/form-data")
|
|
||||||
req.Header.SetMultipartFormBoundary(boundary)
|
|
||||||
req.SetRequestURI(requestURI)
|
|
||||||
req.SetBody(dst)
|
|
||||||
|
|
||||||
if err := http.Do(&req, &resp); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var data Response
|
|
||||||
if err := json.Unmarshal(resp.Body(), &data); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !data.Ok {
|
|
||||||
return nil, errors.New(data.Description)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &data, nil
|
|
||||||
}
|
|
||||||
|
|
88
upload.go
Normal file
88
upload.go
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
package telegram
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"mime/multipart"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
json "github.com/pquerna/ffjson/ffjson"
|
||||||
|
http "github.com/valyala/fasthttp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// There are three ways to send files (photos, stickers, audio, media, etc.):
|
||||||
|
//
|
||||||
|
// 1. If the file is already stored somewhere on the Telegram servers, you don't need to reupload it: 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 HTTP URL 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 browser. 10 MB max size for photos, 50 MB for other files.
|
||||||
|
func (bot *Bot) upload(file InputFile, fieldName, fileName, method string, args *http.Args) (*Response, error) {
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
multi := multipart.NewWriter(&buffer)
|
||||||
|
defer multi.Close()
|
||||||
|
|
||||||
|
switch source := file.(type) {
|
||||||
|
case string:
|
||||||
|
f, err := os.Open(source)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
formFile, err := multi.CreateFormFile(fieldName, f.Name())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if _, err = io.Copy(formFile, f); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
case []byte:
|
||||||
|
formFile, err := multi.CreateFormFile(fieldName, fileName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if _, err = io.Copy(formFile, bytes.NewReader(source)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
case *url.URL:
|
||||||
|
if err := multi.WriteField(fieldName, source.String()); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
case io.Reader:
|
||||||
|
multi.CreateFormFile(fieldName, fileName)
|
||||||
|
default:
|
||||||
|
return nil, errors.New("bad file type")
|
||||||
|
}
|
||||||
|
|
||||||
|
requestURI := fmt.Sprintf(APIEndpoint, bot.AccessToken, method)
|
||||||
|
if args != nil {
|
||||||
|
requestURI += fmt.Sprint("?", args.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
var req http.Request
|
||||||
|
var resp http.Response
|
||||||
|
|
||||||
|
req.Header.SetMethod("POST")
|
||||||
|
req.Header.SetContentType("multipart/form-data")
|
||||||
|
req.Header.SetMultipartFormBoundary(multi.Boundary())
|
||||||
|
args.WriteTo(req.BodyWriter())
|
||||||
|
req.SetRequestURI(requestURI)
|
||||||
|
req.SetBody(buffer.Bytes())
|
||||||
|
|
||||||
|
if err := http.Do(&req, &resp); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var data Response
|
||||||
|
if err := json.Unmarshal(resp.Body(), &data); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !data.Ok {
|
||||||
|
return nil, errors.New(data.Description)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &data, nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user