telegram-bot-api/bot_test.go

733 lines
14 KiB
Go
Raw Normal View History

2018-10-09 06:34:34 +02:00
package tgbotapi
import (
2015-11-21 12:44:26 +01:00
"io/ioutil"
2015-11-21 12:22:08 +01:00
"net/http"
"os"
"testing"
"time"
)
2015-11-21 13:34:30 +01:00
const (
2017-05-22 00:04:12 +02:00
TestToken = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I"
ChatID = 76918703
SupergroupChatID = -1001120141283
2017-05-22 00:04:12 +02:00
ReplyToMessageID = 35
ExistingPhotoFileID = "AgADAgADw6cxG4zHKAkr42N7RwEN3IFShCoABHQwXEtVks4EH2wBAAEC"
ExistingDocumentFileID = "BQADAgADOQADjMcoCcioX1GrDvp3Ag"
ExistingAudioFileID = "BQADAgADRgADjMcoCdXg3lSIN49lAg"
ExistingVoiceFileID = "AwADAgADWQADjMcoCeul6r_q52IyAg"
ExistingVideoFileID = "BAADAgADZgADjMcoCav432kYe0FRAg"
ExistingVideoNoteFileID = "DQADAgADdQAD70cQSUK41dLsRMqfAg"
ExistingStickerFileID = "BQADAgADcwADjMcoCbdl-6eB--YPAg"
2015-11-21 13:34:30 +01:00
)
2018-10-09 06:34:34 +02:00
func getBot(t *testing.T) (*BotAPI, error) {
bot, err := NewBotAPI(TestToken)
2017-05-22 00:04:12 +02:00
bot.Debug = true
2015-11-21 13:34:30 +01:00
if err != nil {
t.Error(err)
}
2015-11-21 13:34:30 +01:00
return bot, err
}
func TestNewBotAPI_notoken(t *testing.T) {
2018-10-09 06:34:34 +02:00
_, err := NewBotAPI("")
2015-11-20 11:42:26 +01:00
if err == nil {
t.Error(err)
}
}
func TestGetUpdates(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
u := NewUpdate(0)
2015-11-21 13:34:30 +01:00
_, err := bot.GetUpdates(u)
if err != nil {
t.Error(err)
}
}
2015-11-20 16:30:50 +01:00
func TestSendWithMessage(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
2015-11-20 18:26:12 +01:00
msg.ParseMode = "markdown"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.Error(err)
2015-11-20 18:26:12 +01:00
}
}
func TestSendWithMessageReply(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2018-10-09 06:34:34 +02:00
msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
2015-11-21 13:34:30 +01:00
msg.ReplyToMessageID = ReplyToMessageID
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.Error(err)
2015-11-20 18:26:12 +01:00
}
}
func TestSendWithMessageForward(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2018-10-09 06:34:34 +02:00
msg := NewForward(ChatID, ChatID, ReplyToMessageID)
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 16:30:50 +01:00
if err != nil {
t.Error(err)
2015-11-20 16:30:50 +01:00
}
}
2015-11-20 17:43:56 +01:00
func TestSendWithNewPhoto(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 16:30:50 +01:00
2018-10-09 06:34:34 +02:00
msg := NewPhotoUpload(ChatID, "tests/image.jpg")
2015-11-20 18:26:12 +01:00
msg.Caption = "Test"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 16:30:50 +01:00
if err != nil {
t.Error(err)
2015-11-20 16:30:50 +01:00
}
}
2015-11-21 12:36:43 +01:00
func TestSendWithNewPhotoWithFileBytes(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 12:36:43 +01:00
data, _ := ioutil.ReadFile("tests/image.jpg")
2018-10-09 06:34:34 +02:00
b := FileBytes{Name: "image.jpg", Bytes: data}
2015-11-21 12:36:43 +01:00
2018-10-09 06:34:34 +02:00
msg := NewPhotoUpload(ChatID, b)
2015-11-21 12:36:43 +01:00
msg.Caption = "Test"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-21 12:36:43 +01:00
if err != nil {
t.Error(err)
2015-11-21 12:36:43 +01:00
}
}
func TestSendWithNewPhotoWithFileReader(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 12:36:43 +01:00
f, _ := os.Open("tests/image.jpg")
2018-10-09 06:34:34 +02:00
reader := FileReader{Name: "image.jpg", Reader: f, Size: -1}
2015-11-21 12:36:43 +01:00
2018-10-09 06:34:34 +02:00
msg := NewPhotoUpload(ChatID, reader)
2015-11-21 12:36:43 +01:00
msg.Caption = "Test"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-21 12:36:43 +01:00
if err != nil {
t.Error(err)
2015-11-21 12:36:43 +01:00
}
}
2015-11-20 18:26:12 +01:00
func TestSendWithNewPhotoReply(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2018-10-09 06:34:34 +02:00
msg := NewPhotoUpload(ChatID, "tests/image.jpg")
2015-11-21 13:34:30 +01:00
msg.ReplyToMessageID = ReplyToMessageID
2015-11-20 17:43:56 +01:00
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.Error(err)
2015-11-20 18:26:12 +01:00
}
}
2015-11-20 17:43:56 +01:00
func TestSendWithExistingPhoto(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
2018-10-09 06:34:34 +02:00
msg := NewPhotoShare(ChatID, ExistingPhotoFileID)
2015-11-20 18:26:12 +01:00
msg.Caption = "Test"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 17:43:56 +01:00
if err != nil {
t.Error(err)
2015-11-20 17:43:56 +01:00
}
}
func TestSendWithNewDocument(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
2018-10-09 06:34:34 +02:00
msg := NewDocumentUpload(ChatID, "tests/image.jpg")
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 17:43:56 +01:00
if err != nil {
t.Error(err)
2015-11-20 17:43:56 +01:00
}
}
func TestSendWithExistingDocument(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
2018-10-09 06:34:34 +02:00
msg := NewDocumentShare(ChatID, ExistingDocumentFileID)
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 17:43:56 +01:00
if err != nil {
t.Error(err)
2015-11-20 17:43:56 +01:00
}
}
2015-11-20 18:26:12 +01:00
func TestSendWithNewAudio(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2018-10-09 06:34:34 +02:00
msg := NewAudioUpload(ChatID, "tests/audio.mp3")
2015-11-20 18:26:12 +01:00
msg.Title = "TEST"
msg.Duration = 10
msg.Performer = "TEST"
msg.MimeType = "audio/mpeg"
msg.FileSize = 688
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.Error(err)
2015-11-20 18:26:12 +01:00
}
}
func TestSendWithExistingAudio(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2018-10-09 06:34:34 +02:00
msg := NewAudioShare(ChatID, ExistingAudioFileID)
2015-11-20 18:26:12 +01:00
msg.Title = "TEST"
msg.Duration = 10
msg.Performer = "TEST"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.Error(err)
2015-11-20 18:26:12 +01:00
}
}
func TestSendWithNewVoice(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2018-10-09 06:34:34 +02:00
msg := NewVoiceUpload(ChatID, "tests/voice.ogg")
2015-11-20 18:26:12 +01:00
msg.Duration = 10
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.Error(err)
2015-11-20 18:26:12 +01:00
}
}
func TestSendWithExistingVoice(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2018-10-09 06:34:34 +02:00
msg := NewVoiceShare(ChatID, ExistingVoiceFileID)
2015-11-20 18:26:12 +01:00
msg.Duration = 10
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.Error(err)
2015-11-20 18:26:12 +01:00
}
}
2016-04-14 00:06:18 +02:00
func TestSendWithContact(t *testing.T) {
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
contact := NewContact(ChatID, "5551234567", "Test")
2016-04-14 00:06:18 +02:00
if _, err := bot.Send(contact); err != nil {
t.Error(err)
2016-04-14 00:06:18 +02:00
}
}
2015-11-20 18:26:12 +01:00
func TestSendWithLocation(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2018-10-09 06:34:34 +02:00
_, err := bot.Send(NewLocation(ChatID, 40, 40))
2015-11-20 18:26:12 +01:00
if err != nil {
t.Error(err)
2015-11-20 18:26:12 +01:00
}
}
2016-04-14 00:06:18 +02:00
func TestSendWithVenue(t *testing.T) {
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
venue := NewVenue(ChatID, "A Test Location", "123 Test Street", 40, 40)
2016-04-14 00:06:18 +02:00
if _, err := bot.Send(venue); err != nil {
t.Error(err)
2016-04-14 00:06:18 +02:00
}
}
2015-11-21 11:17:34 +01:00
func TestSendWithNewVideo(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
2018-10-09 06:34:34 +02:00
msg := NewVideoUpload(ChatID, "tests/video.mp4")
2015-11-21 11:17:34 +01:00
msg.Duration = 10
msg.Caption = "TEST"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-21 11:17:34 +01:00
if err != nil {
t.Error(err)
2015-11-21 11:17:34 +01:00
}
}
func TestSendWithExistingVideo(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
2018-10-09 06:34:34 +02:00
msg := NewVideoShare(ChatID, ExistingVideoFileID)
2015-11-21 11:17:34 +01:00
msg.Duration = 10
msg.Caption = "TEST"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-21 11:17:34 +01:00
if err != nil {
t.Error(err)
2015-11-21 11:17:34 +01:00
}
}
2017-05-22 00:04:12 +02:00
func TestSendWithNewVideoNote(t *testing.T) {
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
msg := NewVideoNoteUpload(ChatID, 240, "tests/videonote.mp4")
2017-05-22 00:04:12 +02:00
msg.Duration = 10
_, err := bot.Send(msg)
if err != nil {
t.Error(err)
}
}
func TestSendWithExistingVideoNote(t *testing.T) {
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
msg := NewVideoNoteShare(ChatID, 240, ExistingVideoNoteFileID)
2017-05-22 00:04:12 +02:00
msg.Duration = 10
_, err := bot.Send(msg)
if err != nil {
t.Error(err)
}
}
2015-11-21 11:17:34 +01:00
func TestSendWithNewSticker(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
2018-10-09 06:34:34 +02:00
msg := NewStickerUpload(ChatID, "tests/image.jpg")
2015-11-21 11:17:34 +01:00
2015-11-21 13:36:55 +01:00
_, err := bot.Send(msg)
2015-11-21 11:17:34 +01:00
if err != nil {
t.Error(err)
2015-11-21 11:17:34 +01:00
}
}
func TestSendWithExistingSticker(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
2018-10-09 06:34:34 +02:00
msg := NewStickerShare(ChatID, ExistingStickerFileID)
2015-11-21 11:17:34 +01:00
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-21 11:17:34 +01:00
if err != nil {
t.Error(err)
2015-11-21 11:17:34 +01:00
}
}
func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
2018-10-09 06:34:34 +02:00
msg := NewStickerUpload(ChatID, "tests/image.jpg")
msg.ReplyMarkup = ReplyKeyboardRemove{
2017-12-29 06:17:32 +01:00
RemoveKeyboard: true,
Selective: false,
}
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-21 11:17:34 +01:00
if err != nil {
t.Error(err)
2015-11-21 11:17:34 +01:00
}
}
func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
2018-10-09 06:34:34 +02:00
msg := NewStickerShare(ChatID, ExistingStickerFileID)
msg.ReplyMarkup = ReplyKeyboardRemove{
2017-12-29 06:17:32 +01:00
RemoveKeyboard: true,
Selective: false,
}
2015-11-21 11:17:34 +01:00
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-21 11:17:34 +01:00
if err != nil {
t.Error(err)
2015-11-21 11:17:34 +01:00
}
}
2015-11-20 17:43:56 +01:00
func TestGetFile(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
2018-10-09 06:34:34 +02:00
file := FileConfig{
2017-12-29 06:17:32 +01:00
FileID: ExistingPhotoFileID,
}
2015-11-20 17:43:56 +01:00
2015-11-21 13:34:30 +01:00
_, err := bot.GetFile(file)
2015-11-20 17:43:56 +01:00
if err != nil {
t.Error(err)
2015-11-20 17:43:56 +01:00
}
}
func TestSendChatConfig(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
2018-10-09 06:34:34 +02:00
_, err := bot.Request(NewChatAction(ChatID, ChatTyping))
2015-11-20 17:43:56 +01:00
if err != nil {
t.Error(err)
2015-11-20 17:43:56 +01:00
}
}
2016-04-12 19:53:19 +02:00
func TestSendEditMessage(t *testing.T) {
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
msg, err := bot.Send(NewMessage(ChatID, "Testing editing."))
2016-04-12 19:53:19 +02:00
if err != nil {
t.Error(err)
2016-04-12 19:53:19 +02:00
}
2018-10-09 06:34:34 +02:00
edit := EditMessageTextConfig{
BaseEdit: BaseEdit{
2016-04-12 19:53:19 +02:00
ChatID: ChatID,
MessageID: msg.MessageID,
},
Text: "Updated text.",
}
_, err = bot.Send(edit)
if err != nil {
t.Error(err)
2016-04-12 19:53:19 +02:00
}
}
2015-11-20 17:43:56 +01:00
func TestGetUserProfilePhotos(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
2018-10-09 06:34:34 +02:00
_, err := bot.GetUserProfilePhotos(NewUserProfilePhotos(ChatID))
2015-11-20 17:43:56 +01:00
if err != nil {
t.Error(err)
2015-11-20 17:43:56 +01:00
}
}
2015-11-21 12:36:43 +01:00
func TestSetWebhookWithCert(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 12:22:08 +01:00
time.Sleep(time.Second * 2)
2018-10-09 06:34:34 +02:00
bot.Request(RemoveWebhookConfig{})
2015-11-21 12:22:08 +01:00
2018-10-09 06:34:34 +02:00
wh := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
_, err := bot.Request(wh)
2015-11-21 12:22:08 +01:00
if err != nil {
t.Error(err)
2015-11-21 12:22:08 +01:00
}
2018-10-08 09:25:09 +02:00
_, err = bot.GetWebhookInfo()
2018-03-26 19:22:16 +02:00
2018-01-07 19:10:50 +01:00
if err != nil {
t.Error(err)
}
2018-03-26 19:22:16 +02:00
2018-10-09 06:34:34 +02:00
bot.Request(RemoveWebhookConfig{})
2015-11-21 12:22:08 +01:00
}
2015-11-21 12:36:43 +01:00
func TestSetWebhookWithoutCert(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 12:36:43 +01:00
time.Sleep(time.Second * 2)
2018-10-09 06:34:34 +02:00
bot.Request(RemoveWebhookConfig{})
2015-11-21 12:36:43 +01:00
2018-10-09 06:34:34 +02:00
wh := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
_, err := bot.Request(wh)
2015-11-21 12:36:43 +01:00
if err != nil {
t.Error(err)
2015-11-21 12:36:43 +01:00
}
2018-01-07 19:10:50 +01:00
info, err := bot.GetWebhookInfo()
2018-03-26 19:22:16 +02:00
2018-01-07 19:10:50 +01:00
if err != nil {
t.Error(err)
}
2018-03-26 19:22:16 +02:00
2018-01-07 19:10:50 +01:00
if info.LastErrorDate != 0 {
2018-03-26 19:22:16 +02:00
t.Errorf("failed to set webhook: %s", info.LastErrorMessage)
2018-01-07 19:10:50 +01:00
}
2018-03-26 19:22:16 +02:00
2018-10-09 06:34:34 +02:00
bot.Request(RemoveWebhookConfig{})
2015-11-21 12:36:43 +01:00
}
func TestSendWithMediaGroup(t *testing.T) {
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
cfg := NewMediaGroup(ChatID, []interface{}{
NewInputMediaPhoto("https://i.imgur.com/unQLJIb.jpg"),
NewInputMediaPhoto("https://i.imgur.com/J5qweNZ.jpg"),
NewInputMediaVideo("https://i.imgur.com/F6RmI24.mp4"),
})
2018-12-25 22:44:01 +01:00
messages, err := bot.SendMediaGroup(cfg)
if err != nil {
t.Error(err)
}
2018-12-25 22:44:01 +01:00
if messages == nil {
t.Error()
}
if len(messages) != 3 {
t.Error()
}
}
func ExampleNewBotAPI() {
2018-10-09 06:34:34 +02:00
bot, err := NewBotAPI("MyAwesomeBotToken")
if err != nil {
2018-10-09 06:34:34 +02:00
panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
2018-10-09 06:34:34 +02:00
u := NewUpdate(0)
u.Timeout = 60
2018-10-09 08:45:18 +02:00
updates := bot.GetUpdatesChan(u)
// Optional: wait for updates and clear them if you don't want to handle
// a large backlog of old messages
time.Sleep(time.Millisecond * 500)
updates.Clear()
2015-11-21 15:26:28 +01:00
for update := range updates {
2016-05-22 17:16:28 +02:00
if update.Message == nil {
continue
}
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
2018-10-09 06:34:34 +02:00
msg := NewMessage(update.Message.Chat.ID, update.Message.Text)
msg.ReplyToMessageID = update.Message.MessageID
2015-11-20 16:30:50 +01:00
bot.Send(msg)
}
}
2015-11-21 12:53:00 +01:00
func ExampleNewWebhook() {
2018-10-09 06:34:34 +02:00
bot, err := NewBotAPI("MyAwesomeBotToken")
2015-11-21 12:53:00 +01:00
if err != nil {
2018-10-09 06:34:34 +02:00
panic(err)
2015-11-21 12:53:00 +01:00
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
2018-10-09 06:34:34 +02:00
_, err = bot.Request(NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
2015-11-21 12:53:00 +01:00
if err != nil {
2018-10-09 06:34:34 +02:00
panic(err)
2015-11-21 12:53:00 +01:00
}
2018-01-07 19:10:50 +01:00
info, err := bot.GetWebhookInfo()
2018-03-26 19:22:16 +02:00
2018-01-07 19:10:50 +01:00
if err != nil {
2018-10-09 06:34:34 +02:00
panic(err)
2018-01-07 19:10:50 +01:00
}
2018-03-26 19:22:16 +02:00
2018-01-07 19:10:50 +01:00
if info.LastErrorDate != 0 {
2018-03-26 19:22:16 +02:00
log.Printf("failed to set webhook: %s", info.LastErrorMessage)
2018-01-07 19:10:50 +01:00
}
2018-03-26 19:22:16 +02:00
updates := bot.ListenForWebhook("/" + bot.Token)
2015-11-21 12:53:00 +01:00
go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
2015-11-21 15:26:28 +01:00
for update := range updates {
2015-11-21 12:53:00 +01:00
log.Printf("%+v\n", update)
}
}
2018-03-26 19:22:16 +02:00
func ExampleInlineConfig() {
2018-10-09 06:34:34 +02:00
bot, err := NewBotAPI("MyAwesomeBotToken") // create new bot
if err != nil {
2018-10-09 06:34:34 +02:00
panic(err)
}
log.Printf("Authorized on account %s", bot.Self.UserName)
2018-10-09 06:34:34 +02:00
u := NewUpdate(0)
u.Timeout = 60
2018-10-09 08:45:18 +02:00
updates := bot.GetUpdatesChan(u)
for update := range updates {
2016-05-22 17:16:28 +02:00
if update.InlineQuery == nil { // if no inline query, ignore it
continue
}
2018-10-09 06:34:34 +02:00
article := NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
article.Description = update.InlineQuery.Query
2018-10-09 06:34:34 +02:00
inlineConf := InlineConfig{
InlineQueryID: update.InlineQuery.ID,
IsPersonal: true,
CacheTime: 0,
Results: []interface{}{article},
}
if _, err := bot.Request(inlineConf); err != nil {
log.Println(err)
}
}
}
func TestDeleteMessage(t *testing.T) {
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
msg.ParseMode = "markdown"
message, _ := bot.Send(msg)
2018-10-09 06:34:34 +02:00
deleteMessageConfig := DeleteMessageConfig{
ChatID: message.Chat.ID,
MessageID: message.MessageID,
}
2017-12-29 07:50:45 +01:00
_, err := bot.Request(deleteMessageConfig)
if err != nil {
t.Error(err)
}
}
func TestPinChatMessage(t *testing.T) {
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
msg.ParseMode = "markdown"
message, _ := bot.Send(msg)
2018-10-09 06:34:34 +02:00
pinChatMessageConfig := PinChatMessageConfig{
2017-11-09 19:51:50 +01:00
ChatID: message.Chat.ID,
MessageID: message.MessageID,
DisableNotification: false,
}
2017-12-29 07:50:45 +01:00
_, err := bot.Request(pinChatMessageConfig)
if err != nil {
t.Error(err)
}
}
func TestUnpinChatMessage(t *testing.T) {
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
msg.ParseMode = "markdown"
message, _ := bot.Send(msg)
// We need pin message to unpin something
2018-10-09 06:34:34 +02:00
pinChatMessageConfig := PinChatMessageConfig{
2017-11-09 19:51:50 +01:00
ChatID: message.Chat.ID,
MessageID: message.MessageID,
DisableNotification: false,
}
if _, err := bot.Request(pinChatMessageConfig); err != nil {
t.Error(err)
}
2018-10-09 06:34:34 +02:00
unpinChatMessageConfig := UnpinChatMessageConfig{
2017-11-09 19:51:50 +01:00
ChatID: message.Chat.ID,
}
if _, err := bot.Request(unpinChatMessageConfig); err != nil {
t.Error(err)
}
2017-11-09 19:51:50 +01:00
}
func TestPolls(t *testing.T) {
bot, _ := getBot(t)
poll := NewPoll(SupergroupChatID, "Are polls working?", "Yes", "No")
msg, err := bot.Send(poll)
if err != nil {
t.Error(err)
}
result, err := bot.StopPoll(NewStopPoll(SupergroupChatID, msg.MessageID))
if err != nil {
t.Error(err)
}
if result.Question != "Are polls working?" {
t.Error("Poll question did not match")
}
if !result.IsClosed {
t.Error("Poll did not end")
}
if result.Options[0].Text != "Yes" || result.Options[0].VoterCount != 0 || result.Options[1].Text != "No" || result.Options[1].VoterCount != 0 {
t.Error("Poll options were incorrect")
}
}
2020-03-30 22:35:53 +02:00
func TestSendDice(t *testing.T) {
bot, _ := getBot(t)
dice := NewSendDice(ChatID)
msg, err := bot.Send(dice)
if err != nil {
t.Error("Unable to send dice roll")
}
if msg.Dice == nil {
t.Error("Dice roll was not received")
}
}
func TestSetCommands(t *testing.T) {
bot, _ := getBot(t)
setCommands := NewSetMyCommands(BotCommand{
Command: "test",
Description: "a test command",
})
if _, err := bot.Request(setCommands); err != nil {
t.Error("Unable to set commands")
}
commands, err := bot.GetMyCommands()
if err != nil {
t.Error("Unable to get commands")
}
if len(commands) != 1 {
t.Error("Incorrect number of commands returned")
}
if commands[0].Command != "test" || commands[0].Description != "a test command" {
t.Error("Commands were incorrectly set")
}
}