1+ package telegram
2+
3+ import (
4+ "fmt"
5+
6+ tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
7+ )
8+
9+ type FlowService interface {
10+ ListFlows (userID int64 ) ([]Flow , error )
11+ CreateFlow (userID int64 , task string ) (* Flow , error )
12+ GetFlowStatus (flowID string ) (* Flow , error )
13+ StopFlow (flowID string ) error
14+ }
15+
16+ type Flow struct {
17+ ID string
18+ Title string
19+ Status string
20+ }
21+
22+ type Handler struct {
23+ api * tgbotapi.BotAPI
24+ svc FlowService
25+ }
26+
27+ func NewHandler (api * tgbotapi.BotAPI , svc FlowService ) * Handler {
28+ return & Handler {api : api , svc : svc }
29+ }
30+
31+ func (h * Handler ) Handle (msg * tgbotapi.Message ) {
32+ switch msg .Command () {
33+ case "start" :
34+ h .reply (msg , FormatWelcome ())
35+ case "help" :
36+ h .reply (msg , FormatHelp ())
37+ case "flows" :
38+ flows , err := h .svc .ListFlows (msg .From .ID )
39+ if err != nil {
40+ h .reply (msg , fmt .Sprintf ("Error fetching flows: %v" , err ))
41+ return
42+ }
43+ h .reply (msg , FormatFlowList (flows ))
44+ case "new" :
45+ task := msg .CommandArguments ()
46+ if task == "" {
47+ h .reply (msg , "Usage: /new <task description>" )
48+ return
49+ }
50+ flow , err := h .svc .CreateFlow (msg .From .ID , task )
51+ if err != nil {
52+ h .reply (msg , fmt .Sprintf ("Error creating flow: %v" , err ))
53+ return
54+ }
55+ h .reply (msg , FormatFlowCreated (flow ))
56+ case "status" :
57+ id := msg .CommandArguments ()
58+ if id == "" {
59+ h .reply (msg , "Usage: /status <flow_id>" )
60+ return
61+ }
62+ flow , err := h .svc .GetFlowStatus (id )
63+ if err != nil {
64+ h .reply (msg , fmt .Sprintf ("Error: %v" , err ))
65+ return
66+ }
67+ h .reply (msg , FormatFlowStatus (flow ))
68+ case "stop" :
69+ id := msg .CommandArguments ()
70+ if id == "" {
71+ h .reply (msg , "Usage: /stop <flow_id>" )
72+ return
73+ }
74+ if err := h .svc .StopFlow (id ); err != nil {
75+ h .reply (msg , fmt .Sprintf ("Error: %v" , err ))
76+ return
77+ }
78+ h .reply (msg , "Flow stopped." )
79+ default :
80+ h .reply (msg , "Unknown command. Send /help for available commands." )
81+ }
82+ }
83+
84+ func (h * Handler ) reply (msg * tgbotapi.Message , text string ) {
85+ m := tgbotapi .NewMessage (msg .Chat .ID , text )
86+ m .ParseMode = tgbotapi .ModeMarkdown
87+ h .api .Send (m )
88+ }
0 commit comments