Skip to content

Commit 533ae3b

Browse files
committed
feat: update GitHub bot command handling, database layer, and repository webhook integration
1 parent b21a331 commit 533ae3b

5 files changed

Lines changed: 37 additions & 8 deletions

File tree

internal/bot/callbacks/callbacks.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,9 @@ func (h *CallbackHandler) handleAddRepoByID(b *gotgbot.Bot, ctx *ext.Context, re
684684

685685
webhookID := createdHook.GetID()
686686
link := models.RepoLink{
687-
RepoFullName: repo.GetFullName(),
688-
WebhookID: webhookID,
687+
RepoFullName: repo.GetFullName(),
688+
WebhookID: webhookID,
689+
MessageThreadID: ctx.EffectiveMessage.MessageThreadId,
689690
}
690691

691692
err = h.DB.AddRepoLink(context.Background(), ctx.EffectiveChat.Id, link)

internal/bot/commands/commands.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@ func (h *CommandHandler) AddRepo(b *gotgbot.Bot, ctx *ext.Context) error {
272272

273273
webhookID := createdHook.GetID()
274274
link := models.RepoLink{
275-
RepoFullName: repoFullName,
276-
WebhookID: webhookID,
275+
RepoFullName: repoFullName,
276+
WebhookID: webhookID,
277+
MessageThreadID: ctx.EffectiveMessage.MessageThreadId,
277278
}
278279

279280
err = h.DB.AddRepoLink(context.Background(), ctx.EffectiveChat.Id, link)

internal/db/db.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,22 @@ func (d *DB) GetRepoLink(ctx context.Context, chatID int64, repoFullName string)
180180
return nil, errors.New("link not found")
181181
}
182182

183+
// GetRepoLinkByWebhookID returns a specific repository link by webhook ID
184+
func (d *DB) GetRepoLinkByWebhookID(ctx context.Context, chatID int64, webhookID int64) (*models.RepoLink, error) {
185+
links, err := d.GetChatLinks(ctx, chatID)
186+
if err != nil {
187+
return nil, err
188+
}
189+
190+
for _, link := range links {
191+
if link.WebhookID == webhookID {
192+
return &link, nil
193+
}
194+
}
195+
196+
return nil, errors.New("link not found")
197+
}
198+
183199
// GetChatsForRepo finds all chats subscribed to a given repository.
184200
func (d *DB) GetChatsForRepo(ctx context.Context, repoFullName string) ([]models.Chat, error) {
185201
cursor, err := d.Chats.Find(ctx, bson.M{"links.repo_full_name": repoFullName})

internal/github/webhooks.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,21 @@ func (s *WebhookServer) processEvent(event interface{}, chatID int64, hookID int
136136

137137
msg = normalizeMessage(msg)
138138

139+
var threadID int64
140+
if hookID != 0 {
141+
link, err := s.DB.GetRepoLinkByWebhookID(context.Background(), chatID, hookID)
142+
if err == nil && link != nil {
143+
threadID = link.MessageThreadID
144+
}
145+
}
146+
139147
opts := &gotgbot.SendMessageOpts{
140148
ParseMode: "MarkdownV2",
141149
LinkPreviewOptions: &gotgbot.LinkPreviewOptions{
142150
IsDisabled: true,
143151
},
144-
ReplyMarkup: markup,
152+
ReplyMarkup: markup,
153+
MessageThreadId: threadID,
145154
}
146155

147156
sentMsg, err := s.Bot.SendMessage(chatID, msg, opts)
@@ -151,7 +160,8 @@ func (s *WebhookServer) processEvent(event interface{}, chatID int64, hookID int
151160
LinkPreviewOptions: &gotgbot.LinkPreviewOptions{
152161
IsDisabled: true,
153162
},
154-
ReplyMarkup: markup,
163+
ReplyMarkup: markup,
164+
MessageThreadId: threadID,
155165
}
156166
sentMsg, err = s.Bot.SendMessage(chatID, plainTextForTelegram(msg), fallbackOpts)
157167
if err != nil {

internal/models/models.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ type User struct {
1515

1616
// RepoLink represents a link to a GitHub repository within a chat
1717
type RepoLink struct {
18-
RepoFullName string `bson:"repo_full_name" json:"repo_full_name"`
19-
WebhookID int64 `bson:"webhook_id,omitempty" json:"webhook_id,omitempty"`
18+
RepoFullName string `bson:"repo_full_name" json:"repo_full_name"`
19+
WebhookID int64 `bson:"webhook_id,omitempty" json:"webhook_id,omitempty"`
20+
MessageThreadID int64 `bson:"message_thread_id,omitempty" json:"message_thread_id,omitempty"`
2021
}
2122

2223
// Chat represents a Telegram chat (group, channel, or private)

0 commit comments

Comments
 (0)