Created
June 14, 2019 13:42
-
-
Save kurtgn/e9b98c399518b2934ab66f32aad99b92 to your computer and use it in GitHub Desktop.
одна функция
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def api_dict_with_extensions(message: FBMessage) -> dict: | |
""" | |
Хелпер, который генерирует api_dict и в зависимости от урлов, | |
подложенных в кнопки, добавляет им messenger_extensions перед отправкой. | |
Это живет здесь, а не в state_objects, | |
потому что state_objects ничего не знают о настройках джанго | |
и о списке безопасных доменов. | |
""" | |
api_dict = message.api_dict() | |
if not isinstance(message, (FBCarouselMessage, FBThreeButtonMessage)): | |
return api_dict | |
# кнопки бывают только у двух типов сообщения. | |
# Для остальных типов ничего не делаем | |
if isinstance(message, FBCarouselMessage): | |
cards = api_dict['attachment']['payload']['elements'] | |
buttons = [] | |
for card in cards: | |
if 'buttons' in card: | |
buttons += card['buttons'] | |
else: | |
buttons = api_dict['payload']['buttons'] | |
regex = re.compile(settings.FLEX_FULL_REGEX) | |
for button in buttons: | |
if button['type'] != 'web_url': | |
continue | |
# является ли урл поддоменом флекса? | |
is_flex = regex.match(button['url']) | |
# Или находится в белом списке доменов платформы? | |
is_whitelisted = is_whitelisted_domain(button['url']) | |
is_https = 'https://' in button['url'] | |
if is_https and (is_flex or is_whitelisted) : | |
button['messenger_extensions'] = True | |
return api_dict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment