Skip to content

Commit b5d9104

Browse files
committed
feat: Message.UsersShared
1 parent 0477a4f commit b5d9104

File tree

3 files changed

+133
-87
lines changed

3 files changed

+133
-87
lines changed

tgbotapi/message.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package tgbotapi
2+
3+
import (
4+
"strings"
5+
"time"
6+
)
7+
8+
// Message is returned by almost every request, and contains data about almost anything.
9+
type Message struct {
10+
MessageID int `json:"message_id"`
11+
From *User `json:"from,omitempty"` // optional
12+
Date int `json:"date"`
13+
Chat *Chat `json:"chat,omitempty"`
14+
ForwardFrom *User `json:"forward_from,omitempty"` // optional
15+
ForwardDate int `json:"forward_date,omitempty"` // optional
16+
ReplyToMessage *Message `json:"reply_to_message,omitempty"` // optional
17+
Text string `json:"text,omitempty"` // optional
18+
Entities *[]MessageEntity `json:"entities,omitempty"` // optional
19+
Audio *Audio `json:"audio,omitempty"` // optional
20+
Document *Document `json:"document,omitempty"` // optional
21+
Photo *[]PhotoSize `json:"photo,omitempty"` // optional
22+
Sticker *Sticker `json:"sticker,omitempty"` // optional
23+
Video *Video `json:"video,omitempty"` // optional
24+
Voice *Voice `json:"voice,omitempty"` // optional
25+
Caption string `json:"caption,omitempty"` // optional
26+
Contact *Contact `json:"contact,omitempty"` // optional
27+
Location *Location `json:"location,omitempty"` // optional
28+
Venue *Venue `json:"venue,omitempty"` // optional
29+
NewChatParticipant *ChatMember `json:"new_chat_participant,omitempty"` // Obsolete
30+
NewChatMember *ChatMember `json:"new_chat_member,omitempty"` // Obsolete
31+
NewChatMembers []ChatMember `json:"new_chat_members,omitempty"` // optional
32+
LeftChatMember *ChatMember `json:"left_chat_member,omitempty"` // optional
33+
NewChatTitle string `json:"new_chat_title,omitempty"` // optional
34+
NewChatPhoto *[]PhotoSize `json:"new_chat_photo,omitempty"` // optional
35+
DeleteChatPhoto bool `json:"delete_chat_photo,omitempty"` // optional
36+
GroupChatCreated bool `json:"group_chat_created,omitempty"` // optional
37+
SuperGroupChatCreated bool `json:"supergroup_chat_created,omitempty"` // optional
38+
ChannelChatCreated bool `json:"channel_chat_created,omitempty"` // optional
39+
MigrateToChatID int64 `json:"migrate_to_chat_id,omitempty"` // optional
40+
MigrateFromChatID int64 `json:"migrate_from_chat_id,omitempty"` // optional
41+
PinnedMessage *Message `json:"pinned_message,omitempty"` // optional
42+
UsersShared *UsersShared `json:"users_shared,omitempty"` // optional
43+
}
44+
45+
// Time converts the message timestamp into a Time.
46+
func (m *Message) Time() time.Time {
47+
return time.Unix(int64(m.Date), 0)
48+
}
49+
50+
// IsCommand returns true if message starts with '/'.
51+
func (m *Message) IsCommand() bool {
52+
return m.Text != "" && m.Text[0] == '/'
53+
}
54+
55+
// Command checks if the message was a command and if it was, returns the
56+
// command. If the Message was not a command, it returns an empty string.
57+
//
58+
// If the command contains the at bot syntax, it removes the bot name.
59+
func (m *Message) Command() string {
60+
if !m.IsCommand() {
61+
return ""
62+
}
63+
64+
command := strings.SplitN(m.Text, " ", 2)[0][1:]
65+
66+
if i := strings.Index(command, "@"); i != -1 {
67+
command = command[:i]
68+
}
69+
70+
return command
71+
}
72+
73+
// CommandArguments checks if the message was a command and if it was,
74+
// returns all text after the command name. If the Message was not a
75+
// command, it returns an empty string.
76+
func (m *Message) CommandArguments() string {
77+
if !m.IsCommand() {
78+
return ""
79+
}
80+
81+
split := strings.SplitN(m.Text, " ", 2)
82+
if len(split) != 2 {
83+
return ""
84+
}
85+
86+
return strings.SplitN(m.Text, " ", 2)[1]
87+
}

