Ejemplo n.º 1
0
    def __init__(self, settings):
        assert Connection.instance is None
        self.driver = Driver(settings)
        #self.driver.client.activate_verbose_logging()
        self.bot = Bot(convert_to_mmpy_bot(settings))
        Connection.instance = self

        # workaround for python versions < 3.7
        if sys.version_info.major < 3 or sys.version_info.minor < 7:
            api.load_driver_attributes()
Ejemplo n.º 2
0
class Connection:
    """
	This class allows the user to connect to a Mattermost server and start the bot.
	
	There should be only one instance per program."""

    instance = None

    def __init__(self, settings):
        assert Connection.instance is None
        self.driver = Driver(settings)
        #self.driver.client.activate_verbose_logging()
        self.bot = Bot(convert_to_mmpy_bot(settings))
        Connection.instance = self

        # workaround for python versions < 3.7
        if sys.version_info.major < 3 or sys.version_info.minor < 7:
            api.load_driver_attributes()

    def __getattr__(self, name):
        return getattr(self.driver, name)

    def login(self):
        logger.info("login...")
        self.driver.login()
        api.me = api.User.by_id(self.driver.client.userid)
        logger.info(f"logged in as {api.me}")

    def start(self):
        # start bot at the end because the method will not return unless an exception occurs
        logger.info("start bot...")
        self.bot.run()
        logger.info("bot started")

    def stop(self):
        self.driver.logout()
        # TODO stop bot

    def __enter__(self):
        self.start()
        return self

    def __exit__(self, type, value, tb):
        self.stop()
Ejemplo n.º 3
0
    reply_random_quote(message)


quote_enabled = False


@listen_to('citation-auto', re.IGNORECASE)
def citation_auto(message):
    global quote_enabled
    if (quote_enabled == False):
        message.reply("Citations activées : une citation toute les 12 heures")
        schedule.every(12 * 3600).seconds.do(reply_random_quote, message)
        quote_enabled = True
    else:
        message.reply("Citations désactivées")
        schedule.clear()
        quote_enabled = False


@listen_to('stop-citation', re.IGNORECASE)
def cancel_jobs(message):
    schedule.clear()
    message.reply('Job arrêté')
    global quote_enabled
    quote_enabled = False


if __name__ == "__main__":

    Bot().run()
Ejemplo n.º 4
0
def test_API_version_check():
    settings.MATTERMOST_API_VERSION = 3
    with pytest.raises(ValueError):
        bot = Bot()
Ejemplo n.º 5
0
def test_API_version_check():
    settings.MATTERMOST_API_VERSION = 3
    with pytest.raises(ValueError):
        bot = Bot()  # noqa: F841; pylint: disable=unused-variable