diff --git a/internal/botapi/server.go b/internal/botapi/server.go index 5495724c..44ae4076 100644 --- a/internal/botapi/server.go +++ b/internal/botapi/server.go @@ -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") diff --git a/internal/rpc/botapi_gateway.go b/internal/rpc/botapi_gateway.go index faf232d2..f8da0dd9 100644 --- a/internal/rpc/botapi_gateway.go +++ b/internal/rpc/botapi_gateway.go @@ -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) {