feat(botapi): add sendChatAction HTTP Bot API method

New POST /bot<token>/sendChatAction endpoint. Maps the standard
chat_action strings (typing, upload_photo, record_video,
upload_video, record_voice/audio, upload_voice/audio,
upload_document, find_location, choose_sticker, record/upload video
note, cancel) to the matching MTProto SendMessageAction and pushes it
through the same UpdateUserTyping/UpdateChannelUserTyping mechanism
messages.setTyping already uses -- just addressed by botID/chatID
instead of a live session. Unrecognized action strings fall back to
'typing' rather than erroring.

Wired into: Router.BotAPISendChatAction (internal/rpc/botapi_gateway.go),
GatewayService interface + HTTP dispatch + handler (internal/botapi/server.go).
No main.go changes needed -- Router already satisfies GatewayService
structurally.

This is a first addition toward filling out the Bot API surface (31
methods were implemented; common ones like forwardMessage,
getChatMember, banChatMember, pinChatMessage etc. are still missing)
-- added incrementally rather than all at once given no working Go
toolchain in this sandbox to compile-verify against.

Verified: gofmt only (caught and fixed one real bug during editing --
a dropped function signature that broke the rest of the file, now
confirmed clean). Build/test locally:
  go build ./...
  go test ./internal/rpc/... ./internal/botapi/...
This commit is contained in:
Claude
2026-08-02 02:49:04 +00:00
Unverified
parent d47e8cc473
commit 84dfd050e6
2 changed files with 104 additions and 0 deletions
+31
View File
@@ -49,6 +49,7 @@ type GatewayService interface {
BotAPIDeleteMessage(ctx context.Context, botID, chatID int64, messageID int) (bool, error)
BotAPIAnswerCallbackQuery(ctx context.Context, botID int64, callbackQueryID, text, url string, showAlert bool, cacheTime int) (bool, error)
BotAPIGetFile(ctx context.Context, botID int64, locationKey string, offset int64, limit int) (domain.FileChunk, bool, error)
BotAPISendChatAction(ctx context.Context, botID, chatID int64, action string) (bool, error)
}
type EphemeralGatewayService interface {
@@ -246,6 +247,8 @@ func (h *handler) handle(w http.ResponseWriter, r *http.Request) {
h.answerCallbackQuery(w, r, botID)
case "getfile":
h.getFile(w, r, botID)
case "sendchataction":
h.sendChatAction(w, r, botID)
case "deletewebhook":
h.deleteWebhook(w, r, botID)
case "getwebhookinfo":
@@ -763,6 +766,34 @@ func (h *handler) deleteMessage(w http.ResponseWriter, r *http.Request, botID in
writeAPIOK(w, ok)
}
func (h *handler) sendChatAction(w http.ResponseWriter, r *http.Request, botID int64) {
if h.gateway == nil {
writeAPIError(w, http.StatusNotImplemented, "METHOD_NOT_FOUND")
return
}
values, err := requestValues(r)
if err != nil {
writeAPIError(w, http.StatusBadRequest, "BAD_REQUEST")
return
}
chatID, err := strconv.ParseInt(strings.TrimSpace(values["chat_id"]), 10, 64)
if err != nil || chatID == 0 {
writeAPIError(w, http.StatusBadRequest, "CHAT_ID_INVALID")
return
}
action := strings.TrimSpace(values["action"])
if action == "" {
writeAPIError(w, http.StatusBadRequest, "ACTION_INVALID")
return
}
ok, err := h.gateway.BotAPISendChatAction(r.Context(), botID, chatID, action)
if err != nil {
writeAPIError(w, http.StatusBadRequest, apiErrorDescription(err))
return
}
writeAPIOK(w, ok)
}
func (h *handler) answerCallbackQuery(w http.ResponseWriter, r *http.Request, botID int64) {
if h.gateway == nil {
writeAPIError(w, http.StatusNotImplemented, "METHOD_NOT_FOUND")
+73
View File
@@ -229,6 +229,79 @@ func (r *Router) BotAPISendMessage(ctx context.Context, botID, chatID int64, tex
return res.SenderMessage, nil
}
// BotAPISendChatAction pushes a typing-style indicator (e.g. "typing",
// "upload_photo") to a chat, the same way messages.setTyping does at the
// MTProto layer, just addressed by botID/chatID instead of a session.
func (r *Router) BotAPISendChatAction(ctx context.Context, botID, chatID int64, action string) (bool, error) {
if r == nil || botID == 0 {
return false, errors.New("BOT_INVALID")
}
peer, ok := botAPIPeerFromChatID(chatID)
if !ok {
return false, errors.New("CHAT_ID_INVALID")
}
tlAction := botAPIChatActionToTL(action)
if peer.Type == domain.PeerTypeChannel {
if peer.ID == 0 {
return false, errors.New("CHAT_ID_INVALID")
}
updates := &tg.Updates{
Updates: []tg.UpdateClass{&tg.UpdateChannelUserTyping{
ChannelID: peer.ID,
FromID: &tg.PeerUser{UserID: botID},
Action: tlAction,
}},
Date: int(r.clock.Now().Unix()),
}
r.pushChannelViewerUpdates(ctx, 0, peer.ID, nil, func(int64) *tg.Updates { return updates })
return true, nil
}
if peer.Type != domain.PeerTypeUser || peer.ID == 0 || peer.ID == botID {
return false, errors.New("CHAT_ID_INVALID")
}
r.pushUserUpdates(ctx, peer.ID, &tg.Updates{
Updates: []tg.UpdateClass{&tg.UpdateUserTyping{
UserID: botID,
Action: tlAction,
}},
Date: int(r.clock.Now().Unix()),
})
return true, nil
}
// botAPIChatActionToTL maps the Bot API's chat_action strings to the
// matching MTProto SendMessageAction. Unrecognized values fall back to
// "typing" rather than erroring, since new action strings get added to the
// Bot API over time and a missing one shouldn't break the call.
func botAPIChatActionToTL(action string) tg.SendMessageActionClass {
switch strings.ToLower(strings.TrimSpace(action)) {
case "upload_photo":
return &tg.SendMessageUploadPhotoAction{}
case "record_video":
return &tg.SendMessageRecordVideoAction{}
case "upload_video":
return &tg.SendMessageUploadVideoAction{}
case "record_voice", "record_audio":
return &tg.SendMessageRecordAudioAction{}
case "upload_voice", "upload_audio":
return &tg.SendMessageUploadAudioAction{}
case "upload_document":
return &tg.SendMessageUploadDocumentAction{}
case "find_location":
return &tg.SendMessageGeoLocationAction{}
case "choose_sticker":
return &tg.SendMessageChooseStickerAction{}
case "record_video_note":
return &tg.SendMessageRecordRoundAction{}
case "upload_video_note":
return &tg.SendMessageUploadRoundAction{}
case "cancel":
return &tg.SendMessageCancelAction{}
default:
return &tg.SendMessageTypingAction{}
}
}
// BotAPISendMedia sends a photo/document message through the same files service
// and private/channel message state machines used by MTProto sendMedia.
func (r *Router) BotAPISendMedia(ctx context.Context, botID, chatID int64, kind, locationKey, remoteURL, fileName, mimeType string, fileBytes []byte, caption string, entities []domain.MessageEntity, replyMarkup *domain.MessageReplyMarkup, silent bool, replyToMessageID int) (domain.Message, error) {