all repos — telegram-bot-api @ 4610c561c6467a0884374769b29f907ec7e8d961

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// Perhaps set a ChatAction of ChatTyping while processing.
  9//
 10// chatID is where to send it, text is the message text.
 11func NewMessage(chatID int, text string) MessageConfig {
 12	return MessageConfig{
 13		Chattable: Chattable{ChatID: chatID},
 14		Text:      text,
 15		DisableWebPagePreview: false,
 16		ReplyToMessageID:      0,
 17	}
 18}
 19
 20// NewForward creates a new forward.
 21//
 22// chatID is where to send it, fromChatID is the source chat,
 23// and messageID is the ID of the original message.
 24func NewForward(chatID int, fromChatID int, messageID int) ForwardConfig {
 25	return ForwardConfig{
 26		Chattable:  Chattable{ChatID: chatID},
 27		FromChatID: fromChatID,
 28		MessageID:  messageID,
 29	}
 30}
 31
 32// NewPhotoUpload creates a new photo uploader.
 33// This requires a file on the local filesystem to upload to Telegram.
 34// Perhaps set a ChatAction of ChatUploadPhoto while processing.
 35//
 36// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
 37func NewPhotoUpload(chatID int, file interface{}) PhotoConfig {
 38	return PhotoConfig{
 39		Chattable:        Chattable{ChatID: chatID},
 40		UseExistingPhoto: false,
 41		File:             file,
 42	}
 43}
 44
 45// NewPhotoShare shares an existing photo.
 46// You may use this to reshare an existing photo without reuploading it.
 47//
 48// chatID is where to send it, fileID is the ID of the file already uploaded.
 49func NewPhotoShare(chatID int, fileID string) PhotoConfig {
 50	return PhotoConfig{
 51		Chattable:        Chattable{ChatID: chatID},
 52		UseExistingPhoto: true,
 53		FileID:           fileID,
 54	}
 55}
 56
 57// NewAudioUpload creates a new audio uploader.
 58// This requires a file on the local filesystem to upload to Telegram.
 59// Perhaps set a ChatAction of ChatRecordAudio or ChatUploadAudio while processing.
 60//
 61// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
 62func NewAudioUpload(chatID int, file interface{}) AudioConfig {
 63	return AudioConfig{
 64		Chattable:        Chattable{ChatID: chatID},
 65		UseExistingAudio: false,
 66		File:             file,
 67	}
 68}
 69
 70// NewAudioShare shares an existing audio file.
 71// You may use this to reshare an existing audio file without reuploading it.
 72//
 73// chatID is where to send it, fileID is the ID of the audio already uploaded.
 74func NewAudioShare(chatID int, fileID string) AudioConfig {
 75	return AudioConfig{
 76		Chattable:        Chattable{ChatID: chatID},
 77		UseExistingAudio: true,
 78		FileID:           fileID,
 79	}
 80}
 81
 82// NewDocumentUpload creates a new document uploader.
 83// This requires a file on the local filesystem to upload to Telegram.
 84// Perhaps set a ChatAction of ChatUploadDocument while processing.
 85//
 86// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
 87func NewDocumentUpload(chatID int, file interface{}) DocumentConfig {
 88	return DocumentConfig{
 89		Chattable:           Chattable{ChatID: chatID},
 90		UseExistingDocument: false,
 91		File:                file,
 92	}
 93}
 94
 95// NewDocumentShare shares an existing document.
 96// You may use this to reshare an existing document without reuploading it.
 97//
 98// chatID is where to send it, fileID is the ID of the document already uploaded.
 99func NewDocumentShare(chatID int, fileID string) DocumentConfig {
100	return DocumentConfig{
101		Chattable:           Chattable{ChatID: chatID},
102		UseExistingDocument: true,
103		FileID:              fileID,
104	}
105}
106
107// NewStickerUpload creates a new sticker uploader.
108// This requires a file on the local filesystem to upload to Telegram.
109//
110// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
111func NewStickerUpload(chatID int, file interface{}) StickerConfig {
112	return StickerConfig{
113		Chattable:          Chattable{ChatID: chatID},
114		UseExistingSticker: false,
115		File:               file,
116	}
117}
118
119// NewStickerShare shares an existing sticker.
120// You may use this to reshare an existing sticker without reuploading it.
121//
122// chatID is where to send it, fileID is the ID of the sticker already uploaded.
123func NewStickerShare(chatID int, fileID string) StickerConfig {
124	return StickerConfig{
125		Chattable:          Chattable{ChatID: chatID},
126		UseExistingSticker: true,
127		FileID:             fileID,
128	}
129}
130
131// NewVideoUpload creates a new video uploader.
132// This requires a file on the local filesystem to upload to Telegram.
133// Perhaps set a ChatAction of ChatRecordVideo or ChatUploadVideo while processing.
134//
135// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
136func NewVideoUpload(chatID int, file interface{}) VideoConfig {
137	return VideoConfig{
138		Chattable:        Chattable{ChatID: chatID},
139		UseExistingVideo: false,
140		File:             file,
141	}
142}
143
144// NewVideoShare shares an existing video.
145// You may use this to reshare an existing video without reuploading it.
146//
147// chatID is where to send it, fileID is the ID of the video already uploaded.
148func NewVideoShare(chatID int, fileID string) VideoConfig {
149	return VideoConfig{
150		Chattable:        Chattable{ChatID: chatID},
151		UseExistingVideo: true,
152		FileID:           fileID,
153	}
154}
155
156// NewVoiceUpload creates a new voice uploader.
157// This requires a file on the local filesystem to upload to Telegram.
158// Perhaps set a ChatAction of ChatRecordVideo or ChatUploadVideo while processing.
159//
160// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
161func NewVoiceUpload(chatID int, file interface{}) VoiceConfig {
162	return VoiceConfig{
163		Chattable:        Chattable{ChatID: chatID},
164		UseExistingVoice: false,
165		File:             file,
166	}
167}
168
169// NewVoiceShare shares an existing voice.
170// You may use this to reshare an existing voice without reuploading it.
171//
172// chatID is where to send it, fileID is the ID of the video already uploaded.
173func NewVoiceShare(chatID int, fileID string) VoiceConfig {
174	return VoiceConfig{
175		Chattable:        Chattable{ChatID: chatID},
176		UseExistingVoice: true,
177		FileID:           fileID,
178	}
179}
180
181// NewLocation shares your location.
182// Perhaps set a ChatAction of ChatFindLocation while processing.
183//
184// chatID is where to send it, latitude and longitude are coordinates.
185func NewLocation(chatID int, latitude float64, longitude float64) LocationConfig {
186	return LocationConfig{
187		Chattable:        Chattable{ChatID: chatID},
188		Latitude:         latitude,
189		Longitude:        longitude,
190		ReplyToMessageID: 0,
191		ReplyMarkup:      nil,
192	}
193}
194
195// NewChatAction sets a chat action.
196// Actions last for 5 seconds, or until your next action.
197//
198// chatID is where to send it, action should be set via CHAT constants.
199func NewChatAction(chatID int, action string) ChatActionConfig {
200	return ChatActionConfig{
201		Chattable: Chattable{ChatID: chatID},
202		Action:    action,
203	}
204}
205
206// NewUserProfilePhotos gets user profile photos.
207//
208// userID is the ID of the user you wish to get profile photos from.
209func NewUserProfilePhotos(userID int) UserProfilePhotosConfig {
210	return UserProfilePhotosConfig{
211		UserID: userID,
212		Offset: 0,
213		Limit:  0,
214	}
215}
216
217// NewUpdate gets updates since the last Offset.
218//
219// offset is the last Update ID to include.
220// You likely want to set this to the last Update ID plus 1.
221func NewUpdate(offset int) UpdateConfig {
222	return UpdateConfig{
223		Offset:  offset,
224		Limit:   0,
225		Timeout: 0,
226	}
227}
228
229// NewWebhook creates a new webhook.
230//
231// link is the url parsable link you wish to get the updates.
232func NewWebhook(link string) WebhookConfig {
233	u, _ := url.Parse(link)
234
235	return WebhookConfig{
236		URL:   u,
237		Clear: false,
238	}
239}
240
241// NewWebhookWithCert creates a new webhook with a certificate.
242//
243// link is the url you wish to get webhooks,
244// file contains a string to a file, or a FileReader or FileBytes.
245func NewWebhookWithCert(link string, file interface{}) WebhookConfig {
246	u, _ := url.Parse(link)
247
248	return WebhookConfig{
249		URL:         u,
250		Clear:       false,
251		Certificate: file,
252	}
253}