Skip to content

Commit a1207f6

Browse files
author
Syfaro
committed
allow ChannelUsername instead of ChatID, fix replies with SendPhoto
1 parent 0d2feed commit a1207f6

File tree

1 file changed

+103
-24
lines changed

1 file changed

+103
-24
lines changed

methods.go

Lines changed: 103 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const (
4949
// MessageConfig contains information about a SendMessage request.
5050
type MessageConfig struct {
5151
ChatID int
52+
ChannelUsername string
5253
Text string
5354
ParseMode string
5455
DisableWebPagePreview bool
@@ -58,14 +59,17 @@ type MessageConfig struct {
5859

5960
// ForwardConfig contains information about a ForwardMessage request.
6061
type ForwardConfig struct {
61-
ChatID int
62-
FromChatID int
63-
MessageID int
62+
ChatID int
63+
ChannelUsername string
64+
FromChatID int
65+
FromChannelUsername string
66+
MessageID int
6467
}
6568

6669
// PhotoConfig contains information about a SendPhoto request.
6770
type PhotoConfig struct {
6871
ChatID int
72+
ChannelUsername string
6973
Caption string
7074
ReplyToMessageID int
7175
ReplyMarkup interface{}
@@ -78,6 +82,7 @@ type PhotoConfig struct {
7882
// AudioConfig contains information about a SendAudio request.
7983
type AudioConfig struct {
8084
ChatID int
85+
ChannelUsername string
8186
Duration int
8287
Performer string
8388
Title string
@@ -92,6 +97,7 @@ type AudioConfig struct {
9297
// DocumentConfig contains information about a SendDocument request.
9398
type DocumentConfig struct {
9499
ChatID int
100+
ChannelUsername string
95101
ReplyToMessageID int
96102
ReplyMarkup interface{}
97103
UseExistingDocument bool
@@ -103,6 +109,7 @@ type DocumentConfig struct {
103109
// StickerConfig contains information about a SendSticker request.
104110
type StickerConfig struct {
105111
ChatID int
112+
ChannelUsername string
106113
ReplyToMessageID int
107114
ReplyMarkup interface{}
108115
UseExistingSticker bool
@@ -114,6 +121,7 @@ type StickerConfig struct {
114121
// VideoConfig contains information about a SendVideo request.
115122
type VideoConfig struct {
116123
ChatID int
124+
ChannelUsername string
117125
Duration int
118126
Caption string
119127
ReplyToMessageID int
@@ -127,6 +135,7 @@ type VideoConfig struct {
127135
// VoiceConfig contains information about a SendVoice request.
128136
type VoiceConfig struct {
129137
ChatID int
138+
ChannelUsername string
130139
Duration int
131140
ReplyToMessageID int
132141
ReplyMarkup interface{}
@@ -139,6 +148,7 @@ type VoiceConfig struct {
139148
// LocationConfig contains information about a SendLocation request.
140149
type LocationConfig struct {
141150
ChatID int
151+
ChannelUsername string
142152
Latitude float64
143153
Longitude float64
144154
ReplyToMessageID int
@@ -147,8 +157,9 @@ type LocationConfig struct {
147157

148158
// ChatActionConfig contains information about a SendChatAction request.
149159
type ChatActionConfig struct {
150-
ChatID int
151-
Action string
160+
ChatID int
161+
ChannelUsername string
162+
Action string
152163
}
153164

154165
// UserProfilePhotosConfig contains information about a GetUserProfilePhotos request.
@@ -322,7 +333,11 @@ func (bot *BotAPI) GetMe() (User, error) {
322333
// DisableWebPagePreview, ReplyToMessageID, and ReplyMarkup are optional.
323334
func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) {
324335
v := url.Values{}
325-
v.Add("chat_id", strconv.Itoa(config.ChatID))
336+
if config.ChannelUsername != "" {
337+
v.Add("chat_id", config.ChannelUsername)
338+
} else {
339+
v.Add("chat_id", strconv.Itoa(config.ChatID))
340+
}
326341
v.Add("text", config.Text)
327342
v.Add("disable_web_page_preview", strconv.FormatBool(config.DisableWebPagePreview))
328343
if config.ParseMode != "" {
@@ -361,8 +376,16 @@ func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) {
361376
// Requires ChatID (destination), FromChatID (source), and MessageID.
362377
func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) {
363378
v := url.Values{}
364-
v.Add("chat_id", strconv.Itoa(config.ChatID))
365-
v.Add("from_chat_id", strconv.Itoa(config.FromChatID))
379+
if config.ChannelUsername != "" {
380+
v.Add("chat_id", config.ChannelUsername)
381+
} else {
382+
v.Add("chat_id", strconv.Itoa(config.ChatID))
383+
}
384+
if config.FromChannelUsername != "" {
385+
v.Add("chat_id", config.FromChannelUsername)
386+
} else {
387+
v.Add("chat_id", strconv.Itoa(config.FromChatID))
388+
}
366389
v.Add("message_id", strconv.Itoa(config.MessageID))
367390

368391
resp, err := bot.MakeRequest("forwardMessage", v)
@@ -389,13 +412,17 @@ func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) {
389412
func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) {
390413
if config.UseExistingPhoto {
391414
v := url.Values{}
392-
v.Add("chat_id", strconv.Itoa(config.ChatID))
415+
if config.ChannelUsername != "" {
416+
v.Add("chat_id", config.ChannelUsername)
417+
} else {
418+
v.Add("chat_id", strconv.Itoa(config.ChatID))
419+
}
393420
v.Add("photo", config.FileID)
394421
if config.Caption != "" {
395422
v.Add("caption", config.Caption)
396423
}
397424
if config.ReplyToMessageID != 0 {
398-
v.Add("reply_to_message_id", strconv.Itoa(config.ChatID))
425+
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
399426
}
400427
if config.ReplyMarkup != nil {
401428
data, err := json.Marshal(config.ReplyMarkup)
@@ -423,7 +450,11 @@ func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) {
423450
}
424451

425452
params := make(map[string]string)
426-
params["chat_id"] = strconv.Itoa(config.ChatID)
453+
if config.ChannelUsername != "" {
454+
params["chat_id"] = config.ChannelUsername
455+
} else {
456+
params["chat_id"] = strconv.Itoa(config.ChatID)
457+
}
427458
if config.Caption != "" {
428459
params["caption"] = config.Caption
429460
}
@@ -475,7 +506,11 @@ func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) {
475506
func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) {
476507
if config.UseExistingAudio {
477508
v := url.Values{}
478-
v.Add("chat_id", strconv.Itoa(config.ChatID))
509+
if config.ChannelUsername != "" {
510+
v.Add("chat_id", config.ChannelUsername)
511+
} else {
512+
v.Add("chat_id", strconv.Itoa(config.ChatID))
513+
}
479514
v.Add("audio", config.FileID)
480515
if config.ReplyToMessageID != 0 {
481516
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
@@ -516,7 +551,11 @@ func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) {
516551

517552
params := make(map[string]string)
518553

519-
params["chat_id"] = strconv.Itoa(config.ChatID)
554+
if config.ChannelUsername != "" {
555+
params["chat_id"] = config.ChannelUsername
556+
} else {
557+
params["chat_id"] = strconv.Itoa(config.ChatID)
558+
}
520559
if config.ReplyToMessageID != 0 {
521560
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
522561
}
@@ -568,7 +607,11 @@ func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) {
568607
func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) {
569608
if config.UseExistingDocument {
570609
v := url.Values{}
571-
v.Add("chat_id", strconv.Itoa(config.ChatID))
610+
if config.ChannelUsername != "" {
611+
v.Add("chat_id", config.ChannelUsername)
612+
} else {
613+
v.Add("chat_id", strconv.Itoa(config.ChatID))
614+
}
572615
v.Add("document", config.FileID)
573616
if config.ReplyToMessageID != 0 {
574617
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
@@ -600,7 +643,11 @@ func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) {
600643

601644
params := make(map[string]string)
602645

603-
params["chat_id"] = strconv.Itoa(config.ChatID)
646+
if config.ChannelUsername != "" {
647+
params["chat_id"] = config.ChannelUsername
648+
} else {
649+
params["chat_id"] = strconv.Itoa(config.ChatID)
650+
}
604651
if config.ReplyToMessageID != 0 {
605652
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
606653
}
@@ -645,7 +692,11 @@ func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) {
645692
func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) {
646693
if config.UseExistingVoice {
647694
v := url.Values{}
648-
v.Add("chat_id", strconv.Itoa(config.ChatID))
695+
if config.ChannelUsername != "" {
696+
v.Add("chat_id", config.ChannelUsername)
697+
} else {
698+
v.Add("chat_id", strconv.Itoa(config.ChatID))
699+
}
649700
v.Add("voice", config.FileID)
650701
if config.ReplyToMessageID != 0 {
651702
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
@@ -680,7 +731,11 @@ func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) {
680731

681732
params := make(map[string]string)
682733

683-
params["chat_id"] = strconv.Itoa(config.ChatID)
734+
if config.ChannelUsername != "" {
735+
params["chat_id"] = config.ChannelUsername
736+
} else {
737+
params["chat_id"] = strconv.Itoa(config.ChatID)
738+
}
684739
if config.ReplyToMessageID != 0 {
685740
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
686741
}
@@ -726,7 +781,11 @@ func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) {
726781
func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) {
727782
if config.UseExistingSticker {
728783
v := url.Values{}
729-
v.Add("chat_id", strconv.Itoa(config.ChatID))
784+
if config.ChannelUsername != "" {
785+
v.Add("chat_id", config.ChannelUsername)
786+
} else {
787+
v.Add("chat_id", strconv.Itoa(config.ChatID))
788+
}
730789
v.Add("sticker", config.FileID)
731790
if config.ReplyToMessageID != 0 {
732791
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
@@ -758,7 +817,11 @@ func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) {
758817

759818
params := make(map[string]string)
760819

761-
params["chat_id"] = strconv.Itoa(config.ChatID)
820+
if config.ChannelUsername != "" {
821+
params["chat_id"] = config.ChannelUsername
822+
} else {
823+
params["chat_id"] = strconv.Itoa(config.ChatID)
824+
}
762825
if config.ReplyToMessageID != 0 {
763826
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
764827
}
@@ -801,7 +864,11 @@ func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) {
801864
func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) {
802865
if config.UseExistingVideo {
803866
v := url.Values{}
804-
v.Add("chat_id", strconv.Itoa(config.ChatID))
867+
if config.ChannelUsername != "" {
868+
v.Add("chat_id", config.ChannelUsername)
869+
} else {
870+
v.Add("chat_id", strconv.Itoa(config.ChatID))
871+
}
805872
v.Add("video", config.FileID)
806873
if config.ReplyToMessageID != 0 {
807874
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
@@ -839,7 +906,11 @@ func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) {
839906

840907
params := make(map[string]string)
841908

842-
params["chat_id"] = strconv.Itoa(config.ChatID)
909+
if config.ChannelUsername != "" {
910+
params["chat_id"] = config.ChannelUsername
911+
} else {
912+
params["chat_id"] = strconv.Itoa(config.ChatID)
913+
}
843914
if config.ReplyToMessageID != 0 {
844915
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
845916
}
@@ -880,7 +951,11 @@ func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) {
880951
// ReplyToMessageID and ReplyMarkup are optional.
881952
func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) {
882953
v := url.Values{}
883-
v.Add("chat_id", strconv.Itoa(config.ChatID))
954+
if config.ChannelUsername != "" {
955+
v.Add("chat_id", config.ChannelUsername)
956+
} else {
957+
v.Add("chat_id", strconv.Itoa(config.ChatID))
958+
}
884959
v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64))
885960
v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64))
886961
if config.ReplyToMessageID != 0 {
@@ -916,7 +991,11 @@ func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) {
916991
// Requires ChatID and a valid Action (see Chat constants).
917992
func (bot *BotAPI) SendChatAction(config ChatActionConfig) error {
918993
v := url.Values{}
919-
v.Add("chat_id", strconv.Itoa(config.ChatID))
994+
if config.ChannelUsername != "" {
995+
v.Add("chat_id", config.ChannelUsername)
996+
} else {
997+
v.Add("chat_id", strconv.Itoa(config.ChatID))
998+
}
920999
v.Add("action", config.Action)
9211000

9221001
_, err := bot.MakeRequest("sendChatAction", v)
@@ -1016,7 +1095,7 @@ func (bot *BotAPI) GetUpdates(config UpdateConfig) ([]Update, error) {
10161095
// SetWebhook sets a webhook.
10171096
// If this is set, GetUpdates will not get any data!
10181097
//
1019-
// Requires Url OR to set Clear to true.
1098+
// Requires URL OR to set Clear to true.
10201099
func (bot *BotAPI) SetWebhook(config WebhookConfig) (APIResponse, error) {
10211100
if config.Certificate == nil {
10221101
v := url.Values{}

0 commit comments

Comments
 (0)