Skip to content

Commit

Permalink
Merge pull request #44 from iamwavecut/master
Browse files Browse the repository at this point in the history
Tidy up versioning situation, cleanup tests
  • Loading branch information
iamwavecut authored Nov 7, 2024
2 parents 1376e01 + 01dd2c6 commit 0961022
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 181 deletions.
179 changes: 1 addition & 178 deletions bot_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package tgbotapi

import (
"net/http"
"os"
"testing"
"time"
)

const (
TestToken = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I"
ChatID = 76918703
Channel = "@tgbotapitest"
SupergroupChatID = -1001120141283
Expand All @@ -35,7 +33,7 @@ func (t testLogger) Printf(format string, v ...interface{}) {
}

func getBot(t *testing.T) (*BotAPI, error) {
bot, err := NewBotAPI(TestToken)
bot, err := NewBotAPI(os.Getenv("TEST_TOKEN"))
bot.Debug = true

logger := testLogger{t}
Expand Down Expand Up @@ -671,154 +669,6 @@ func TestSendWithMediaGroupAudio(t *testing.T) {
}
}

func ExampleNewBotAPI() {
bot, err := NewBotAPI("MyAwesomeBotToken")
if err != nil {
panic(err)
}

bot.Debug = true

log.Printf("Authorized on account %s", bot.Self.UserName)

u := NewUpdate(0)
u.Timeout = 60

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()

for update := range updates {
if update.Message == nil {
continue
}

log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)

msg := NewMessage(update.Message.Chat.ID, update.Message.Text)
msg.ReplyParameters.MessageID = update.Message.MessageID

bot.Send(msg)
}
}

func ExampleNewWebhook() {
bot, err := NewBotAPI("MyAwesomeBotToken")
if err != nil {
panic(err)
}

bot.Debug = true

log.Printf("Authorized on account %s", bot.Self.UserName)

wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, FilePath("cert.pem"))

if err != nil {
panic(err)
}

_, err = bot.Request(wh)

if err != nil {
panic(err)
}

info, err := bot.GetWebhookInfo()

if err != nil {
panic(err)
}

if info.LastErrorDate != 0 {
log.Printf("failed to set webhook: %s", info.LastErrorMessage)
}

updates := bot.ListenForWebhook("/" + bot.Token)
go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)

for update := range updates {
log.Printf("%+v\n", update)
}
}

func ExampleWebhookHandler() {
bot, err := NewBotAPI("MyAwesomeBotToken")
if err != nil {
panic(err)
}

bot.Debug = true

log.Printf("Authorized on account %s", bot.Self.UserName)

wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, FilePath("cert.pem"))

if err != nil {
panic(err)
}

_, err = bot.Request(wh)
if err != nil {
panic(err)
}
info, err := bot.GetWebhookInfo()
if err != nil {
panic(err)
}
if info.LastErrorDate != 0 {
log.Printf("[Telegram callback failed]%s", info.LastErrorMessage)
}

http.HandleFunc("/"+bot.Token, func(w http.ResponseWriter, r *http.Request) {
update, err := bot.HandleUpdate(r)
if err != nil {
log.Printf("%+v\n", err.Error())
} else {
log.Printf("%+v\n", *update)
}
})

go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
}

func ExampleInlineConfig() {
bot, err := NewBotAPI("MyAwesomeBotToken") // create new bot
if err != nil {
panic(err)
}

log.Printf("Authorized on account %s", bot.Self.UserName)

u := NewUpdate(0)
u.Timeout = 60

updates := bot.GetUpdatesChan(u)

for update := range updates {
if update.InlineQuery == nil { // if no inline query, ignore it
continue
}

article := NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
article.Description = update.InlineQuery.Query

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)

Expand Down Expand Up @@ -1021,33 +871,6 @@ func TestCommands(t *testing.T) {
}
}

// TODO: figure out why test is failing
//
// func TestEditMessageMedia(t *testing.T) {
// bot, _ := getBot(t)

