passport.go (view raw)
1package tgbotapi
2
3import (
4 "encoding/json"
5 "fmt"
6 "net/url"
7 "strconv"
8)
9
10// PassportRequestInfoConfig allows you to request passport info
11type PassportRequestInfoConfig struct {
12 BotID int `json:"bot_id"`
13 Scope *PassportScope `json:"scope"`
14 Nonce string `json:"nonce"`
15 PublicKey string `json:"public_key"`
16}
17
18func LinkToPassportRequest(config PassportRequestInfoConfig) (string, error) {
19 scope, err := json.Marshal(config.Scope)
20 if err != nil {
21 panic("couldn't pack scope")
22 }
23 tgurl := fmt.Sprintf("tg://resolve?domain=telegrampassport&bot_id=%v&scope=%v&public_key=%v&nonce=%v",
24 strconv.Itoa(config.BotID),
25 url.PathEscape(string(scope)),
26 url.PathEscape(config.PublicKey),
27 url.PathEscape(config.Nonce),
28 )
29 fmt.Println(tgurl)
30
31 return tgurl, nil
32}
33
34type PassportScopeElement interface {
35 ScopeType() string
36}
37type PassportScope struct {
38 V int `json:"v"`
39 Data []PassportScopeElement `json:"data"`
40}
41
42type PassportScopeElementOneOfSeveral struct {
43}
44
45func (eo *PassportScopeElementOneOfSeveral) ScopeType() string {
46 return "one_of"
47}
48
49type PassportScopeElementOne struct {
50 Type string `json:"type"` // One of “personal_details”, “passport”, “driver_license”, “identity_card”, “internal_passport”, “address”, “utility_bill”, “bank_statement”, “rental_agreement”, “passport_registration”, “temporary_registration”, “phone_number”, “email”
51 Selfie bool `json:"selfie"`
52 Translation bool `json:"translation"`
53 NativeNames bool `json:"native_name"`
54}
55
56func (eo *PassportScopeElementOne) ScopeType() string {
57 return "one"
58}
59
60type (
61 // PassportData contains information about Telegram Passport data shared with
62 // the bot by the user.
63 PassportData struct {
64 // Array with information about documents and other Telegram Passport
65 // elements that was shared with the bot
66 Data []EncryptedPassportElement `json:"data"`
67
68 // Encrypted credentials required to decrypt the data
69 Credentials *EncryptedCredentials `json:"credentials"`
70 }
71
72 // PassportFile represents a file uploaded to Telegram Passport. Currently all
73 // Telegram Passport files are in JPEG format when decrypted and don't exceed
74 // 10MB.
75 PassportFile struct {
76 // Unique identifier for this file
77 FileID string `json:"file_id"`
78
79 // File size
80 FileSize int `json:"file_size"`
81
82 // Unix time when the file was uploaded
83 FileDate int64 `json:"file_date"`
84 }
85
86 // EncryptedPassportElement contains information about documents or other
87 // Telegram Passport elements shared with the bot by the user.
88 EncryptedPassportElement struct {
89 // Element type.
90 Type string `json:"type"`
91
92 // Base64-encoded encrypted Telegram Passport element data provided by
93 // the user, available for "personal_details", "passport",
94 // "driver_license", "identity_card", "identity_passport" and "address"
95 // types. Can be decrypted and verified using the accompanying
96 // EncryptedCredentials.
97 Data string `json:"data,omitempty"`
98
99 // User's verified phone number, available only for "phone_number" type
100 PhoneNumber string `json:"phone_number,omitempty"`
101
102 // User's verified email address, available only for "email" type
103 Email string `json:"email,omitempty"`
104
105 // Array of encrypted files with documents provided by the user,
106 // available for "utility_bill", "bank_statement", "rental_agreement",
107 // "passport_registration" and "temporary_registration" types. Files can
108 // be decrypted and verified using the accompanying EncryptedCredentials.
109 Files []PassportFile `json:"files,omitempty"`
110
111 // Encrypted file with the front side of the document, provided by the
112 // user. Available for "passport", "driver_license", "identity_card" and
113 // "internal_passport". The file can be decrypted and verified using the
114 // accompanying EncryptedCredentials.
115 FrontSide *PassportFile `json:"front_side,omitempty"`
116
117 // Encrypted file with the reverse side of the document, provided by the
118 // user. Available for "driver_license" and "identity_card". The file can
119 // be decrypted and verified using the accompanying EncryptedCredentials.
120 ReverseSide *PassportFile `json:"reverse_side,omitempty"`
121
122 // Encrypted file with the selfie of the user holding a document,
123 // provided by the user; available for "passport", "driver_license",
124 // "identity_card" and "internal_passport". The file can be decrypted
125 // and verified using the accompanying EncryptedCredentials.
126 Selfie *PassportFile `json:"selfie,omitempty"`
127 }
128
129 // EncryptedCredentials contains data required for decrypting and
130 // authenticating EncryptedPassportElement. See the Telegram Passport
131 // Documentation for a complete description of the data decryption and
132 // authentication processes.
133 EncryptedCredentials struct {
134 // Base64-encoded encrypted JSON-serialized data with unique user's
135 // payload, data hashes and secrets required for EncryptedPassportElement
136 // decryption and authentication
137 Data string `json:"data"`
138
139 // Base64-encoded data hash for data authentication
140 Hash string `json:"hash"`
141
142 // Base64-encoded secret, encrypted with the bot's public RSA key,
143 // required for data decryption
144 Secret string `json:"secret"`
145 }
146
147 // PassportElementError represents an error in the Telegram Passport element
148 // which was submitted that should be resolved by the user.
149 PassportElementError interface{}
150
151 // PassportElementErrorDataField represents an issue in one of the data
152 // fields that was provided by the user. The error is considered resolved
153 // when the field's value changes.
154 PassportElementErrorDataField struct {
155 // Error source, must be data
156 Source string `json:"source"`
157
158 // The section of the user's Telegram Passport which has the error, one
159 // of "personal_details", "passport", "driver_license", "identity_card",
160 // "internal_passport", "address"
161 Type string `json:"type"`
162
163 // Name of the data field which has the error
164 FieldName string `json:"field_name"`
165
166 // Base64-encoded data hash
167 DataHash string `json:"data_hash"`
168
169 // Error message
170 Message string `json:"message"`
171 }
172 // PassportElementErrorFrontSide represents an issue with the front side of
173 // a document. The error is considered resolved when the file with the front
174 // side of the document changes.
175 PassportElementErrorFrontSide struct {
176 // Error source, must be front_side
177 Source string `json:"source"`
178
179 // The section of the user's Telegram Passport which has the issue, one
180 // of "passport", "driver_license", "identity_card", "internal_passport"
181 Type string `json:"type"`
182
183 // Base64-encoded hash of the file with the front side of the document
184 FileHash string `json:"file_hash"`
185
186 // Error message
187 Message string `json:"message"`
188 }
189
190 // PassportElementErrorReverseSide represents an issue with the reverse side
191 // of a document. The error is considered resolved when the file with reverse
192 // side of the document changes.
193 PassportElementErrorReverseSide struct {
194 // Error source, must be reverse_side
195 Source string `json:"source"`
196
197 // The section of the user's Telegram Passport which has the issue, one
198 // of "driver_license", "identity_card"
199 Type string `json:"type"`
200
201 // Base64-encoded hash of the file with the reverse side of the document
202 FileHash string `json:"file_hash"`
203
204 // Error message
205 Message string `json:"message"`
206 }
207
208 // PassportElementErrorSelfie represents an issue with the selfie with a
209 // document. The error is considered resolved when the file with the selfie
210 // changes.
211 PassportElementErrorSelfie struct {
212 // Error source, must be selfie
213 Source string `json:"source"`
214
215 // The section of the user's Telegram Passport which has the issue, one
216 // of "passport", "driver_license", "identity_card", "internal_passport"
217 Type string `json:"type"`
218
219 // Base64-encoded hash of the file with the selfie
220 FileHash string `json:"file_hash"`
221
222 // Error message
223 Message string `json:"message"`
224 }
225
226 // PassportElementErrorFile represents an issue with a document scan. The
227 // error is considered resolved when the file with the document scan changes.
228 PassportElementErrorFile struct {
229 // Error source, must be file
230 Source string `json:"source"`
231
232 // The section of the user's Telegram Passport which has the issue, one
233 // of "utility_bill", "bank_statement", "rental_agreement",
234 // "passport_registration", "temporary_registration"
235 Type string `json:"type"`
236
237 // Base64-encoded file hash
238 FileHash string `json:"file_hash"`
239
240 // Error message
241 Message string `json:"message"`
242 }
243
244 // PassportElementErrorFiles represents an issue with a list of scans. The
245 // error is considered resolved when the list of files containing the scans
246 // changes.
247 PassportElementErrorFiles struct {
248 // Error source, must be files
249 Source string `json:"source"`
250
251 // The section of the user's Telegram Passport which has the issue, one
252 // of "utility_bill", "bank_statement", "rental_agreement",
253 // "passport_registration", "temporary_registration"
254 Type string `json:"type"`
255
256 // List of base64-encoded file hashes
257 FileHashes []string `json:"file_hashes"`
258
259 // Error message
260 Message string `json:"message"`
261 }
262
263 Credentials struct {
264 Data SecureData `json:"secure_data"`
265 // Nonce the same nonce given in the request
266 Nonce string `json:"nonce"`
267 }
268
269 SecureData map[string]*SecureValue
270 // PersonalDetails *SecureValue `json:"personal_details"`
271 // Passport *SecureValue `json:"passport"`
272 // InternalPassport *SecureValue `json:"internal_passport"`
273 // DriverLicense *SecureValue `json:"driver_license"`
274 // IdentityCard *SecureValue `json:"identity_card"`
275 // Address *SecureValue `json:"address"`
276 // UtilityBill *SecureValue `json:"utility_bill"`
277 // BankStatement *SecureValue `json:"bank_statement"`
278 // RentalAgreement *SecureValue `json:"rental_agreement"`
279 // PassportRegistration *SecureValue `json:"passport_registration"`
280 // TemporaryRegistration *SecureValue `json:"temporary_registration"`
281
282 SecureValue struct {
283 Data *DataCredentials `json:"data"`
284 FrontSide *FileCredentials `json:"front_side"`
285 ReverseSide *FileCredentials `json:"reverse_side"`
286 Selfie *FileCredentials `json:"selfie"`
287 Translation []*FileCredentials `json:"translation"`
288 Files []*FileCredentials `json:"files"`
289 }
290 DataCredentials struct {
291 // DataHash checksum of encrypted data
292 DataHash string `json:"data_hash"`
293 // Secret of encrypted data
294 Secret string `json:"secret"`
295 }
296
297 FileCredentials struct {
298 // FileHash checksum of encrypted data
299 FileHash string `json:"file_hash"`
300 // Secret of encrypted data
301 Secret string `json:"secret"`
302 }
303 // PersonalDetails https://core.telegram.org/passport#personaldetails
304 PersonalDetails struct {
305 FirstName string `json:"first_name"`
306 LastName string `json:"last_name"`
307 MiddleName string `json:"middle_name"`
308 BirthDate string `json:"birth_date"`
309 Gender string `json:"gender"`
310 CountryCode string `json:"country_code"`
311 ResidenceCountryCode string `json:"residence_country_code"`
312 FirstNameNative string `json:"first_name_native"`
313 LastNameNative string `json:"last_name_native"`
314 MiddleNameNative string `json:"middle_name_native"`
315 }
316
317 // IdDocumentData https://core.telegram.org/passport#iddocumentdata
318 IDDocumentData struct {
319 DocumentNumber string `json:"document_no"`
320 ExpiryDate string `json:"expiry_date"`
321 }
322)