Fixes, tests improved

bot-api-6.1
Gleb Sinyavsky 2015-11-21 14:22:08 +03:00
parent c67df95407
commit 53dd9f6367
7 changed files with 101 additions and 101 deletions

8
bot.go
View File

@ -405,10 +405,10 @@ func (bot *BotAPI) UpdatesChan(config UpdateConfig) error {
}
// ListenForWebhook registers a http handler for a webhook.
func (bot *BotAPI) ListenForWebhook(pattern string) {
func (bot *BotAPI) ListenForWebhook(pattern string) http.Handler {
bot.Updates = make(chan Update, 100)
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bytes, _ := ioutil.ReadAll(r.Body)
var update Update
@ -416,6 +416,10 @@ func (bot *BotAPI) ListenForWebhook(pattern string) {
bot.Updates <- update
})
http.HandleFunc(pattern, handler)
return handler
}
// SendChatAction sets a current action in a chat.

View File

@ -3,7 +3,10 @@ package tgbotapi_test
import (
"github.com/zhulik/telegram-bot-api"
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
@ -45,6 +48,7 @@ func TestGetUpdates(t *testing.T) {
_, err = bot.GetUpdates(u)
if err != nil {
t.Log(err.Error())
t.Fail()
}
}
@ -408,6 +412,44 @@ func TestGetUserProfilePhotos(t *testing.T) {
}
}
func TestListenForWebhook(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
handler := bot.ListenForWebhook("/")
req, _ := http.NewRequest("GET", "", strings.NewReader("{}"))
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Home page didn't return %v", http.StatusOK)
}
}
func TestSetWebhook(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
wh := tgbotapi.WebhookConfig{Clear: true}
_, err = bot.SetWebhook(wh)
wh = tgbotapi.NewWebhookWithCert("https://example.com/tgbotapi-test/" + bot.Token, "tests/cert.pem")
_, err = bot.SetWebhook(wh)
if err != nil {
t.Fail()
}
wh = tgbotapi.WebhookConfig{Clear: true}
_, err = bot.SetWebhook(wh)
}
func TestUpdatesChan(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))

View File

@ -193,7 +193,7 @@ func (config PhotoConfig) Params() (map[string]string, error) {
func (config PhotoConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
v.Add("photo", config.FileID)
v.Add(config.Name(), config.FileID)
if config.Caption != "" {
v.Add("caption", config.Caption)
}
@ -219,7 +219,7 @@ type AudioConfig struct {
func (config AudioConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
v.Add("audio", config.FileID)
v.Add(config.Name(), config.FileID)
if config.Duration != 0 {
v.Add("duration", strconv.Itoa(config.Duration))
}
@ -267,7 +267,7 @@ type DocumentConfig struct {
func (config DocumentConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
v.Add("document", config.FileID)
v.Add(config.Name(), config.FileID)
return v, nil
}
@ -294,7 +294,7 @@ type StickerConfig struct {
func (config StickerConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
v.Add("sticker", config.FileID)
v.Add(config.Name(), config.FileID)
return v, nil
}
@ -323,7 +323,7 @@ type VideoConfig struct {
func (config VideoConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
v.Add("video", config.FileID)
v.Add(config.Name(), config.FileID)
if config.Duration != 0 {
v.Add("duration", strconv.Itoa(config.Duration))
}
@ -357,7 +357,7 @@ type VoiceConfig struct {
func (config VoiceConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
v.Add("voice", config.FileID)
v.Add(config.Name(), config.FileID)
if config.Duration != 0 {
v.Add("duration", strconv.Itoa(config.Duration))
}

18
tests/cert.pem 100644
View File

@ -0,0 +1,18 @@
-----BEGIN CERTIFICATE-----
MIIC0zCCAbugAwIBAgIJAPYfllX657axMA0GCSqGSIb3DQEBCwUAMAA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tDq63xOTcUoAtXXRkSgohV58npEKXVGWkQ
Kk0SjG7Fvc35XWlY0z3qZk6/AuOIqfOxcHUMEPatAtgwlH5RNo+T1EQNF/U6wotq
e9q6vp026XgEyJwt29Y+giy2ZrDaRywgiFs1d0H3t0bKyXMUopQmPJFXdesCgYEA
psCxXcDpZjxGX/zPsGZrbOdxtRtisTlg0k0rp93pO8tV90HtDHeDMT54g2ItzJPr
TMev6XOpJNPZyf6+8GhpOuO2EQkT85u2VYoCeslz95gBabvFfIzZrUZYcnw76bm8
YjAP5DN+CEfq2PyG0Df+W1ojPSvlKSCSJQMOG1vr81cCgYEAkjPY5WR99uJxYBNI
OTFMSkETgDUbPXBu/E/h5Dtn79v8Moj9FvC7+q6sg9qXhrGhfK2xDev3/sTrbS/E
Gcf8UNIne3AXsoAS8MtkOwJXHkYaTIboIYgDX4LlDmbGQlIRaWgyh2POI6VtjLBT
ms6AdsdpIB6As9xNUBUwj/RnTZQ=
-----END PRIVATE KEY-----

View File

@ -1,92 +0,0 @@
// Package tgutils provides extra functions to make certain tasks easier.
package tgutils
import (
"github.com/syfaro/telegram-bot-api"
"os"
"os/exec"
"path/filepath"
"strconv"
"sync"
"time"
)
var rand uint32
var randmu sync.Mutex
func reseed() uint32 {
return uint32(time.Now().UnixNano() + int64(os.Getpid()))
}
func nextSuffix() string {
randmu.Lock()
r := rand
if r == 0 {
r = reseed()
}
r = r*1664525 + 1013904223 // constants from Numerical Recipes
rand = r
randmu.Unlock()
return strconv.Itoa(int(1e9 + r%1e9))[1:]
}
// this function ripped from ioutils.TempFile, except with a suffix, instead of prefix.
func tempFileWithSuffix(dir, suffix string) (f *os.File, err error) {
if dir == "" {
dir = os.TempDir()
}
nconflict := 0
for i := 0; i < 10000; i++ {
name := filepath.Join(dir, nextSuffix()+suffix)
f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if os.IsExist(err) {
if nconflict++; nconflict > 10 {
randmu.Lock()
rand = reseed()
randmu.Unlock()
}
continue
}
break
}
return
}
// EncodeAudio takes a file and attempts to convert it to a .ogg for Telegram.
// It then updates the path to the audio file in the AudioConfig.
//
// This function requires ffmpeg and opusenc to be installed on the system!
func EncodeAudio(audio *tgbotapi.AudioConfig) error {
f, err := tempFileWithSuffix(os.TempDir(), "_tgutils.ogg")
if err != nil {
return err
}
defer f.Close()
ffmpegArgs := []string{
"-i",
audio.FilePath,
"-f",
"wav",
"-",
}
opusArgs := []string{
"--bitrate",
"256",
"-",
f.Name(),
}
c1 := exec.Command("ffmpeg", ffmpegArgs...)
c2 := exec.Command("opusenc", opusArgs...)
c2.Stdin, _ = c1.StdoutPipe()
c2.Stdout = os.Stdout
c2.Start()
c1.Run()
c2.Wait()
return nil
}

View File

@ -71,4 +71,4 @@ func TestFileLink(t *testing.T) {
if file.Link("token") != "https://api.telegram.org/file/bottoken/test/test.txt" {
t.Fail()
}
}
}