all repos — telegram-bot-api @ d40f7f97034c8473b3fabe6e3034512fa1d655c9

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