-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: add Telegram bot integration #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
freddycodes23
wants to merge
1
commit into
vxcontrol:main
Choose a base branch
from
freddycodes23:feat/telegram-bot
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+208
β0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package telegram | ||
|
|
||
| type Auth struct { | ||
| allowed map[int64]bool | ||
| } | ||
|
|
||
| func NewAuth(ids []int64) *Auth { | ||
| m := make(map[int64]bool, len(ids)) | ||
| for _, id := range ids { | ||
| m[id] = true | ||
| } | ||
| return &Auth{allowed: m} | ||
| } | ||
|
|
||
| func (a *Auth) IsAllowed(userID int64) bool { | ||
| return a.allowed[userID] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package telegram | ||
|
|
||
| import ( | ||
| "context" | ||
| "log" | ||
|
|
||
|
freddycodes23 marked this conversation as resolved.
|
||
| tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" | ||
| ) | ||
|
freddycodes23 marked this conversation as resolved.
|
||
|
|
||
| type Bot struct { | ||
| api *tgbotapi.BotAPI | ||
| handler *Handler | ||
| auth *Auth | ||
| stopCh chan struct{} | ||
| } | ||
|
|
||
| func New(token string, allowedIDs []int64, svc FlowService) (*Bot, error) { | ||
| api, err := tgbotapi.NewBotAPI(token) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| api.Debug = false | ||
| log.Printf("Telegram bot authorized as @%s", api.Self.UserName) | ||
| return &Bot{ | ||
| api: api, | ||
| handler: NewHandler(api, svc), | ||
| auth: NewAuth(allowedIDs), | ||
| stopCh: make(chan struct{}), | ||
| }, nil | ||
| } | ||
|
|
||
| func (b *Bot) Start(ctx context.Context) { | ||
| u := tgbotapi.NewUpdate(0) | ||
| u.Timeout = 60 | ||
| updates := b.api.GetUpdatesChan(u) | ||
| log.Println("Telegram bot polling for updates...") | ||
| for { | ||
| select { | ||
| case update := <-updates: | ||
| if update.Message == nil { | ||
| continue | ||
| } | ||
| if !b.auth.IsAllowed(update.Message.From.ID) { | ||
| log.Printf("Telegram: blocked user %d", update.Message.From.ID) | ||
| continue | ||
| } | ||
| go b.handler.Handle(update.Message) | ||
| case <-ctx.Done(): | ||
|
freddycodes23 marked this conversation as resolved.
|
||
| return | ||
| case <-b.stopCh: | ||
| return | ||
| } | ||
|
freddycodes23 marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| func (b *Bot) Stop() { | ||
| close(b.stopCh) | ||
| b.api.StopReceivingUpdates() | ||
| } | ||
|
freddycodes23 marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package telegram | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| func FormatWelcome() string { | ||
| return `*Welcome to PentAGI* | ||
|
|
||
| I am your AI-powered security assistant. | ||
|
|
||
| Send /help to see available commands.` | ||
| } | ||
|
|
||
| func FormatHelp() string { | ||
| return `*Available commands* | ||
|
|
||
| /flows β list your recent flows | ||
| /new <task> β create a new flow | ||
| /status <id> β check flow status | ||
| /stop <id> β stop a running flow | ||
| /help β show this message` | ||
| } | ||
|
|
||
| func FormatFlowList(flows []Flow) string { | ||
| if len(flows) == 0 { | ||
| return "No flows found. Use /new <task> to create one." | ||
| } | ||
| var sb strings.Builder | ||
| sb.WriteString("*Your flows:*\n\n") | ||
| for _, f := range flows { | ||
| sb.WriteString(fmt.Sprintf("β’ `%s` β %s (%s)\n", f.ID[:8], f.Title, f.Status)) | ||
| } | ||
| return sb.String() | ||
| } | ||
|
|
||
| func FormatFlowCreated(f *Flow) string { | ||
| return fmt.Sprintf("*Flow created*\n\nID: `%s`\nTask: %s\nStatus: %s", f.ID, f.Title, f.Status) | ||
| } | ||
|
|
||
| func FormatFlowStatus(f *Flow) string { | ||
| return fmt.Sprintf("*Flow status*\n\nID: `%s`\nTask: %s\nStatus: %s", f.ID, f.Title, f.Status) | ||
| } | ||
|
freddycodes23 marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package telegram | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" | ||
| ) | ||
|
|
||
| type FlowService interface { | ||
| ListFlows(userID int64) ([]Flow, error) | ||
| CreateFlow(userID int64, task string) (*Flow, error) | ||
| GetFlowStatus(flowID string) (*Flow, error) | ||
| StopFlow(flowID string) error | ||
| } | ||
|
freddycodes23 marked this conversation as resolved.
|
||
|
|
||
| type Flow struct { | ||
| ID string | ||
| Title string | ||
| Status string | ||
| } | ||
|
|
||
| type Handler struct { | ||
| api *tgbotapi.BotAPI | ||
| svc FlowService | ||
| } | ||
|
|
||
| func NewHandler(api *tgbotapi.BotAPI, svc FlowService) *Handler { | ||
| return &Handler{api: api, svc: svc} | ||
| } | ||
|
|
||
| func (h *Handler) Handle(msg *tgbotapi.Message) { | ||
| switch msg.Command() { | ||
| case "start": | ||
| h.reply(msg, FormatWelcome()) | ||
| case "help": | ||
| h.reply(msg, FormatHelp()) | ||
| case "flows": | ||
| flows, err := h.svc.ListFlows(msg.From.ID) | ||
| if err != nil { | ||
| h.reply(msg, fmt.Sprintf("Error fetching flows: %v", err)) | ||
| return | ||
| } | ||
| h.reply(msg, FormatFlowList(flows)) | ||
| case "new": | ||
| task := msg.CommandArguments() | ||
| if task == "" { | ||
| h.reply(msg, "Usage: /new <task description>") | ||
| return | ||
| } | ||
| flow, err := h.svc.CreateFlow(msg.From.ID, task) | ||
| if err != nil { | ||
| h.reply(msg, fmt.Sprintf("Error creating flow: %v", err)) | ||
| return | ||
| } | ||
| h.reply(msg, FormatFlowCreated(flow)) | ||
| case "status": | ||
| id := msg.CommandArguments() | ||
| if id == "" { | ||
| h.reply(msg, "Usage: /status <flow_id>") | ||
| return | ||
| } | ||
| flow, err := h.svc.GetFlowStatus(id) | ||
| if err != nil { | ||
| h.reply(msg, fmt.Sprintf("Error: %v", err)) | ||
| return | ||
| } | ||
| h.reply(msg, FormatFlowStatus(flow)) | ||
| case "stop": | ||
| id := msg.CommandArguments() | ||
| if id == "" { | ||
| h.reply(msg, "Usage: /stop <flow_id>") | ||
| return | ||
| } | ||
| if err := h.svc.StopFlow(id); err != nil { | ||
| h.reply(msg, fmt.Sprintf("Error: %v", err)) | ||
| return | ||
| } | ||
| h.reply(msg, "Flow stopped.") | ||
| default: | ||
| h.reply(msg, "Unknown command. Send /help for available commands.") | ||
| } | ||
| } | ||
|
|
||
| func (h *Handler) reply(msg *tgbotapi.Message, text string) { | ||
| m := tgbotapi.NewMessage(msg.Chat.ID, text) | ||
| m.ParseMode = tgbotapi.ModeMarkdown | ||
| h.api.Send(m) | ||
|
freddycodes23 marked this conversation as resolved.
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.