all repos — telegram-bot-api @ da026b435e78db6e13663d1af658db812e868cdc

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