1#!/usr/bin/env python
2# pylint: disable=unused-argument, wrong-import-position
3# This program is dedicated to the public domain under the CC0 license.
4
5"""
6Simple Bot to print/download all incoming passport data
7
8See https://telegram.org/blog/passport for info about what telegram passport is.
9
10See https://github.com/python-telegram-bot/python-telegram-bot/wiki/Telegram-Passport
11 for how to use Telegram Passport properly with python-telegram-bot.
12
13Note:
14To use Telegram Passport, you must install PTB via
15`pip install python-telegram-bot[passport]`
16"""
17import logging
18from pathlib import Path
19
20from telegram import __version__ as TG_VER
21
22try:
23 from telegram import __version_info__
24except ImportError:
25 __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
26
27if __version_info__ < (20, 0, 0, "alpha", 5):
28 raise RuntimeError(
29 f"This example is not compatible with your current PTB version {TG_VER}. To view the "
30 f"{TG_VER} version of this example, "
31 f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
32 )
33from telegram import Update
34from telegram.ext import Application, ContextTypes, MessageHandler, filters
35
36# Enable logging
37
38logging.basicConfig(
39 format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
40)
41
42logger = logging.getLogger(__name__)
43
44
45async def msg(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
46 """Downloads and prints the received passport data."""
47 # Retrieve passport data
48 passport_data = update.message.passport_data
49 # If our nonce doesn't match what we think, this Update did not originate from us
50 # Ideally you would randomize the nonce on the server
51 if passport_data.decrypted_credentials.nonce != "thisisatest":
52 return
53
54 # Print the decrypted credential data
55 # For all elements
56 # Print their decrypted data
57 # Files will be downloaded to current directory
58 for data in passport_data.decrypted_data: # This is where the data gets decrypted
59 if data.type == "phone_number":
60 print("Phone: ", data.phone_number)
61 elif data.type == "email":
62 print("Email: ", data.email)
63 if data.type in (
64 "personal_details",
65 "passport",
66 "driver_license",
67 "identity_card",
68 "internal_passport",
69 "address",
70 ):
71 print(data.type, data.data)
72 if data.type in (
73 "utility_bill",
74 "bank_statement",
75 "rental_agreement",
76 "passport_registration",
77 "temporary_registration",
78 ):
79 print(data.type, len(data.files), "files")
80 for file in data.files:
81 actual_file = await file.get_file()
82 print(actual_file)
83 await actual_file.download_to_drive()
84 if (
85 data.type in ("passport", "driver_license", "identity_card", "internal_passport")
86 and data.front_side
87 ):
88 front_file = await data.front_side.get_file()
89 print(data.type, front_file)
90 await front_file.download_to_drive()
91 if data.type in ("driver_license" and "identity_card") and data.reverse_side:
92 reverse_file = await data.reverse_side.get_file()
93 print(data.type, reverse_file)
94 await reverse_file.download_to_drive()
95 if (
96 data.type in ("passport", "driver_license", "identity_card", "internal_passport")
97 and data.selfie
98 ):
99 selfie_file = await data.selfie.get_file()
100 print(data.type, selfie_file)
101 await selfie_file.download_to_drive()
102 if data.translation and data.type in (
103 "passport",
104 "driver_license",
105 "identity_card",
106 "internal_passport",
107 "utility_bill",
108 "bank_statement",
109 "rental_agreement",
110 "passport_registration",
111 "temporary_registration",
112 ):
113 print(data.type, len(data.translation), "translation")
114 for file in data.translation:
115 actual_file = await file.get_file()
116 print(actual_file)
117 await actual_file.download_to_drive()
118
119
120def main() -> None:
121 """Start the bot."""
122 # Create the Application and pass it your token and private key
123 private_key = Path("private.key")
124 application = (
125 Application.builder().token("TOKEN").private_key(private_key.read_bytes()).build()
126 )
127
128 # On messages that include passport data call msg
129 application.add_handler(MessageHandler(filters.PASSPORT_DATA, msg))
130
131 # Run the bot until the user presses Ctrl-C
132 application.run_polling()
133
134
135if __name__ == "__main__":
136 main()