tgbotapi/types.go

Lines changed: 20 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
botsgocore "github.com/bots-go-framework/bots-go-core"
1111
"net/url"
1212
"strconv"
13-
"strings"
14-
"time"
1513
)
1614

1715
// APIResponse is a response from the Telegram API with the result
@@ -194,86 +192,6 @@ func (c *Chat) IsChannel() bool {
194192
return c.Type == "channel"
195193
}
196194

197-
// Message is returned by almost every request, and contains data about almost anything.
198-
type Message struct {
199-
MessageID int `json:"message_id"`
200-
From *User `json:"from,omitempty"` // optional
201-
Date int `json:"date"`
202-
Chat *Chat `json:"chat,omitempty"`
203-
ForwardFrom *User `json:"forward_from,omitempty"` // optional
204-
ForwardDate int `json:"forward_date,omitempty"` // optional
205-
ReplyToMessage *Message `json:"reply_to_message,omitempty"` // optional
206-
Text string `json:"text,omitempty"` // optional
207-
Entities *[]MessageEntity `json:"entities,omitempty"` // optional
208-
Audio *Audio `json:"audio,omitempty"` // optional
209-
Document *Document `json:"document,omitempty"` // optional
210-
Photo *[]PhotoSize `json:"photo,omitempty"` // optional
211-
Sticker *Sticker `json:"sticker,omitempty"` // optional
212-
Video *Video `json:"video,omitempty"` // optional
213-
Voice *Voice `json:"voice,omitempty"` // optional
214-
Caption string `json:"caption,omitempty"` // optional
215-
Contact *Contact `json:"contact,omitempty"` // optional
216-
Location *Location `json:"location,omitempty"` // optional
217-
Venue *Venue `json:"venue,omitempty"` // optional
218-
NewChatParticipant *ChatMember `json:"new_chat_participant,omitempty"` // Obsolete
219-
NewChatMember *ChatMember `json:"new_chat_member,omitempty"` // Obsolete
220-
NewChatMembers []ChatMember `json:"new_chat_members,omitempty"` // optional
221-
LeftChatMember *ChatMember `json:"left_chat_member,omitempty"` // optional
222-
NewChatTitle string `json:"new_chat_title,omitempty"` // optional
223-
NewChatPhoto *[]PhotoSize `json:"new_chat_photo,omitempty"` // optional
224-
DeleteChatPhoto bool `json:"delete_chat_photo,omitempty"` // optional
225-
GroupChatCreated bool `json:"group_chat_created,omitempty"` // optional
226-
SuperGroupChatCreated bool `json:"supergroup_chat_created,omitempty"` // optional
227-
ChannelChatCreated bool `json:"channel_chat_created,omitempty"` // optional
228-
MigrateToChatID int64 `json:"migrate_to_chat_id,omitempty"` // optional
229-
MigrateFromChatID int64 `json:"migrate_from_chat_id,omitempty"` // optional
230-
PinnedMessage *Message `json:"pinned_message,omitempty"` // optional
231-
}
232-
233-
// Time converts the message timestamp into a Time.
234-
func (m *Message) Time() time.Time {
235-
return time.Unix(int64(m.Date), 0)
236-
}
237-
238-
// IsCommand returns true if message starts with '/'.
239-
func (m *Message) IsCommand() bool {
240-
return m.Text != "" && m.Text[0] == '/'
241-
}
242-
243-
// Command checks if the message was a command and if it was, returns the
244-
// command. If the Message was not a command, it returns an empty string.
245-
//
246-
// If the command contains the at bot syntax, it removes the bot name.
247-
func (m *Message) Command() string {
248-
if !m.IsCommand() {
249-
return ""
250-
}
251-
252-
command := strings.SplitN(m.Text, " ", 2)[0][1:]
253-
254-
if i := strings.Index(command, "@"); i != -1 {
255-
command = command[:i]
256-
}
257-
258-
return command
259-
}
260-
261-
// CommandArguments checks if the message was a command and if it was,
262-
// returns all text after the command name. If the Message was not a
263-
// command, it returns an empty string.
264-
func (m *Message) CommandArguments() string {
265-
if !m.IsCommand() {
266-
return ""
267-
}
268-
269-
split := strings.SplitN(m.Text, " ", 2)
270-
if len(split) != 2 {
271-
return ""
272-
}
273-
274-
return strings.SplitN(m.Text, " ", 2)[1]
275-
}
276-
277195
// MessageEntity contains information about data in a Message.
278196
type MessageEntity struct {
279197
Type string `json:"type"`
@@ -424,12 +342,27 @@ type KeyboardButtonRequestUsers struct {
424342
// Must be unique within the message
425343
RequestID int `json:"request_id"`
426344

427-
UserIsBot bool `json:"user_is_bot,omitempty"`
428-
UserIsPremium bool `json:"user_is_premium,omitempty"`
429-
MaxQuantity int `json:"max_quantity,omitempty"` // The maximum number of users to be selected; 1-10. Defaults to 1.
430-
RequestName bool `json:"request_name,omitempty"`
345+
// Optional.
346+
// Pass True to request bots, pass False to request regular users.
347+
// If not specified, no additional restrictions are applied.
348+
UserIsBot bool `json:"user_is_bot,omitempty"`
349+
350+
// Optional.
351+
// Pass True to request premium users, pass False to request non-premium users.
352+
// If not specified, no additional restrictions are applied.
353+
UserIsPremium bool `json:"user_is_premium,omitempty"`
354+
355+
// Optional. The maximum number of users to be selected; 1-10. Defaults to 1.
356+
MaxQuantity int `json:"max_quantity,omitempty"`
357+
358+
// Optional. Pass True to request the users' first and last names
359+
RequestName bool `json:"request_name,omitempty"`
360+
361+
// Optional. Pass True to request the users' usernames
431362
RequestUsername bool `json:"request_username,omitempty"`
432-
RequestPhoto bool `json:"request_photo,omitempty"`
363+
364+
// Optional. Pass True to request the users' photos
365+
RequestPhoto bool `json:"request_photo,omitempty"`
433366
}
434367

435368
// KeyboardButtonRequestChat represents a request from the bot to send a chat

tgbotapi/users_shared.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package tgbotapi
2+
3+
// UsersShared contains information about the users whose identifiers were shared with the bot using a KeyboardButtonRequestUsers button.
4+
// https://core.telegram.org/bots/api#usersshared
5+
type UsersShared struct {
6+
RequestID int `json:"request_id"`
7+
UserIDs []int `json:"user_ids"`
8+
Users []SharedUser `json:"users"`
9+
}
10+
11+
// SharedUser contains information about a user that was shared with the bot using a KeyboardButtonRequestUsers button.
12+
// https://core.telegram.org/bots/api#shareduser
13+
type SharedUser struct {
14+
UserID int `json:"user_id"`
15+
16+
// Optional. First name of the user, if the name was requested by the bot
17+
FirstName string `json:"first_name"`
18+
19+
// Optional. Last name of the user, if the name was requested by the bot
20+
LastName string `json:"last_name"`
21+
22+
// Optional. Username of the user, if the name was requested by the bot
23+
Username string `json:"username"`
24+
25+
Photo []PhotoSize `json:"photo"`
26+
}

0 commit comments

Comments
 (0)