• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

orgball2608 / insta-parser-telegram-bot / 15365390507

31 May 2025 04:12PM UTC coverage: 0.0%. Remained the same
15365390507

Pull #1

github

orgball2608
feat: support get current story command
Pull Request #1: Improve: Code base

0 of 822 new or added lines in 15 files covered. (0.0%)

11 existing lines in 4 files now uncovered.

0 of 1424 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/internal/command/commandimpl/story.go
1
package commandimpl
2

3
import (
4
        "context"
5
        "fmt"
6
        "strings"
7
        "time"
8

9
        tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
10
        "github.com/orgball2608/insta-parser-telegram-bot/internal/domain"
11
)
12

13
// HandleCommand processes incoming Telegram commands
14
func (c *CommandImpl) HandleCommand() error {
×
15
        ctx, cancel := context.WithCancel(context.Background())
×
16
        defer cancel()
×
17

×
18
        u := tgbotapi.NewUpdate(0)
×
19
        u.Timeout = 60
×
20

×
21
        updates, err := c.Telegram.GetUpdatesChan(u)
×
22
        if err != nil {
×
23
                c.Logger.Error("Error getting updates from telegram", "error", err)
×
24
                return fmt.Errorf("failed to get telegram updates: %w", err)
×
25
        }
×
26

27
        c.Logger.Info("Starting command handler, waiting for messages")
×
28

×
29
        for update := range updates {
×
30
                if update.Message == nil {
×
31
                        continue
×
32
                }
33

34
                c.Logger.Info("Message received",
×
35
                        "from", update.Message.From.UserName,
×
36
                        "text", update.Message.Text)
×
37

×
38
                if update.Message.IsCommand() {
×
39
                        if err := c.processCommand(ctx, update); err != nil {
×
40
                                c.Logger.Error("Error processing command",
×
41
                                        "command", update.Message.Command(),
×
42
                                        "error", err)
×
43

×
44
                                // Send error message to user
×
45
                                _, _ = c.Telegram.SendMessage(update.Message.Chat.ID,
×
46
                                        fmt.Sprintf("Error: %s", err.Error()))
×
47
                        }
×
48
                }
49
        }
50

51
        return nil
×
52
}
53

54
// processCommand handles individual commands from updates
55
func (c *CommandImpl) processCommand(ctx context.Context, update tgbotapi.Update) error {
×
56
        command := update.Message.Command()
×
57

×
58
        switch command {
×
59
        case "story":
×
60
                return c.handleStoryCommand(ctx, update)
×
NEW
61
        case "highlights":
×
NEW
62
                return c.handleHighlightsCommand(ctx, update)
×
63
        // Add other commands here
64
        default:
×
65
                _, err := c.Telegram.SendMessage(update.Message.Chat.ID,
×
NEW
66
                        "Unknown command. Available commands:\n"+
×
NEW
67
                                "/story <username> - Get user's current stories\n"+
×
NEW
68
                                "/highlights <username> - Get user highlights")
×
UNCOV
69
                return err
×
70
        }
71
}
72

73
// handleStoryCommand processes the /story command (formerly currentstory)
74
func (c *CommandImpl) handleStoryCommand(ctx context.Context, update tgbotapi.Update) error {
×
75
        // Extract username from command
×
76
        args := strings.TrimSpace(strings.TrimPrefix(update.Message.Text, "/story"))
×
77
        userName := strings.TrimSpace(args)
×
78

×
79
        if userName == "" {
×
80
                _, err := c.Telegram.SendMessage(update.Message.Chat.ID,
×
81
                        "Please provide a username: /story <username>")
×
82
                return err
×
83
        }
×
84

85
        // Send initial response
86
        _, err := c.Telegram.SendMessage(update.Message.Chat.ID,
×
NEW
87
                fmt.Sprintf("Getting current stories for user: %s...", userName))
×
NEW
88
        if err != nil {
×
NEW
89
                return fmt.Errorf("failed to send initial message: %w", err)
×
NEW
90
        }
×
91

92
        // Set timeout for story fetching
NEW
93
        ctxWithTimeout, cancel := context.WithTimeout(ctx, 30*time.Second)
×
NEW
94
        defer cancel()
×
NEW
95

×
NEW
96
        // Get current user stories
×
NEW
97
        stories, err := c.Instagram.GetUserStories(userName)
×
NEW
98
        if err != nil {
×
NEW
99
                return fmt.Errorf("failed to get stories for %s: %w", userName, err)
×
NEW
100
        }
×
101

NEW
102
        c.Logger.Info("Retrieved stories", "username", userName, "count", len(stories))
×
NEW
103

×
NEW
104
        if len(stories) == 0 {
×
NEW
105
                _, err := c.Telegram.SendMessage(update.Message.Chat.ID,
×
NEW
106
                        fmt.Sprintf("No current stories found for user: %s", userName))
×
NEW
107
                return err
×
NEW
108
        }
×
109

110
        // Clear previous current stories for this user
NEW
111
        if err := c.Parser.ClearCurrentStories(userName); err != nil {
×
NEW
112
                c.Logger.Error("Error clearing current stories", "error", err)
×
NEW
113
                // Continue processing even if clearing fails
×
NEW
114
        }
×
115

NEW
116
        processedCount := 0
×
NEW
117

×
NEW
118
        // Process each story and save to the CurrentStory repository
×
NEW
119
        for _, item := range stories {
×
NEW
120
                // Add cancellation check
×
NEW
121
                select {
×
NEW
122
                case <-ctxWithTimeout.Done():
×
NEW
123
                        return fmt.Errorf("operation timed out")
×
NEW
124
                default:
×
NEW
125
                        // Store media URL
×
NEW
126
                        var mediaURL string
×
NEW
127
                        if len(item.Videos) > 0 {
×
NEW
128
                                mediaURL = item.Videos[0].URL
×
NEW
129
                        } else if len(item.Images.Versions) > 0 {
×
NEW
130
                                mediaURL = item.Images.Versions[0].URL
×
NEW
131
                        }
×
132

NEW
133
                        if mediaURL == "" {
×
NEW
134
                                continue
×
135
                        }
136

137
                        // Create a CurrentStory entity
NEW
138
                        currentStory := domain.CurrentStory{
×
NEW
139
                                UserName:  userName,
×
NEW
140
                                MediaURL:  mediaURL,
×
NEW
141
                                CreatedAt: time.Now(),
×
NEW
142
                        }
×
NEW
143

×
NEW
144
                        // Save to repository
×
NEW
145
                        if err := c.Parser.SaveCurrentStory(currentStory); err != nil {
×
NEW
146
                                c.Logger.Error("Error saving current story", "error", err)
×
NEW
147
                                continue
×
148
                        }
149

150
                        // Send media to Telegram
NEW
151
                        c.Telegram.SendImageToChanelByUrl(mediaURL)
×
NEW
152
                        processedCount++
×
153
                }
154
        }
155

156
        // Send a completion message
NEW
157
        _, err = c.Telegram.SendMessage(update.Message.Chat.ID,
×
NEW
158
                fmt.Sprintf("Processed %d current stories for %s", processedCount, userName))
×
NEW
159

×
NEW
160
        return err
×
161
}
162

