all repos — telegram-bot-api @ 10c5261d919e9311450a5ddb950c759b31f8ec0d

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 int, 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 int, fromChatID int, 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.
 37func NewPhotoUpload(chatID int, file interface{}) PhotoConfig {
 38	return PhotoConfig{
 39		BaseFile: BaseFile{
 40			BaseChat:    BaseChat{ChatID: chatID},
 41			File:        file,
 42			UseExisting: false,
 43		},
 44	}
 45}
 46
 47// NewPhotoShare shares an existing photo.
 48// You may use this to reshare an existing photo without reuploading it.
 49//
 50// chatID is where to send it, fileID is the ID of the file
 51// already uploaded.
 52func NewPhotoShare(chatID int, fileID string) PhotoConfig {
 53	return PhotoConfig{
 54		BaseFile: BaseFile{
 55			BaseChat:    BaseChat{ChatID: chatID},
 56			FileID:      fileID,
 57			UseExisting: true,
 58		},
 59	}
 60}
 61
 62// NewAudioUpload creates a new audio uploader.
 63//
 64// chatID is where to send it, file is a string path to the file,
 65// FileReader, or FileBytes.
 66func NewAudioUpload(chatID int, file interface{}) AudioConfig {
 67	return AudioConfig{
 68		BaseFile: BaseFile{
 69			BaseChat:    BaseChat{ChatID: chatID},
 70			File:        file,
 71			UseExisting: false,
 72		},
 73	}
 74}
 75
 76// NewAudioShare shares an existing audio file.
 77// You may use this to reshare an existing audio file without
 78// reuploading it.
 79//
 80// chatID is where to send it, fileID is the ID of the audio
 81// already uploaded.
 82func NewAudioShare(chatID int, fileID string) AudioConfig {
 83	return AudioConfig{
 84		BaseFile: BaseFile{
 85			BaseChat:    BaseChat{ChatID: chatID},
 86			FileID:      fileID,
 87			UseExisting: true,
 88		},
 89	}
 90}
 91
 92// NewDocumentUpload creates a new document uploader.
 93//
 94// chatID is where to send it, file is a string path to the file,
 95// FileReader, or FileBytes.
 96func NewDocumentUpload(chatID int, file interface{}) DocumentConfig {
 97	return DocumentConfig{
 98		BaseFile: BaseFile{
 99			BaseChat:    BaseChat{ChatID: chatID},
100			File:        file,
101			UseExisting: false,
102		},
103	}
104}
105
106// NewDocumentShare shares an existing document.
107// You may use this to reshare an existing document without
108// reuploading it.
109//
110// chatID is where to send it, fileID is the ID of the document
111// already uploaded.
112func NewDocumentShare(chatID int, fileID string) DocumentConfig {
113	return DocumentConfig{
114		BaseFile: BaseFile{
115			BaseChat:    BaseChat{ChatID: chatID},
116			FileID:      fileID,
117			UseExisting: true,
118		},
119	}
120}
121
122// NewStickerUpload creates a new sticker uploader.
123//
124// chatID is where to send it, file is a string path to the file,
125// FileReader, or FileBytes.
126func NewStickerUpload(chatID int, file interface{}) StickerConfig {
127	return StickerConfig{
128		BaseFile: BaseFile{
129			BaseChat:    BaseChat{ChatID: chatID},
130			File:        file,
131			UseExisting: false,
132		},
133	}
134}
135
136// NewStickerShare shares an existing sticker.
137// You may use this to reshare an existing sticker without
138// reuploading it.
139//
140// chatID is where to send it, fileID is the ID of the sticker
141// already uploaded.
142func NewStickerShare(chatID int, fileID string) StickerConfig {
143	return StickerConfig{
144		BaseFile: BaseFile{
145			BaseChat:    BaseChat{ChatID: chatID},
146			FileID:      fileID,
147			UseExisting: true,
148		},
149	}
150}
151
152// NewVideoUpload creates a new video uploader.
153//
154// chatID is where to send it, file is a string path to the file,
155// FileReader, or FileBytes.
156func NewVideoUpload(chatID int, file interface{}) VideoConfig {
157	return VideoConfig{
158		BaseFile: BaseFile{
159			BaseChat:    BaseChat{ChatID: chatID},
160			File:        file,
161			UseExisting: false,
162		},
163	}
164}
165
166// NewVideoShare shares an existing video.
167// You may use this to reshare an existing video without reuploading it.
168//
169// chatID is where to send it, fileID is the ID of the video
170// already uploaded.
171func NewVideoShare(chatID int, fileID string) VideoConfig {
172	return VideoConfig{
173		BaseFile: BaseFile{
174			BaseChat:    BaseChat{ChatID: chatID},
175			FileID:      fileID,
176			UseExisting: true,
177		},
178	}
179}
180
181// NewVoiceUpload creates a new voice uploader.
182//
183// chatID is where to send it, file is a string path to the file,
184// FileReader, or FileBytes.
185func NewVoiceUpload(chatID int, file interface{}) VoiceConfig {
186	return VoiceConfig{
187		BaseFile: BaseFile{
188			BaseChat:    BaseChat{ChatID: chatID},
189			File:        file,
190			UseExisting: false,
191		},
192	}
193}
194
195// NewVoiceShare shares an existing voice.
196// You may use this to reshare an existing voice without reuploading it.
197//
198// chatID is where to send it, fileID is the ID of the video
199// already uploaded.
200func NewVoiceShare(chatID int, fileID string) VoiceConfig {
201	return VoiceConfig{
202		BaseFile: BaseFile{
203			BaseChat:    BaseChat{ChatID: chatID},
204			FileID:      fileID,
205			UseExisting: true,
206		},
207	}
208}
209
210// NewLocation shares your location.
211//
212// chatID is where to send it, latitude and longitude are coordinates.
213func NewLocation(chatID int, latitude float64, longitude float64) LocationConfig {
214	return LocationConfig{
215		BaseChat: BaseChat{
216			ChatID:           chatID,
217			ReplyToMessageID: 0,
218			ReplyMarkup:      nil,
219		},
220		Latitude:  latitude,
221		Longitude: longitude,
222	}
223}
224
225// NewChatAction sets a chat action.
226// Actions last for 5 seconds, or until your next action.
227//
228// chatID is where to send it, action should be set via Chat constants.
229func NewChatAction(chatID int, action string) ChatActionConfig {
230	return ChatActionConfig{
231		BaseChat: BaseChat{ChatID: chatID},
232		Action:   action,
233	}
234}
235
236// NewUserProfilePhotos gets user profile photos.
237//
238// userID is the ID of the user you wish to get profile photos from.
239func NewUserProfilePhotos(userID int) UserProfilePhotosConfig {
240	return UserProfilePhotosConfig{
241		UserID: userID,
242		Offset: 0,
243		Limit:  0,
244	}
245}
246
247// NewUpdate gets updates since the last Offset.
248//
249// offset is the last Update ID to include.
250// You likely want to set this to the last Update ID plus 1.
251func NewUpdate(offset int) UpdateConfig {
252	return UpdateConfig{
253		Offset:  offset,
254		Limit:   0,
255		Timeout: 0,
256	}
257}
258
259// NewWebhook creates a new webhook.
260//
261// link is the url parsable link you wish to get the updates.
262func NewWebhook(link string) WebhookConfig {
263	u, _ := url.Parse(link)
264
265	return WebhookConfig{
266		URL: u,
267	}
268}
269
270// NewWebhookWithCert creates a new webhook with a certificate.
271//
272// link is the url you wish to get webhooks,
273// file contains a string to a file, FileReader, or FileBytes.
274func NewWebhookWithCert(link string, file interface{}) WebhookConfig {
275	u, _ := url.Parse(link)
276
277	return WebhookConfig{
278		URL:         u,
279		Certificate: file,
280	}
281}