# Path: golem/core/message_parser.py
# def parse_text_message(text, num_tries=1):
#     if len(ENTITY_EXTRACTORS) <= 0:
#         logging.warning('No entity extractors configured!')
#         return {'type': 'message', 'entities': {'_message_text': [{'value': text}]}}
# 
#     entities = {}
# 
#     for extractor in ENTITY_EXTRACTORS:
#         append = extractor.extract_entities(text)
#         for entity, values in append.items():
#             entities.setdefault(entity, []).extend(values)
# 
#     logging.debug('Extracted entities:', entities)
#     append = parse_additional_entities(text)
# 
#     for (entity, value) in re.findall(re.compile(r'/([^/]+)/([^/]+)/'), text):
#         if not entity in append:
#             append[entity] = []
#         append[entity].append({'value': value})
# 
#     # add all new entity values
#     for entity, values in append.items():
#         if not entity in entities:
#             entities[entity] = []
#         entities[entity] += values
# 
#     entities['_message_text'] = [{'value': text}]
# 
#     parsed = {'entities': entities, 'type': 'message'}
# 
#     return parsed
# 
# Path: golem/core/persistence.py
# def get_redis():
#     global _connection_pool
#     global _redis
#     if not _connection_pool:
#         config = settings.GOLEM_CONFIG.get('REDIS')
#         _connection_pool = redis.ConnectionPool(
#             host=config['HOST'],
#             port=config['PORT'],
#             password=config['PASSWORD'],
#             db=0,
#             max_connections=2
#         )
#     if not _redis:
#         _redis = redis.StrictRedis(connection_pool=_connection_pool)
#     return _redis
# 
# Path: golem/tasks.py
# @shared_task
# def accept_user_message(session: dict, raw_message):
#     from golem.core.dialog_manager import DialogManager
#     session = ChatSession.from_json(session)
#     print("Accepting message - chat id {}, message: {}".format(session.chat_id, raw_message))
# 
#     dialog = DialogManager(session)
#     parsed = session.interface.parse_message(raw_message)
#     _process_message(dialog, parsed)
# 
#     should_log_messages = settings.GOLEM_CONFIG.get('SHOULD_LOG_MESSAGES', False)
# 
#     if should_log_messages and 'text' in raw_message:
#         text = raw_message['text']
#         message_logger.on_message.delay(session, text, dialog, from_user=True)
#     return True  # FIXME


import json
import logging
import string
import random
import requests
from datetime import datetime, timedelta
from typing import Optional
from django.conf import settings
from golem.core.message_parser import parse_text_message
from golem.core.persistence import get_redis
from golem.tasks import accept_user_message
        from django.urls import reverse
        from golem.core.interfaces.adapter.telegram import TelegramAdapter



# FIXME needs to be updated

class TelegramInterface:
    name = 'telegram'
    prefix = 'tg'

    @staticmethod
    def get_base_url() -> Optional[str]:
        token = settings.GOLEM_CONFIG.get('TELEGRAM_TOKEN')
        if not token:
            logging.warning('Telegram token not provided. Telegram will not work.')
            return None
        return 'https://api.telegram.org/bot{}/'.format(token)

    @staticmethod
    def init_webhooks():

        base_url = TelegramInterface.get_base_url()
        if not base_url:
            return
        url = base_url + 'setWebhook'

        logging.debug('Reverse telegram is: {}'.format(reverse('telegram')))
        callback_url = settings.GOLEM_CONFIG.get('DEPLOY_URL') + reverse('telegram')

        payload = {'url': callback_url}
        response = requests.post(url, data=payload)

# Path: golem/core/message_parser.py
# def parse_text_message(text, num_tries=1):
#     if len(ENTITY_EXTRACTORS) <= 0:
#         logging.warning('No entity extractors configured!')
#         return {'type': 'message', 'entities': {'_message_text': [{'value': text}]}}
# 
#     entities = {}
# 
#     for extractor in ENTITY_EXTRACTORS:
#         append = extractor.extract_entities(text)
#         for entity, values in append.items():
#             entities.setdefault(entity, []).extend(values)
# 
#     logging.debug('Extracted entities:', entities)
#     append = parse_additional_entities(text)
# 
#     for (entity, value) in re.findall(re.compile(r'/([^/]+)/([^/]+)/'), text):
#         if not entity in append:
#             append[entity] = []
#         append[entity].append({'value': value})
# 
#     # add all new entity values
#     for entity, values in append.items():
#         if not entity in entities:
#             entities[entity] = []
#         entities[entity] += values
# 
#     entities['_message_text'] = [{'value': text}]
# 
#     parsed = {'entities': entities, 'type': 'message'}
# 
#     return parsed
# 
# Path: golem/core/persistence.py
# def get_redis():
#     global _connection_pool
#     global _redis
#     if not _connection_pool:
#         config = settings.GOLEM_CONFIG.get('REDIS')
#         _connection_pool = redis.ConnectionPool(
#             host=config['HOST'],
#             port=config['PORT'],
#             password=config['PASSWORD'],
#             db=0,
#             max_connections=2
#         )
#     if not _redis:
#         _redis = redis.StrictRedis(connection_pool=_connection_pool)
#     return _redis
# 
# Path: golem/tasks.py
# @shared_task
# def accept_user_message(session: dict, raw_message):
#     from golem.core.dialog_manager import DialogManager
#     session = ChatSession.from_json(session)
#     print("Accepting message - chat id {}, message: {}".format(session.chat_id, raw_message))
# 
#     dialog = DialogManager(session)
#     parsed = session.interface.parse_message(raw_message)
#     _process_message(dialog, parsed)
# 
#     should_log_messages = settings.GOLEM_CONFIG.get('SHOULD_LOG_MESSAGES', False)
# 
#     if should_log_messages and 'text' in raw_message:
#         text = raw_message['text']
#         message_logger.on_message.delay(session, text, dialog, from_user=True)
#     return True  # FIXME


# Path: golem/core/interfaces/telegram.py
import json
import logging
import string
import random
import requests
from datetime import datetime, timedelta
from typing import Optional
from django.conf import settings
from golem.core.message_parser import parse_text_message
from golem.core.persistence import get_redis
from golem.tasks import accept_user_message
        from django.urls import reverse
        from golem.core.interfaces.adapter.telegram import TelegramAdapter




# FIXME needs to be updated

class TelegramInterface:
    name = 'telegram'
    prefix = 'tg'

    @staticmethod
    def get_base_url() -> Optional[str]:
        token = settings.GOLEM_CONFIG.get('TELEGRAM_TOKEN')
        if not token:
            logging.warning('Telegram token not provided. Telegram will not work.')
            return None
        return 'https://api.telegram.org/bot{}/'.format(token)

    @staticmethod
    def init_webhooks():

        base_url = TelegramInterface.get_base_url()
        if not base_url:
            return
        url = base_url + 'setWebhook'

        logging.debug('Reverse telegram is: {}'.format(reverse('telegram')))
        callback_url = settings.GOLEM_CONFIG.get('DEPLOY_URL') + reverse('telegram')

        payload = {'url': callback_url}
        response = requests.post(url, data=payload)

        if not response.json()['ok']: