Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ ADDR=:8080

# Telegram client config
BOT_TOKEN=<telegram-bot-token>
USERNAME_LIMITS=
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ If you add a caption to an image it will be used as the question.
Env variables:
- `BOT_TOKEN` - The token of the bot you created in Telegram
- `AI_SERVICE` - AI service (the server-bot's) address (Default: `http://127.0.0.1:8080`)
- `USERNAME_LIMITS` - A comma separated list of usernames that are allowed to use the bot (Default: `""`, means everybody can use it)


#### Facebook messenger client
Expand Down
38 changes: 34 additions & 4 deletions cmd/client-telegram/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"os/signal"
"strings"

"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
Expand All @@ -29,7 +30,14 @@ func main() {
aiSrv = "http://127.0.0.1:8080"
}

l := New(aiSrv)
usernameLimits := make([]string, 0)
if usernameLimitsEnv := os.Getenv("USERNAME_LIMITS"); usernameLimitsEnv != "" {
for _, u := range strings.Split(usernameLimitsEnv, ",") {
usernameLimits = append(usernameLimits, strings.TrimSpace(u))
}
}

l := New(aiSrv, usernameLimits)
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

Expand All @@ -49,17 +57,39 @@ func main() {
}

type Logic struct {
httpB *httpBotter.Logic
httpB *httpBotter.Logic
userLimits []string
}

func New(baseURL string) *Logic {
func New(baseURL string, userLimit []string) *Logic {
return &Logic{
httpB: httpBotter.New(baseURL),
httpB: httpBotter.New(baseURL),
userLimits: userLimit,
}
}

// Handler .
func (l *Logic) Handler(ctx context.Context, b *bot.Bot, update *models.Update) {
// If we have any limits set, check them
if len(l.userLimits) > 0 {
found := false
for _, u := range l.userLimits {
if update.Message.From.Username == u {
found = true
break
}
}

if !found {
_, _ = b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: "🙅You are not allowed to use this bot.",
})

return
}
}

var payload []byte
msg := update.Message.Text

Expand Down