An EphemeralReply is a status notice a channel delivers and then removes once its TTL elapses, so a transient placeholder does not linger in the chat history after it is superseded. The TTL lives on the reply, the Delete capability joins AdapterCapabilities alongside Media, and channels.EphemeralSender does the send-then-retract with an injected clock. A sender that cannot delete still delivers the reply and logs the missed retraction instead of failing the turn. Telegram implements MessageDeleter through deleteMessage and honours the mechanism for the "Archie reloaded." status notice; the pre-restart "Reloading…" message remains the durable acknowledgement.
61 lines
2.1 KiB
Go
61 lines
2.1 KiB
Go
package telegram
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/go-telegram/bot"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
|
)
|
|
|
|
// telegramMessageDeleter removes messages the launch that built it sent,
|
|
// through the Bot API's deleteMessage method.
|
|
//
|
|
// It captures the *bot.Bot at construction rather than reading a Gateway
|
|
// field, matching telegramMediaSender and telegramApprover: a deleter belongs
|
|
// to the launch that built it, so a /restart abandons in-flight retractions
|
|
// with the outgoing bot instance instead of silently directing them through
|
|
// the new one.
|
|
type telegramMessageDeleter struct {
|
|
bot *bot.Bot
|
|
chatID int64
|
|
}
|
|
|
|
var (
|
|
_ messaging.MessageDeleter = (*telegramMessageDeleter)(nil)
|
|
_ messaging.CapabilityReporter = (*telegramMessageDeleter)(nil)
|
|
)
|
|
|
|
// NewMessageDeleter returns a MessageDeleter bound to one chat and to the bot
|
|
// instance of the launch that created it. Construct it per launch, from the
|
|
// same b that handled the update.
|
|
func (g *Gateway) NewMessageDeleter(b *bot.Bot, chatID int64) messaging.MessageDeleter {
|
|
return &telegramMessageDeleter{bot: b, chatID: chatID}
|
|
}
|
|
|
|
// Capabilities reports Delete support. Telegram can remove any message the
|
|
// bot sent in a chat it administers, so this is unconditionally true.
|
|
func (d *telegramMessageDeleter) Capabilities() messaging.AdapterCapabilities {
|
|
return messaging.AdapterCapabilities{Delete: true}
|
|
}
|
|
|
|
// DeleteMessage removes the message event names. The event's ID is the
|
|
// Telegram message ID the send returned; a non-numeric one cannot address a
|
|
// platform message, so it is reported rather than sent to the API as a
|
|
// doomed request.
|
|
func (d *telegramMessageDeleter) DeleteMessage(ctx context.Context, event messaging.MessageEvent) error {
|
|
id, err := strconv.Atoi(event.ID)
|
|
if err != nil {
|
|
return fmt.Errorf("telegram delete: message id %q is not numeric: %w", event.ID, err)
|
|
}
|
|
if _, err := d.bot.DeleteMessage(ctx, &bot.DeleteMessageParams{
|
|
ChatID: d.chatID,
|
|
MessageID: id,
|
|
}); err != nil {
|
|
return fmt.Errorf("telegram delete message %d: %w", id, err)
|
|
}
|
|
return nil
|
|
}
|