Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cfab7d0
Added support for telegram passport. Decryption is not implemented yet.
mathefreak1 Jul 29, 2018
f3c4ae6
Fixed tests
mathefreak1 Jul 29, 2018
caa44f8
Fixed tests
mathefreak1 Jul 29, 2018
b26fc88
Fixed tests
mathefreak1 Jul 29, 2018
4ac586d
Fixed tests
mathefreak1 Jul 29, 2018
fee4cd5
Fixed tests
mathefreak1 Jul 29, 2018
80913ef
Fixed tests
mathefreak1 Jul 29, 2018
baeb49a
Fixed tests
mathefreak1 Jul 29, 2018
3961591
Fixed Bug
mathefreak1 Jul 29, 2018
f89aa7b
Remove print
mathefreak1 Jul 29, 2018
a09394b
Add basic passport decryption.
jsmnbom Jul 31, 2018
c717a70
Move passport errors to single file
jsmnbom Aug 10, 2018
2c82a5d
Add better passport credential handling
jsmnbom Aug 10, 2018
a637b42
Add better passport example
jsmnbom Aug 10, 2018
e1234e2
Add additional passport datatypes
jsmnbom Aug 10, 2018
738a4ba
Allow passing private key password to updator
jsmnbom Aug 10, 2018
9092731
Add passport docs
jsmnbom Aug 10, 2018
9f5b33f
Only accept passport messages in passportbot examples
jsmnbom Aug 10, 2018
f7e22a9
Fix some tests
jsmnbom Aug 10, 2018
a8a5d49
Using correct escape function
mathefreak1 Aug 12, 2018
014ba26
Merge remote-tracking branch 'origin/Passport' into Passport
mathefreak1 Aug 12, 2018
b3f4de2
Allow parsing of already encrypted stuff into passportdata
jsmnbom Aug 12, 2018
50c9376
Add proper passportdata decryption tests
jsmnbom Aug 12, 2018
14d75a2
Replace non ascii chars with ascii ones
jsmnbom Aug 12, 2018
ef48a09
Remove test_passportdata
jsmnbom Aug 12, 2018
69b03c5
Make passport decryption work on py2
jsmnbom Aug 12, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/source/telegram.credentials.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
telegram.Credentials
====================

.. autoclass:: telegram.Credentials
:members:
:show-inheritance:
6 changes: 6 additions & 0 deletions docs/source/telegram.datacredentials.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
telegram.DataCredentials
========================

.. autoclass:: telegram.DataCredentials
:members:
:show-inheritance:
6 changes: 6 additions & 0 deletions docs/source/telegram.filecredentials.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
telegram.FileCredentials
========================

.. autoclass:: telegram.FileCredentials
:members:
:show-inheritance:
6 changes: 6 additions & 0 deletions docs/source/telegram.iddocumentdata.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
telegram.IdDocumentData
=======================

.. autoclass:: telegram.IdDocumentData
:members:
:show-inheritance:
6 changes: 6 additions & 0 deletions docs/source/telegram.passportelementerror.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
telegram.PassportElementError
=============================

.. autoclass:: telegram.PassportElementError
:members:
:show-inheritance:
6 changes: 6 additions & 0 deletions docs/source/telegram.passportelementerrordatafield.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
telegram.PassportElementErrorDataField
======================================

.. autoclass:: telegram.PassportElementErrorDataField
:members:
:show-inheritance:
6 changes: 6 additions & 0 deletions docs/source/telegram.passportelementerrorfile.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
telegram.PassportElementErrorFile
=================================

.. autoclass:: telegram.PassportElementErrorFile
:members:
:show-inheritance:
6 changes: 6 additions & 0 deletions docs/source/telegram.passportelementerrorfiles.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
telegram.PassportElementErrorFiles
==================================

.. autoclass:: telegram.PassportElementErrorFiles
:members:
:show-inheritance:
6 changes: 6 additions & 0 deletions docs/source/telegram.passportelementerrorfrontside.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
telegram.PassportElementErrorFrontSide
======================================

.. autoclass:: telegram.PassportElementErrorFrontSide
:members:
:show-inheritance:
6 changes: 6 additions & 0 deletions docs/source/telegram.passportelementerrorreverseside.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
telegram.PassportElementErrorReverseSide
========================================

.. autoclass:: telegram.PassportElementErrorReverseSide
:members:
:show-inheritance:
6 changes: 6 additions & 0 deletions docs/source/telegram.personaldetails.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
telegram.PersonalDetails
========================

