Ejemplo n.º 1
0
    def __init__(self,
                 surface_name: str,
                 auth_token: str,
                 webhook_url: str,
                 test_mode: bool = False) -> None:
        """Create the surface and initialize the elements

        :param surface_name: a name that identify uniquely the Telegram bot
        connected with this surface instance
        :type surface_name: str

        :param auth_token: Telegram authorization token for the connected bot
        :type auth_token: str

        :param webhook_url: webhook url for the connected bot
        :type webhook_url: str

        :param test_mode: class instance created for test purposes, some
        features are disabled
        :type test_mode: bool
        """

        super().__init__(surface_name)
        # Initialise and interact with Telegram only if not in test mode
        self._logger = LoggingService.get_logger(__name__)

        self._test_mode = test_mode
        if not self._test_mode:
            self._fix_connection_reset_error()
            self.telegram_bot = telepot.Bot(auth_token)
            self._set_webhook(webhook_url)
Ejemplo n.º 2
0
    def __init__(
        self,
        config_service: ConfigService,
        test_mode: bool = False
    ) -> None:
        """Init the class

        :param config_service: configuration service
        :type config_service: ConfigService

        :param test_mode: class instance created for test purposes, some
        features are disabled
        :type test_mode: bool
        """
        super().__init__(self.__class__.__name__, self.INTENTS)
        self._config_service = config_service
        self._logger = LoggingService.get_logger(__name__)

        # Registers the interaction surface
        self._admin_surface = TelegramNotifyAdminSurface(
            GlobalBag.SURFACE_NOTIFY_ADMIN,
            self._config_service.get_config("telegram_notifyadmin_authorization_token"),
            self._config_service.get_config("telegram_notifyadmin_chat_id"),
            test_mode=test_mode
        )
Ejemplo n.º 3
0
    def __init__(self, tasks_file: str) -> None:
        """

        :param tasks_file:
        :type tasks_file: str
        """
        # Create the logger and initialise it
        self._logger = LoggingService.get_logger(__name__)
        self._load_tasks(tasks_file)
Ejemplo n.º 4
0
    def __init__(self, config_file: str = GlobalBag.CONFIG_FILE) -> None:
        """Initialize this class.

        :param config_file: JSON file with app configuration.
        :type config_file: str
        """

        # Create the logger and initialise it
        self._logger = LoggingService.get_logger(__name__)
        self._logger.info("Config service is starting")

        # Load the config file
        self._load_config_file(config_file)
Ejemplo n.º 5
0
    def __init__(self, destination_url: str, test_mode: bool = False) -> None:
        """

        :param destination_url: url where send the author and title
        :type destination_url: str

        :param test_mode: class instance created for test purposes, some
        features are disabled
        :type test_mode: bool
        """
        super().__init__(self.__class__.__name__, self.INTENTS)
        self._logger = LoggingService.get_logger(__name__)
        self._destination_url = destination_url
        self._test_mode = test_mode
Ejemplo n.º 6
0
    def __init__(self, youtube_api_key: str, newssources_urls: List[str],
                 storage_service: BSS) -> None:
        """Constructor

        :param youtube_api_key: the API key to use for YouTube API v3 calls
        :type api_key: str 

        :param storage_service: the storage service to use. Caller object will decide the specific implementation
        :type storaga_service: BaseStorageService subclass
        """

        super().__init__(self.__class__.__name__, self.INTENTS)
        self._logger = LoggingService.get_logger(__name__)
        self._youtube_api_key = youtube_api_key
        self._newssources_urls = newssources_urls
        self._storage = storage_service
