chore(examples): use api.Ptr for optional pointer fields

Replace the throwaway-local-var-then-take-address pattern with
api.Ptr in the polls and moderation examples. Net effect: 14 lines
gone, the pointer wrangling no longer steals visual focus from the
actual API call.
This commit is contained in:
2026-05-09 18:20:00 +01:00
parent f3fbef64ca
commit 1ff26006f7
2 changed files with 12 additions and 16 deletions
+11 -14
View File
@@ -96,11 +96,10 @@ func kickHandler(c *dispatch.Context, m *api.Message) error {
}); err != nil { }); err != nil {
return handleAdminErr(c, m.Chat.ID, err) return handleAdminErr(c, m.Chat.ID, err)
} }
truVal := true
if _, err := api.UnbanChatMember(c.Ctx, c.Bot, &api.UnbanChatMemberParams{ if _, err := api.UnbanChatMember(c.Ctx, c.Bot, &api.UnbanChatMemberParams{
ChatID: api.ChatIDFromInt(m.Chat.ID), ChatID: api.ChatIDFromInt(m.Chat.ID),
UserID: target, UserID: target,
OnlyIfBanned: &truVal, OnlyIfBanned: api.Ptr(true),
}); err != nil { }); err != nil {
log.Printf("unban after kick: %v", err) log.Printf("unban after kick: %v", err)
} }
@@ -130,23 +129,21 @@ func muteHandler(c *dispatch.Context, m *api.Message) error {
reply(c, m.Chat.ID, "Reply to a user's message with /mute to silence them for 1 hour.") reply(c, m.Chat.ID, "Reply to a user's message with /mute to silence them for 1 hour.")
return nil return nil
} }
until := time.Now().Add(time.Hour).Unix()
falseVal := false
if _, err := api.RestrictChatMember(c.Ctx, c.Bot, &api.RestrictChatMemberParams{ if _, err := api.RestrictChatMember(c.Ctx, c.Bot, &api.RestrictChatMemberParams{
ChatID: api.ChatIDFromInt(m.Chat.ID), ChatID: api.ChatIDFromInt(m.Chat.ID),
UserID: target, UserID: target,
Permissions: api.ChatPermissions{ Permissions: api.ChatPermissions{
CanSendMessages: &falseVal, CanSendMessages: api.Ptr(false),
CanSendAudios: &falseVal, CanSendAudios: api.Ptr(false),
CanSendDocuments: &falseVal, CanSendDocuments: api.Ptr(false),
CanSendPhotos: &falseVal, CanSendPhotos: api.Ptr(false),
CanSendVideos: &falseVal, CanSendVideos: api.Ptr(false),
CanSendVideoNotes: &falseVal, CanSendVideoNotes: api.Ptr(false),
CanSendVoiceNotes: &falseVal, CanSendVoiceNotes: api.Ptr(false),
CanSendPolls: &falseVal, CanSendPolls: api.Ptr(false),
CanSendOtherMessages: &falseVal, CanSendOtherMessages: api.Ptr(false),
}, },
UntilDate: &until, UntilDate: api.Ptr(time.Now().Add(time.Hour).Unix()),
}); err != nil { }); err != nil {
return handleAdminErr(c, m.Chat.ID, err) return handleAdminErr(c, m.Chat.ID, err)
} }
+1 -2
View File
@@ -94,12 +94,11 @@ func main() {
}) })
return nil return nil
} }
isAnon := false
msg, err := api.SendPoll(c.Ctx, c.Bot, &api.SendPollParams{ msg, err := api.SendPoll(c.Ctx, c.Bot, &api.SendPollParams{
ChatID: api.ChatIDFromInt(m.Chat.ID), ChatID: api.ChatIDFromInt(m.Chat.ID),
Question: question, Question: question,
Options: pollOptions, Options: pollOptions,
IsAnonymous: &isAnon, IsAnonymous: api.Ptr(false),
}) })
if err != nil { if err != nil {
return err return err