Skip to content

Commit

Permalink
context: implement DeleteAfter helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Jan 21, 2022
1 parent b14a065 commit 1004fb5
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package telebot
import (
"strings"
"sync"
"time"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -123,6 +124,11 @@ type Context interface {
// See Delete from bot.go.
Delete() error

// DeleteAfter waits for the duration to elapse and then removes the
// message. It handles an error automatically using b.OnError callback.
// It returns a Timer that can be used to cancel the call using its Stop method.
DeleteAfter(d time.Duration) *time.Timer

// Notify updates the chat action for the current recipient.
// See Notify from bot.go.
Notify(action ChatAction) error
Expand Down Expand Up @@ -445,6 +451,14 @@ func (c *nativeContext) Delete() error {
return c.b.Delete(msg)
}

func (c *nativeContext) DeleteAfter(d time.Duration) *time.Timer {
return time.AfterFunc(d, func() {
if err := c.Delete(); err != nil {
c.b.OnError(err, c)
}
})
}

func (c *nativeContext) Notify(action ChatAction) error {
return c.b.Notify(c.Recipient(), action)
}
Expand Down

0 comments on commit 1004fb5

Please sign in to comment.