all repos — telegram-bot-api @ c0eb5db6c3f5289bcbb06bdd0e68ef5ea6216371

Golang bindings for the Telegram Bot API

helpers.go (view raw)

  1package tgbotapi
  2
  3import (
  4	"net/url"
  5)
  6
  7// NewMessage creates a new Message.
  8//
  9// chatID is where to send it, text is the message text.
 10func NewMessage(chatID int64, text string) MessageConfig {
 11	return MessageConfig{
 12		BaseChat: BaseChat{
 13			ChatID:           chatID,
 14			ReplyToMessageID: 0,
 15		},
 16		Text: text,
 17		DisableWebPagePreview: false,
 18	}
 19}
 20
 21// NewForward creates a new forward.
 22//
 23// chatID is where to send it, fromChatID is the source chat,
 24// and messageID is the ID of the original message.
 25func NewForward(chatID int64, fromChatID int64, messageID int) ForwardConfig {
 26	return ForwardConfig{
 27		BaseChat:   BaseChat{ChatID: chatID},
 28		FromChatID: fromChatID,
 29		MessageID:  messageID,
 30	}
 31}
 32
 33// NewPhotoUpload creates a new photo uploader.
 34//
 35// chatID is where to send it, file is a string path to the file,
 36// FileReader, or FileBytes.
 37//
 38// Note that you must send animated GIFs as a document.
 39func NewPhotoUpload(chatID int64, file interface{}) PhotoConfig {
 40	return PhotoConfig{
 41		BaseFile: BaseFile{
 42			BaseChat:    BaseChat{ChatID: chatID},
 43			File:        file,
 44			UseExisting: false,
 45		},
 46	}
 47}
 48
 49// NewPhotoShare shares an existing photo.
 50// You may use this to reshare an existing photo without reuploading it.
 51//
 52// chatID is where to send it, fileID is the ID of the file
 53// already uploaded.
 54func NewPhotoShare(chatID int64, fileID string) PhotoConfig {
 55	return PhotoConfig{
 56		BaseFile: BaseFile{
 57			BaseChat:    BaseChat{ChatID: chatID},
 58			FileID:      fileID,
 59			UseExisting: true,
 60		},
 61	}
 62}
 63
 64// NewAudioUpload creates a new audio uploader.
 65//
 66// chatID is where to send it, file is a string path to the file,
 67// FileReader, or FileBytes.
 68func NewAudioUpload(chatID int64, file interface{}) AudioConfig {
 69	return AudioConfig{
 70		BaseFile: BaseFile{
 71			BaseChat:    BaseChat{ChatID: chatID},
 72			File:        file,
 73			UseExisting: false,
 74		},
 75	}
 76}
 77
 78// NewAudioShare shares an existing audio file.
 79// You may use this to reshare an existing audio file without
 80// reuploading it.
 81//
 82// chatID is where to send it, fileID is the ID of the audio
 83// already uploaded.
 84func NewAudioShare(chatID int64, fileID string) AudioConfig {
 85	return AudioConfig{
 86		BaseFile: BaseFile{
 87			BaseChat:    BaseChat{ChatID: chatID},
 88			FileID:      fileID,
 89			UseExisting: true,
 90		},
 91	}
 92}
 93
 94// NewDocumentUpload creates a new document uploader.
 95//
 96// chatID is where to send it, file is a string path to the file,
 97// FileReader, or FileBytes.
 98func NewDocumentUpload(chatID int64, file interface{}) DocumentConfig {
 99	return DocumentConfig{
100		BaseFile: BaseFile{
101			BaseChat:    BaseChat{ChatID: chatID},
102			File:        file,
103			UseExisting: false,
104		},
105	}
106}
107
108// NewDocumentShare shares an existing document.
109// You may use this to reshare an existing document without
110// reuploading it.
111//
112// chatID is where to send it, fileID is the ID of the document
113// already uploaded.
114func NewDocumentShare(chatID int64, fileID string) DocumentConfig {
115	return DocumentConfig{
116		BaseFile: BaseFile{
117			BaseChat:    BaseChat{ChatID: chatID},
118			FileID:      fileID,
119			UseExisting: true,
120		},
121	}
122}
123
124// NewStickerUpload creates a new sticker uploader.
125//
126// chatID is where to send it, file is a string path to the file,
127// FileReader, or FileBytes.
128func NewStickerUpload(chatID int64, file interface{}) StickerConfig {
129	return StickerConfig{
130		BaseFile: BaseFile{
131			BaseChat:    BaseChat{ChatID: chatID},
132			File:        file,
133			UseExisting: false,
134		},
135	}
136}
137
138// NewStickerShare shares an existing sticker.
139// You may use this to reshare an existing sticker without
140// reuploading it.
141//
142// chatID is where to send it, fileID is the ID of the sticker
143// already uploaded.
144func NewStickerShare(chatID int64, fileID string) StickerConfig {
145	return StickerConfig{
146		BaseFile: BaseFile{
147			BaseChat:    BaseChat{ChatID: chatID},
148			FileID:      fileID,
149			UseExisting: true,
150		},
151	}
152}
153
154// NewVideoUpload creates a new video uploader.
155//
156// chatID is where to send it, file is a string path to the file,
157// FileReader, or FileBytes.
158func NewVideoUpload(chatID int64, file interface{}) VideoConfig {
159	return VideoConfig{
160		BaseFile: BaseFile{
161			BaseChat:    BaseChat{ChatID: chatID},
162			File:        file,
163			UseExisting: false,
164		},
165	}
166}
167
168// NewVideoShare shares an existing video.
169// You may use this to reshare an existing video without reuploading it.
170//
171// chatID is where to send it, fileID is the ID of the video
172// already uploaded.
173func NewVideoShare(chatID int64, fileID string) VideoConfig {
174	return VideoConfig{
175		BaseFile: BaseFile{
176			BaseChat:    BaseChat{ChatID: chatID},
177			FileID:      fileID,
178			UseExisting: true,
179		},
180	}
181}
182
183// NewVoiceUpload creates a new voice uploader.
184//
185// chatID is where to send it, file is a string path to the file,
186// FileReader, or FileBytes.
187func NewVoiceUpload(chatID int64, file interface{}) VoiceConfig {
188	return VoiceConfig{
189		BaseFile: BaseFile{
190			BaseChat:    BaseChat{ChatID: chatID},
191			File:        file,
192			UseExisting: false,
193		},
194	}
195}
196
197// NewVoiceShare shares an existing voice.
198// You may use this to reshare an existing voice without reuploading it.
199//
200// chatID is where to send it, fileID is the ID of the video
201// already uploaded.
202func NewVoiceShare(chatID int64, fileID string) VoiceConfig {
203	return VoiceConfig{
204		BaseFile: BaseFile{
205			BaseChat:    BaseChat{ChatID: chatID},
206			FileID:      fileID,
207			UseExisting: true,
208		},
209	}
210}
211
212// NewContact allows you to send a shared contact.
213func NewContact(chatID int64, phoneNumber, firstName string) ContactConfig {
214	return ContactConfig{
215		BaseChat: BaseChat{
216			ChatID: chatID,
217		},
218		PhoneNumber: phoneNumber,
219		FirstName:   firstName,
220	}
221}
222
223// NewLocation shares your location.
224//
225// chatID is where to send it, latitude and longitude are coordinates.
226func NewLocation(chatID int64, latitude float64, longitude float64) LocationConfig {
227	return LocationConfig{
228		BaseChat: BaseChat{
229			ChatID: chatID,
230		},
231		Latitude:  latitude,
232		Longitude: longitude,
233	}
234}
235
236// NewVenue allows you to send a venue and its location.
237func NewVenue(chatID int64, title, address string, latitude, longitude float64) VenueConfig {
238	return VenueConfig{
239		BaseChat: BaseChat{
240			ChatID: chatID,
241		},
242		Title:     title,
243		Address:   address,
244		Latitude:  latitude,
245		Longitude: longitude,
246	}
247}
248
249// NewChatAction sets a chat action.
250// Actions last for 5 seconds, or until your next action.
251//
252// chatID is where to send it, action should be set via Chat constants.
253func NewChatAction(chatID int64, action string) ChatActionConfig {
254	return ChatActionConfig{
255		BaseChat: BaseChat{ChatID: chatID},
256		Action:   action,
257	}
258}
259
260// NewUserProfilePhotos gets user profile photos.
261//
262// userID is the ID of the user you wish to get profile photos from.
263func NewUserProfilePhotos(userID int) UserProfilePhotosConfig {
264	return UserProfilePhotosConfig{
265		UserID: userID,
266		Offset: 0,
267		Limit:  0,
268	}
269}
270
271// NewUpdate gets updates since the last Offset.
272//
273// offset is the last Update ID to include.
274// You likely want to set this to the last Update ID plus 1.
275func NewUpdate(offset int) UpdateConfig {
276	return UpdateConfig{
277		Offset:  offset,
278		Limit:   0,
279		Timeout: 0,
280	}
281}
282
283// NewWebhook creates a new webhook.
284//
285// link is the url parsable link you wish to get the updates.
286func NewWebhook(link string) WebhookConfig {
287	u, _ := url.Parse(link)
288
289	return WebhookConfig{
290		URL: u,
291	}
292}
293
294// NewWebhookWithCert creates a new webhook with a certificate.
295//
296// link is the url you wish to get webhooks,
297// file contains a string to a file, FileReader, or FileBytes.
298func NewWebhookWithCert(link string, file interface{}) WebhookConfig {
299	u, _ := url.Parse(link)
300
301	return WebhookConfig{
302		URL:         u,
303		Certificate: file,
304	}
305}
306
307// NewInlineQueryResultArticle creates a new inline query article.
308func NewInlineQueryResultArticle(id, title, messageText string) InlineQueryResultArticle {
309	return InlineQueryResultArticle{
310		Type:  "article",
311		ID:    id,
312		Title: title,
313		InputMessageContent: InputTextMessageContent{
314			Text: messageText,
315		},
316	}
317}
318
319// NewInlineQueryResultGIF creates a new inline query GIF.
320func NewInlineQueryResultGIF(id, url string) InlineQueryResultGIF {
321	return InlineQueryResultGIF{
322		Type: "gif",
323		ID:   id,
324		URL:  url,
325	}
326}
327
328// NewInlineQueryResultMPEG4GIF creates a new inline query MPEG4 GIF.
329func NewInlineQueryResultMPEG4GIF(id, url string) InlineQueryResultMPEG4GIF {
330	return InlineQueryResultMPEG4GIF{
331		Type: "mpeg4_gif",
332		ID:   id,
333		URL:  url,
334	}
335}
336
337// NewInlineQueryResultPhoto creates a new inline query photo.
338func NewInlineQueryResultPhoto(id, url string) InlineQueryResultPhoto {
339	return InlineQueryResultPhoto{
340		Type: "photo",
341		ID:   id,
342		URL:  url,
343	}
344}
345
346// NewInlineQueryResultVideo creates a new inline query video.
347func NewInlineQueryResultVideo(id, url string) InlineQueryResultVideo {
348	return InlineQueryResultVideo{
349		Type: "video",
350		ID:   id,
351		URL:  url,
352	}
353}
354
355// NewEditMessageText allows you to edit the text of a message.
356func NewEditMessageText(chatID int64, messageID int, text string) EditMessageTextConfig {
357	return EditMessageTextConfig{
358		BaseEdit: BaseEdit{
359			ChatID:    chatID,
360			MessageID: messageID,
361		},
362		Text: text,
363	}
364}
365
366// NewEditMessageCaption allows you to edit the caption of a message.
367func NewEditMessageCaption(chatID int64, messageID int, caption string) EditMessageCaptionConfig {
368	return EditMessageCaptionConfig{
369		BaseEdit: BaseEdit{
370			ChatID:    chatID,
371			MessageID: messageID,
372		},
373		Caption: caption,
374	}
375}
376
377// NewEditMessageReplyMarkup allows you to edit the inline
378// keyboard markup.
379func NewEditMessageReplyMarkup(chatID int64, messageID int, replyMarkup InlineKeyboardMarkup) EditMessageReplyMarkupConfig {
380	return EditMessageReplyMarkupConfig{
381		BaseEdit: BaseEdit{
382			ChatID:    chatID,
383			MessageID: messageID,
384		},
385		ReplyMarkup: &replyMarkup,
386	}
387}