Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constants for chat actions #179

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
30 changes: 30 additions & 0 deletions scripts/generate/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package gotgbot
consts.WriteString(updateConsts)

consts.WriteString(generateParseModeConsts())
consts.WriteString(generateChatActionConsts())

stickerTypeConsts, err := generateTypeConsts(d, "Sticker")
if err != nil {
Expand Down Expand Up @@ -122,6 +123,35 @@ func generateParseModeConsts() string {
return out.String()
}

func generateChatActionConsts() string {
chatActions := []string{
"typing",
"upload_photo",
"record_video",
"upload_video",
"record_voice",
"upload_voice",
"upload_document",
"choose_sticker",
"find_location",
"record_video_note",
"upload_video_note",
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible for this list to be obtained from the type spec?
For example, by using some regexes on the sendChatAction method's "action" field description?

If the list is defined manually, then it might as well not be generated; it's just a manually written list, and any changes on telegrams side wouldn't reflect.
I know it's done this way for formatting consts, but that was intended as an exception; parsemodes aren't expected to change, and are relatively short list!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PaulSonOfLars I agree, let's try to actually generate it.
I decided not to create universal function in this case because it would require a lot of input.

func generateConstsFromMethodFieldDescription(d APIDescription, methodName string, fieldName string, regexp string) (string, error) {
    ...
}

Please let me know if current code sucks.


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()
}

func writeConst(name string, value string) string {
return fmt.Sprintf("\n%s = \"%s\"", name, value)
}