163
// handleHighlightsCommand processes the /highlights command
NEW
164
func (c *CommandImpl) handleHighlightsCommand(ctx context.Context, update tgbotapi.Update) error {
×
NEW
165
        // Extract username from command
×
NEW
166
        args := strings.TrimSpace(strings.TrimPrefix(update.Message.Text, "/highlights"))
×
NEW
167
        userName := strings.TrimSpace(args)
×
NEW
168

×
NEW
169
        if userName == "" {
×
NEW
170
                _, err := c.Telegram.SendMessage(update.Message.Chat.ID,
×
NEW
171
                        "Please provide a username: /highlights <username>")
×
NEW
172
                return err
×
NEW
173
        }
×
174

175
        // Send initial response
NEW
176
        _, err := c.Telegram.SendMessage(update.Message.Chat.ID,
×
NEW
177
                fmt.Sprintf("Getting highlights for user: %s...", userName))
×
178
        if err != nil {
×
179
                return fmt.Errorf("failed to send initial message: %w", err)
×
180
        }
×
181

182
        // Set timeout for highlight fetching
183
        ctxWithTimeout, cancel := context.WithTimeout(ctx, 30*time.Second)
×
184
        defer cancel()
×
185

×
NEW
186
        // Get user highlights
×
187
        highlights, err := c.Instagram.GetUserHighlights(userName)
×
188
        if err != nil {
×
189
                return fmt.Errorf("failed to get highlights for %s: %w", userName, err)
×
190
        }
×
191

192
        c.Logger.Info("Retrieved highlights", "username", userName, "count", len(highlights))
×
193

×
194
        if len(highlights) == 0 {
×
195
                _, err := c.Telegram.SendMessage(update.Message.Chat.ID,
×
196
                        fmt.Sprintf("No highlights found for user: %s", userName))
×
197
                return err
×
198
        }
×
199

NEW
200
        processedCount := 0
×
NEW
201

×
NEW
202
        // Process each highlight and save to the Highlights repository
×
203
        for _, highlight := range highlights {
×
204
                c.Logger.Info("Processing highlight", "title", highlight.Title, "items", len(highlight.Items))
×
205

×
206
                if len(highlight.Items) == 0 {
×
207
                        continue
×
208
                }
209

210
                // Add cancellation check
211
                select {
×
212
                case <-ctxWithTimeout.Done():
×
213
                        return fmt.Errorf("operation timed out")
×
214
                default:
×
NEW
215
                        // Process each item in the highlight
×
NEW
216
                        for _, item := range highlight.Items {
×
NEW
217
                                // Store media URL
×
NEW
218
                                var mediaURL string
×
NEW
219
                                if len(item.Videos) > 0 {
×
NEW
220
                                        mediaURL = item.Videos[0].URL
×
NEW
221
                                } else if len(item.Images.Versions) > 0 {
×
NEW
222
                                        mediaURL = item.Images.Versions[0].URL
×
NEW
223
                                }
×
224

NEW
225
                                if mediaURL == "" {
×
NEW
226
                                        continue
×
227
                                }
228

229
                                // Create a Highlights entity
NEW
230
                                highlightItem := domain.Highlights{
×
NEW
231
                                        UserName:  userName,
×
NEW
232
                                        MediaURL:  mediaURL,
×
NEW
233
                                        CreatedAt: time.Now(),
×
NEW
234
                                }
×
NEW
235

×
NEW
236
                                // Save to repository
×
NEW
237
                                if err := c.Parser.SaveHighlight(highlightItem); err != nil {
×
NEW
238
                                        c.Logger.Error("Error saving highlight", "error", err)
×
NEW
239
                                        continue
×
240
                                }
241

242
                                // Send media to Telegram
NEW
243
                                c.Telegram.SendImageToChanelByUrl(mediaURL)
×
NEW
244
                                processedCount++
×
245
                        }
246
                }
247
        }
248

249
        // Send a completion message
250
        _, err = c.Telegram.SendMessage(update.Message.Chat.ID,
×
NEW
251
                fmt.Sprintf("Processed %d highlight items for %s", processedCount, userName))
×
252

×
253
        return err
×
254
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc