-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram_controllers.py
More file actions
64 lines (57 loc) · 2.86 KB
/
telegram_controllers.py
File metadata and controls
64 lines (57 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Telegram controller
"""
from telegram_models import TelegramUserAccount
from phone_util import sanitize_phone_number
import telegram_util
import logging
LOGGER = logging.getLogger(__name__)
class TelegramController(object):
def get(self, request_params, response):
"""
Endpoint called for retrieving the telegram contacts of the specified phone number. The phone number has
to be logged in first for this endpoint to work.
"""
phone_number = sanitize_phone_number(request_params.get('phone'))
page = request_params.get('page')
with TelegramUserAccount(phone_number, user_phone=phone_number) as telegram_user:
if not telegram_user.is_user_authorized():
LOGGER.error('Telegram user unauthorized.')
raise Exception('Telegram user unauthorized.')
else:
LOGGER.info('Telegram user authorized, fetching contacts...')
telegram_user.get_contacts(request_params.get('limit'), request_params.get('page'))
LOGGER.info('Fetched telegram contacts: {}'.format({
'contacts': telegram_user.contacts,
'last_page': telegram_user.last_page
}))
return {
'contacts': telegram_user.contacts,
'last_page': telegram_user.last_page
}
def post(self, request_params, response):
"""
This endpoint is called for initiating the authorization flow.
@param request_params: should contain an 'type' paramter. 'onboard' is for
the initialization of the flow, while 'code' is for verifying the authorization code.
"""
phone_number = sanitize_phone_number(request_params.get('phone'))
auth_type = request_params.get('type')
result = {}
with TelegramUserAccount(phone_number, user_phone=phone_number) as telegram_user:
if not telegram_user.is_user_authorized():
if auth_type == 'onboard':
LOGGER.info('Sending telegram verification code to {}'.format('+' + phone_number))
telegram_user.send_code_request(phone_number)
result = {"identifier": telegram_user.phone_code_hashes[phone_number]}
LOGGER.info(telegram_user.phone_code_hashes)
elif auth_type == 'code':
code = request_params.get('code')
identifier = request_params.get('identifier')
telegram_user.phone_code_hashes.update({phone_number: identifier})
LOGGER.info('Authorizing telegram phone with hash {} and code {}'
.format(telegram_user.phone_code_hashes[phone_number], code))
telegram_user.authorize_code(code)
else:
response.status = 208
return result