.. autoclass:: telegram.PersonalDetails
:members:
:show-inheritance:
6 changes: 6 additions & 0 deletions docs/source/telegram.residentialaddress.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
telegram.ResidentialAddress
===========================

.. autoclass:: telegram.ResidentialAddress
:members:
:show-inheritance:
20 changes: 20 additions & 0 deletions docs/source/telegram.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ Games
telegram.callbackgame
telegram.gamehighscore

Passport
--------

.. toctree::

telegram.passportelementerror
telegram.passportelementerrorfile
telegram.passportelementerrorreverseside
telegram.passportelementerrorfrontside
telegram.passportelementerrorfiles
telegram.passportelementerrordatafield
telegram.passportelementerrorfile
telegram.credentials
telegram.datacredentials
telegram.securedata
telegram.filecredentials
telegram.iddocumentdata
telegram.personaldetails
telegram.residentialaddress


Module contents
---------------
Expand Down
6 changes: 6 additions & 0 deletions docs/source/telegram.securedata.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
telegram.SecureData
===================

.. autoclass:: telegram.SecureData
:members:
:show-inheritance:
29 changes: 29 additions & 0 deletions examples/passportbot.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Telegram passport test!</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!--- Needs file from https://github.com/TelegramMessenger/TGPassportJsSDK downloaded --->
<script src="telegram-passport.js"></script>
<script>
"use strict";

Telegram.Passport.createAuthButton('telegram_passport_auth', {
bot_id: BOT_ID, // YOUR BOT ID
scope: ['id_document', 'phone_number', 'email'], // WHAT DATA YOU WANT TO RECEIVE
public_key: '-----BEGIN PUBLIC KEY----- ...', // YOUR PUBLIC KEY
payload: 'thisisatest', // YOUR BOT WILL RECEIVE THIS DATA WITH THE REQUEST
callback_url: 'https://example.org' // TELEGRAM WILL SEND YOUR USER BACK TO THIS URL
});

</script>
</head>
<body>
<h1>Telegram passport test</h1>

<div id="telegram_passport_auth"></div>
</body>
</html>
115 changes: 115 additions & 0 deletions examples/passportbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Simple Bot to print/download all incoming passport data
# This program is dedicated to the public domain under the CC0 license.
"""
See https://telegram.org/blog/passport for info about what telegram passport is.

To try this example with simple javascript SDK do the following:

1) Make sure you have openssl
2) Generate a private key by typing this in a terminal
> openssl genrsa 2048 > private.key
WARNING: Keep this key private
3) Generate a public key by typing this in a terminal
> openssl rsa -in private.key -pubout
4) Copy-paste the result of previous command into /setpublickey with @BotFather
5) Add a privacy policy to your bot that details how you intend to handle this data
6) Open examples/passportbot.html and insert your bot id and public key. Bot id is the part
before : in your bot token
7) Also in examples/passportbot.html decide what scopes (data) your bot needs
8) If you upload the examples/passportbot.html file to a server, put the url in the callback
url. For test/example purposes, example.org is fine.
9) Make sure that the SDK file from https://github.com/TelegramMessenger/TGPassportJsSDK is
download and in the same directory as examples/passportbot.html.
10) Make sure the private.key from step 2 is in the same folder as this file.
11) Start the bot by executing this file
12) Open examples/passportbot.html in a browser and click the button
13) This should open telegram with it's passport setup
14) After you've authorized the request, the bot will print and download all the data and files.

"""
import logging

from telegram.ext import Updater, MessageHandler, Filters

# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG)

logger = logging.getLogger(__name__)


def msg(bot, update):
# If we received any passport data
passport_data = update.message.passport_data
if passport_data:
# Print the decrypted credential data
# For all elements
# Print their decrypted data
# Files will be downloaded to current directory
for data in passport_data.data:
if data.type == 'phone_number':
print('Phone: ', data.phone_number)
elif data.type == 'email':
print('Email: ', data.email)
if data.type in ('personal_details', 'passport', 'driver_license', 'identity_card',
'identity_passport', 'address'):
print(data.type, data.data)
if data.type in ('utility_bill', 'bank_statement', 'rental_agreement',
'passport_registration', 'temporary_registration'):
print(data.type, len(data.files), 'files')
for file in data.files:
actual_file = file.get_file()
print(actual_file)
actual_file.download()
if data.type in ('passport', 'driver_license', 'identity_card',
'internal_passport'):
if data.front_side:
file = data.front_side.get_file()
print(data.type, file)
file.download()
if data.type in ('driver_license' and 'identity_card'):
if data.reverse_side:
file = data.reverse_side.get_file()
print(data.type, file)
file.download()
if data.type in ('passport', 'driver_license', 'identity_card',
'internal_passport'):
if data.selfie:
file = data.selfie.get_file()
print(data.type, file)
file.download()


