Skip to content
This repository has been archived by the owner on Dec 27, 2021. It is now read-only.

Commit

Permalink
add long poller method
Browse files Browse the repository at this point in the history
  • Loading branch information
IEP committed Apr 15, 2021
1 parent 5cc1815 commit 213cc4c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# vendor/
data.json
config.yaml
telegram-random-image-carousel
telegram-random-image-carousel*
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

## Usage

Create `config.yaml` and `data.json` (see example files) and run with `./telegram-random-image-carousel`
Create `config.yaml` and `data.json` (see example files) and finally run the bot by using `./telegram-random-image-carousel`.

Get the random image by triggering it using `/bucketName` command.
16 changes: 14 additions & 2 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,27 @@ import (
tb "gopkg.in/tucnak/telebot.v2"
)

// RunBot based on the configuration and repository passed
func RunBot(config *Config, repository *Repository) {
settings := tb.Settings{
Token: config.BotAPIToken,
Poller: &tb.Webhook{
}

// if both port and webhook URL are configured, use webhook
if config.Port != "" && config.WebhookURL != "" {
settings.Poller = &tb.Webhook{
Listen: ":" + config.Port,
Endpoint: &tb.WebhookEndpoint{
PublicURL: config.WebhookURL,
},
},
}
// fallback to long poller
} else {
settings.Poller = &tb.LongPoller{
Timeout: 10 * time.Second,
}
}

log.Println("Loading the bot")
b, err := tb.NewBot(settings)
if err != nil {
Expand All @@ -34,6 +45,7 @@ func RunBot(config *Config, repository *Repository) {
b.Start()
}

// SetRepository binds repository data to the bot commands
func SetRepository(b *tb.Bot, repository *Repository) {
rand.Seed(time.Now().UnixNano())

Expand Down
4 changes: 2 additions & 2 deletions config.example.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
bot_api_token: 1234567890
webhook_url: https://some_website.com
port: 3000
webhook_url: https://some_website.com # Comment this line if you want to use long poller
port: 3000 # Comment this line if you want to use long poller
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func TestConfig(t *testing.T) {
reference := &Config{
BotAPIToken: "1234567890",
WebhookURL: "https://some_website.com/api/telegram",
WebhookURL: "https://some_website.com",
Port: "3000",
}

Expand Down
3 changes: 3 additions & 0 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func LoadData(filename string) (*Repository, error) {

repo := Repository{Bucket: b, BucketSize: make(map[string]int)}
for k, v := range repo.Bucket {
if len(v) == 0 {
panic("bucket size must be larger than zero")
}
repo.BucketSize[k] = len(v)
}

Expand Down

0 comments on commit 213cc4c

Please sign in to comment.