Ejemplo n.º 7
0
    def __init__(self,
                 nlu_engine: NluEngine = None,
                 scheduler: SchedulerService = None,
                 config_file: str = GlobalBag.CONFIG_FILE,
                 test_mode: bool = False) -> None:
        """Init the bot

        :param nlu_engine: engine to use to extract intent and arguments
        :type nlu_engine: NluEngine

        :param scheduler: the scheduler service
        :type scheduler: SchedulerService

        :param config_file: config file with several values. By default, the
        file yellowbot_config.json in the same folder of this file is used,
        but feel free to point to any other file. If only the file name is
        used, the assumption it is in the same folder of this file
        :type config_file: str

        :param test_mode: class instance created for test purposes, some
        features are disabled
        :type test_mode: bool
        """

        # Create the logger and initialise it
        self._logger = LoggingService.get_logger(__name__)
        self._logger.info("YellowBot is starting")

        # Load the config file
        self._config_service = ConfigService(config_file)

        # Creates the datastore service
        # db_filename = os.path.join(os.path.dirname(__file__), GlobalBag.DATABASE_FILE)
        # self._datastore = DatastoreService(db_filename)

        # Registers gears
        self._gears: List[BaseGear] = []
        self._register_gears(test_mode)

        # Assigns the NLU engine
        self._nlu_engine = nlu_engine if nlu_engine is not None else NluEngine(
        )

        # Assigns the scheduler service
        self._scheduler = scheduler if scheduler is not None else SchedulerService(
            GlobalBag.SCHEDULER_FILE)
Ejemplo n.º 8
0
    def __init__(self,
                 config_service: ConfigService,
                 test_mode: bool = False) -> None:
        """Init the class

        :param config_service: configuration service
        :type config_service: ConfigService

        :param test_mode: class instance created for test purposes, some
        features are disabled
        :type test_mode: bool
        """

        super().__init__(self.__class__.__name__, self.INTENTS)
        self._config_service = config_service
        self._logger = LoggingService.get_logger(__name__)

        # Registers the interaction surface
        self._surfaces: Dict[str, BIS] = {}
        self._register_interaction_surfaces(test_mode)
Ejemplo n.º 9
0
    def __init__(self,
                 name: str,
                 when: str,
                 timezone: str,
                 intent: str,
                 params: dict = None,
                 surface_id: str = None,
                 surface_channel_id: str = None,
                 default_message: str = None):
        """
        Create a new task for the scheduler service
        :param name: name of the task
        :type name: str
        :param when: when the task is executed, HH:mm format
        :type when: str
        :param timezone: the timezone of the task, for example "Europe/Rome"
        :type timezone: str
        :param intent: intent to launch
        :type intent: str
        :param params: optional parameters for the intent
        :type params: dict
        :param surface_id: surface id to use for communication
        :type surface_id: str
        :param surface_channel_id: surface channel id to use for communication
        :type surface_channel_id: str
        :param default_message: default message to use for communication, if the task result is empty
        :type default_message: str
        """
        self._logger = LoggingService.get_logger(__name__)

        self.name = name
        self.when = when
        self.timezone = timezone if timezone else "UTC"
        self.intent = intent
        self.default_message = default_message
        self.params = params
        if not (surface_id and surface_id.strip()):
            # If surface_id is None, empty of full of spaces
            self.surface = None
        else:
            self.surface = SurfaceMessage(surface_id, surface_channel_id, None)
Ejemplo n.º 10
0
  )

 Basic code to deal with Telegram: https://blog.pythonanywhere.com/148/
"""
import threading

from flask import Flask, request, make_response, jsonify
from werkzeug.exceptions import abort, BadRequest

from yellowbot.globalbag import GlobalBag
from yellowbot.loggingservice import LoggingService
from yellowbot.yellowbot import YellowBot
from yellowbot.surfaces.telegramsurface import TelegramSurface

# Init logging
LoggingService.init()
_logger = LoggingService.get_logger(__name__)

# Flask init
app = Flask(__name__)

# Allow to setup a test environment. It's dirty and I don't like it, but
#  it works.
# Used to avoid error like too many requests for Telegram webhook check etc
yellowbot = YellowBot(test_mode=GlobalBag.TEST_ENVIRONMENT)

# Base address for all the API calls
FLASK_BASE_API_ADDRESS = yellowbot.get_config("base_api_address")
FLASK_TELEGRAM_BOT_LURCH_WEBHOOK = yellowbot.get_config(
    "telegram_lurch_webhook_url_relative")
Ejemplo n.º 11
0
 def __init__(self) -> None:
     super().__init__(self.__class__.__name__, self.INTENTS)
     self._logger = LoggingService.get_logger(__name__)
 def __init__(self) -> None:
     """Initialize the class
     """
     super().__init__()
     self._client = datastore.Client()  # Client will contain the class
     self._logger = LoggingService.get_logger(__name__)