Skip to content

Commit

Permalink
Add constants for chat actions (#179)
Browse files Browse the repository at this point in the history
* Add constants for chat actions

* Generate chat actions from the field description
  • Loading branch information
megum1n authored Jul 12, 2024
1 parent 422f716 commit 00c0e11
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
15 changes: 15 additions & 0 deletions gen_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
54 changes: 54 additions & 0 deletions scripts/generate/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"regexp"
"strings"
)

Expand All @@ -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)
Expand Down Expand Up @@ -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<action>[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)
}

0 comments on commit 00c0e11

Please sign in to comment.