From 00c0e11b9921089ca6908997257fd7ca92b0f2cb Mon Sep 17 00:00:00 2001 From: Megum1n Date: Fri, 12 Jul 2024 23:01:14 +0200 Subject: [PATCH] Add constants for chat actions (#179) * Add constants for chat actions * Generate chat actions from the field description --- gen_consts.go | 15 +++++++++++ scripts/generate/consts.go | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/gen_consts.go b/gen_consts.go index a118f15..df523b9 100755 --- a/gen_consts.go +++ b/gen_consts.go @@ -111,6 +111,21 @@ const ( ParseModeNone = "" ) +// The consts listed below represent all the chat action options that can be sent to telegram. +const ( + ChatActionTyping = "typing" + ChatActionUploadPhoto = "upload_photo" + ChatActionRecordVideo = "record_video" + ChatActionUploadVideo = "upload_video" + ChatActionRecordVoice = "record_voice" + ChatActionUploadVoice = "upload_voice" + ChatActionUploadDocument = "upload_document" + ChatActionChooseSticker = "choose_sticker" + ChatActionFindLocation = "find_location" + ChatActionRecordVideoNote = "record_video_note" + ChatActionUploadVideoNote = "upload_video_note" +) + // The consts listed below represent all the sticker types that can be obtained from telegram. const ( StickerTypeRegular = "regular" diff --git a/scripts/generate/consts.go b/scripts/generate/consts.go index d6d0dee..0f1b631 100644 --- a/scripts/generate/consts.go +++ b/scripts/generate/consts.go @@ -3,6 +3,7 @@ package main import ( "errors" "fmt" + "regexp" "strings" ) @@ -24,6 +25,12 @@ package gotgbot consts.WriteString(generateParseModeConsts()) + chatActions, err := generateChatActionConsts(d) + if err != nil { + return fmt.Errorf("failed to generate consts for chat actions: %w", err) + } + consts.WriteString(chatActions) + stickerTypeConsts, err := generateTypeConsts(d, "Sticker") if err != nil { return fmt.Errorf("failed to generate consts for sticker types: %w", err) @@ -122,6 +129,53 @@ func generateParseModeConsts() string { return out.String() } +func generateChatActionConsts(d APIDescription) (string, error) { + methodName := "sendChatAction" + fieldName := "action" + + sendChatActionMethod, ok := d.Methods[methodName] + if !ok { + return "", errors.New("missing '" + methodName + "' method data") + } + + var description string + + for _, field := range sendChatActionMethod.Fields { + if field.Name == fieldName { + description = field.Description + + break + } + } + + if description == "" { + return "", errors.New("missing '" + fieldName + "' method field") + } + + // Parse chat action from the description + var chatActions []string + + re := regexp.MustCompile(`(?P[a-z_]+) f?or`) + results := re.FindAllStringSubmatch(description, -1) + + for _, result := range results { + chatActions = append(chatActions, result[1]) + } + + out := strings.Builder{} + out.WriteString("\n// The consts listed below represent all the chat action options that can be sent to telegram.") + out.WriteString("\nconst (") + + for _, a := range chatActions { + constName := "ChatAction" + snakeToTitle(a) + out.WriteString(writeConst(constName, a)) + } + + out.WriteString(")\n\n") + + return out.String(), nil +} + func writeConst(name string, value string) string { return fmt.Sprintf("\n%s = \"%s\"", name, value) }