Skip to content

Commit

Permalink
Update typing status when Botka is generating a reply
Browse files Browse the repository at this point in the history
  • Loading branch information
kozaktomas committed Jan 16, 2025
1 parent 9e0c1fa commit 523b9dc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions backend/pkg/hook/botka.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ func (b *Botka) aiHandler() wa.EventHandler {
return true // always match as a backup command
},
HandleFunc: func(from, msg string) error {
err := b.whatsapp.SetTyping(from, true)
if err != nil {
b.logger.Warnf("could not set typing: %v", err)
}

defer func() {
err := b.whatsapp.SetTyping(from, false)
if err != nil {
b.logger.Warnf("could not unset typing: %v", err)
}
}()

conversation, err := b.storage.GetConversation(from)
if err != nil {
return fmt.Errorf("could not get conversation: %w", err)
Expand Down
19 changes: 19 additions & 0 deletions backend/pkg/wa/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func New(ctx context.Context, conf *config.Config, logger *logrus.Logger) *Whats
}
}

err = client.SendPresence(types.PresenceAvailable)
if err != nil {
logger.Errorf("Failed to send presence: %v", err)
}

wa := &WhatsAppClient{
client: client,
store: deviceStore,
Expand Down Expand Up @@ -152,6 +157,20 @@ func (wa *WhatsAppClient) handleIncomingMessage(msg *events.Message) {
}
}

func (wa *WhatsAppClient) SetTyping(to string, typing bool) error {
state := types.ChatPresencePaused
if typing {
state = types.ChatPresenceComposing
}

err := wa.client.SendChatPresence(wa.buildJid(to), state, types.ChatPresenceMediaText)
if err != nil {
return fmt.Errorf("failed to send chat presence: %w", err)
}

return nil
}

func (wa *WhatsAppClient) SendText(to, text string) error {
msg := &waE2E.Message{
Conversation: proto.String(text),
Expand Down

0 comments on commit 523b9dc

Please sign in to comment.