def error(bot, update, error):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, error)


def main():
"""Start the bot."""
# Create the Updater and pass it your token and private key
updater = Updater("TOKEN", private_key=open('private.key', 'rb').read())

# Get the dispatcher to register handlers
dp = updater.dispatcher

# On messages that include passport data call msg
dp.add_handler(MessageHandler(Filters.passport_data, msg))

# log all errors
dp.add_error_handler(error)

# Start the Bot
updater.start_polling()

# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
future>=0.16.0
certifi
cryptography
25 changes: 23 additions & 2 deletions telegram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
from .payment.orderinfo import OrderInfo
from .payment.successfulpayment import SuccessfulPayment
from .payment.invoice import Invoice
from .passport.credentials import EncryptedCredentials
from .passport.passportfile import PassportFile
from .passport.data import IdDocumentData, PersonalDetails, ResidentialAddress
from .passport.encryptedpassportelement import EncryptedPassportElement
from .passport.passportdata import PassportData
from .message import Message
from .callbackquery import CallbackQuery
from .choseninlineresult import ChosenInlineResult
Expand Down Expand Up @@ -99,6 +104,17 @@
from .files.inputmedia import InputMedia
from .files.inputmediavideo import InputMediaVideo
from .files.inputmediaphoto import InputMediaPhoto
from .passport.passportelementerrors import (PassportElementError,
PassportElementErrorDataField,
PassportElementErrorFile,
PassportElementErrorFiles,
PassportElementErrorFrontSide,
PassportElementErrorReverseSide,
PassportElementErrorSelfie)
from .passport.credentials import (Credentials,
DataCredentials,
SecureData,
FileCredentials)
from .version import __version__ # flake8: noqa

__author__ = 'devs@python-telegram-bot.org'
Expand All @@ -116,7 +132,8 @@
'InlineQueryResultPhoto', 'InlineQueryResultVenue', 'InlineQueryResultVideo',
'InlineQueryResultVoice', 'InlineQueryResultGame', 'InputContactMessageContent', 'InputFile',
'InputLocationMessageContent', 'InputMessageContent', 'InputTextMessageContent',
'InputVenueMessageContent', 'KeyboardButton', 'Location', 'Message', 'MessageEntity',
'InputVenueMessageContent', 'KeyboardButton', 'Location', 'EncryptedCredentials',
'PassportFile', 'EncryptedPassportElement', 'PassportData', 'Message', 'MessageEntity',
'ParseMode', 'PhotoSize', 'ReplyKeyboardRemove', 'ReplyKeyboardMarkup', 'ReplyMarkup',
'Sticker', 'TelegramError', 'TelegramObject', 'Update', 'User', 'UserProfilePhotos', 'Venue',
'Video', 'Voice', 'MAX_MESSAGE_LENGTH', 'MAX_CAPTION_LENGTH', 'SUPPORTED_WEBHOOK_PORTS',
Expand All @@ -125,5 +142,9 @@
'Game', 'GameHighScore', 'VideoNote', 'LabeledPrice', 'SuccessfulPayment', 'ShippingOption',
'ShippingAddress', 'PreCheckoutQuery', 'OrderInfo', 'Invoice', 'ShippingQuery', 'ChatPhoto',
'StickerSet', 'MaskPosition', 'CallbackGame', 'InputMedia', 'InputMediaPhoto',
'InputMediaVideo'
'InputMediaVideo', 'PassportElementError', 'PassportElementErrorFile',
'PassportElementErrorReverseSide', 'PassportElementErrorFrontSide',
'PassportElementErrorFiles', 'PassportElementErrorDataField', 'PassportElementErrorFile',
'Credentials', 'DataCredentials', 'SecureData', 'FileCredentials', 'IdDocumentData',
'PersonalDetails', 'ResidentialAddress'
]
2 changes: 1 addition & 1 deletion telegram/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def to_dict(self):
data = dict()

for key in iter(self.__dict__):
if key in ('bot', '_id_attrs'):
if key in ('bot', '_id_attrs', '_credentials'):
continue

value = self.__dict__[key]
Expand Down
Loading