// msg := NewPhoto(ChatID, "tests/image.jpg")
// msg.Caption = "Test"
// m, err := bot.Send(msg)

// if err != nil {
// t.Error(err)
// }

// edit := EditMessageMediaConfig{
// BaseEdit: BaseEdit{
// ChatID: ChatID,
// MessageID: m.MessageID,
// },
// Media: NewInputMediaVideo(FilePath("tests/video.mp4")),
// }

// _, err = bot.Request(edit)
// if err != nil {
// t.Error(err)
// }
// }

func TestPrepareInputMediaForParams(t *testing.T) {
media := []interface{}{
NewInputMediaPhoto(FilePath("tests/image.jpg")),
Expand Down
73 changes: 73 additions & 0 deletions examples/polling_echo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"log"
"time"

api "github.com/OvyFlash/telegram-bot-api"
)

// func main() { polling_echo() }

func polling_echo() {
bot, err := api.NewBotAPI("MyAwesomeBotToken")
if err != nil {
panic(err)
}

bot.Debug = true

log.Printf("Authorized on account %s", bot.Self.UserName)

updateConfig := api.NewUpdate(0)
updateConfig.Timeout = 60

// Empty list allows all updates excluding
// UpdateTypeChatMember, UpdateTypeMessageReaction and UpdateTypeMessageReactionCount
updateConfig.AllowedUpdates = []string{
api.UpdateTypeMessage,
api.UpdateTypeEditedMessage,
api.UpdateTypeChannelPost,
api.UpdateTypeEditedChannelPost,
api.UpdateTypeBusinessConnection,
api.UpdateTypeBusinessMessage,
api.UpdateTypeEditedBusinessMessage,
api.UpdateTypeDeletedBusinessMessages,
api.UpdateTypeMessageReaction,
api.UpdateTypeMessageReactionCount,
api.UpdateTypeInlineQuery,
api.UpdateTypeChosenInlineResult,
api.UpdateTypeCallbackQuery,
api.UpdateTypeShippingQuery,
api.UpdateTypePreCheckoutQuery,
api.UpdateTypePurchasedPaidMedia,
api.UpdateTypePoll,
api.UpdateTypePollAnswer,
api.UpdateTypeMyChatMember,
api.UpdateTypeChatMember,
api.UpdateTypeChatJoinRequest,
api.UpdateTypeChatBoost,
api.UpdateTypeRemovedChatBoost,
}

updatesChannel := bot.GetUpdatesChan(updateConfig)

// 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)
updatesChannel.Clear()

for update := range updatesChannel {
if update.Message == nil {
continue
}

log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)

// Send an echo reply to the message
msg := api.NewMessage(update.Message.Chat.ID, update.Message.Text)
msg.ReplyParameters.MessageID = update.Message.MessageID

bot.Send(msg)
}
}
43 changes: 43 additions & 0 deletions examples/polling_inline_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"log"

api "github.com/OvyFlash/telegram-bot-api"
)

// func main() { polling_inline_query() }

func polling_inline_query() {
bot, err := api.NewBotAPI("MyAwesomeBotToken") // create new bot
if err != nil {
panic(err)
}

log.Printf("Authorized on account %s", bot.Self.UserName)

updateConfig := api.NewUpdate(0)
updateConfig.Timeout = 60

updatesChannel := bot.GetUpdatesChan(updateConfig)

for update := range updatesChannel {
if update.InlineQuery == nil { // if no inline query, skip update
continue
}

article := api.NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
article.Description = update.InlineQuery.Query

inlineConfig := api.InlineConfig{
InlineQueryID: update.InlineQuery.ID,
IsPersonal: true,
CacheTime: 0,
Results: []interface{}{article},
}

if _, err := bot.Request(inlineConfig); err != nil {
log.Println(err)
}
}
}
Loading

0 comments on commit 0961022

Please sign in to comment.