コード例 #1
0
def print_update_canceled(peer):
    bot.messaging.send_message(peer, _('The update has been cancelled'))
コード例 #2
0
def print_overview_canceled(peer):
    bot.messaging.send_message(peer, _('The overview has been cancelled'))
コード例 #3
0
def print_update_completed(peer):
    bot.messaging.send_message(peer, _('The result is updated'))
コード例 #4
0
def print_overview_completed(peer):
    bot.messaging.send_message(peer, _('The overview is completed'))
コード例 #5
0
    except Exception as exception:
        print(exception)
        print_error(peer, _('Some internal error happens'))


if __name__ == '__main__':
    try:
        config = parse_config()

        gtmhub_config = config['gtmhubConfig']
        dialog_config = config['dialogConfig']

        handler = GtmHubMetricHandler(gtmhub_config['url'], gtmhub_config['token'], gtmhub_config['account'])

        scheduler = GtmHubScheduler(handler.get_usernames_list(), scheduled_overview)

        bot = DialogBot.get_secure_bot(
            dialog_config['host']+':'+dialog_config['port'],
            grpc.ssl_channel_credentials(),
            dialog_config['token']
        )

        bot.messaging.on_message(on_message, on_interact)
    except KeyboardInterrupt as ex:
        print(_('Stopping scheduler thread ...'))
        if scheduler.loop.is_running() is True:
            scheduler.loop.stop()
    except Exception as ex:
        print(_('Error initializing the bot - {}').format(str(ex)))
        raise ex
コード例 #6
0
def print_need_reminder_day(peer, days_list):
    select = InteractiveMediaSelect(days_list, _('Choose day'))
    action = InteractiveMedia('cron', select)
    group = InteractiveMediaGroup([action])
    bot.messaging.send_message(peer, _('Please select day to update OKR with you'), [group])
コード例 #7
0
 def parse_numeric(value):
     try:
         return float(value)
     except ValueError:
         raise InputException(_('Invalid numeric value'))
コード例 #8
0
    def create_cron(self, peer_id):
        if self.has_cron(peer_id):
            raise InputException(_('You have a reminder curently'))

        self.crons[peer_id] = Cron(peer_id)
コード例 #9
0
 def cancel_overview(self, peer_id):
     try:
         del self.overviews[peer_id]
     except KeyError:
         raise InputException(_("You don't have an overview currently"))
コード例 #10
0
 def get_next_overview_checkin_id(self, peer_id):
     if not self.has_overview(peer_id):
         raise InputException(_('You have not an overview currently'))
     if self.get_overview_lenght(peer_id) == 0:
         raise InputException(_('Your overview is currently empty'))
     return list(self.overviews[peer_id])[0]
コード例 #11
0
 def cancel_checkin(self, peer_id):
     try:
         del self.checkins[peer_id]
     except KeyError:
         raise InputException(_("You don't have a checkin currently"))
コード例 #12
0
 def update_checkin(self, peer_id, value):
     try:
         self.checkins[peer_id].set_value(value)
     except KeyError:
         raise InputException(_("You don't have a checkin currently"))
コード例 #13
0
 def cancel_cron(self, peer_id):
     try:
         self.scheduler.del_job(peer_id)
         del self.crons[peer_id]
     except KeyError:
         InputException(_("You don't have a reminder currently"))
コード例 #14
0
def print_scheduler_canceled(peer):
    bot.messaging.send_message(peer, _('The reminder has been cancelled'))
コード例 #15
0
 def parse_text(value):
     if not value:
         raise InputException(_('Invalid empty text'))
     return value
コード例 #16
0
def on_message(*params):
    peer = params[0].peer
    message = params[0].message
    sender_uid = params[0].sender_uid

    if peer.id == sender_uid:
        try:
            if not hasattr(message, 'textMessage'):
                raise InputException(_('Invalid input value, only text messages are allowed'))

            text = message.textMessage.text

            if is_cancel_text_command(text):
                if handler.has_checkin(peer.id):
                    handler.cancel_checkin(peer.id)
                    print_update_canceled(peer)
                elif handler.has_overview(peer.id):
                    handler.cancel_overview(peer.id)
                    print_overview_canceled(peer)
                elif scheduler.need_params(peer.id):
                    scheduler.cancel_cron(peer.id)
                    print_scheduler_canceled(peer)
                else:
                    print_error(peer, _('Nothing to cancel'))
            elif is_overview_text_command(text):
                if scheduler.need_params(peer.id):
                    raise InputException(_('Please, finish your reminder configuration, or cancel it, before continue'))

                command_params = parse_command_params(_('overview'), text)

                username = command_params.get('username', None)
                list_name = command_params.get('list_name', None)

                handler.create_overview(peer.id, list_name, username)
                print_okr_list(peer, handler.overviews[peer.id])
            elif is_okr_text_command(text):
                if scheduler.need_params(peer.id):
                    raise InputException(_('Please, finish your reminder configuration, or cancel it, before continue'))
                if handler.has_checkin(peer.id):
                    raise InputException(_('You have a checkin currently'))
                if handler.has_overview(peer.id):
                    raise InputException(_("Please, finish you overview, or cancel it, before continue"))

                command_params = parse_command_params(_('okr'), text)

                username = command_params.get('username', None)
                list_name = command_params.get('list_name', None)

                okr_list = handler.get_okr_list(list_name, username)

                print_okr_list(peer, okr_list)
            elif is_activate_text_command(text):
                if handler.has_checkin(peer.id) or handler.has_overview(peer.id):
                    raise InputException(_('Please, finish your updates before begin configuring the reminder'))
                if scheduler.has_cron(peer.id):
                    raise InputException(_('You have a reminder currently, remove it to setup a new one'))
                scheduler.create_cron(peer.id)
                print_need_reminder_day(peer, GtmHubScheduler.days_list)
            elif is_desactivate_text_command(text):
                if not scheduler.has_cron(peer.id):
                    raise InputException(_('You have not a scheduler currently'))
                scheduler.cancel_cron(peer.id)
                print_scheduler_canceled(peer)
            else:
                if handler.has_checkin(peer.id):
                    handler.update_checkin(peer.id, text)
                    checkin_current_state = handler.get_checkin_state(peer.id)
                    if checkin_current_state == GtmHubCheckin.STATES.INITIALIZED:
                        print_need_changes_info(peer)
                    elif checkin_current_state == GtmHubCheckin.STATES.COMMENTED:
                        confidence_levels = handler.get_confidence_levels(peer.id)
                        print_need_confidence_level(peer, confidence_levels)
                    else:
                        raise Exception(_('Unexpected checkin state'))
                elif scheduler.need_params(peer.id):
                    scheduler.set_value(peer.id, text)
                    if not scheduler.need_params(peer.id):
                        scheduler.enable_cron(peer.id)
                        day = scheduler.days_list[scheduler.crons[peer.id].day]
                        hour = scheduler.crons[peer.id].hour
                        print_message(peer, _("Ok, then each {} at {} I will start updating okr's with you.").format(day, hour))
                    else:
                        if scheduler.crons[peer.id].hour is None:
                            print_need_scheduler_hour(peer)
                        else:
                            print_need_scheduler_username(peer)
                else:
                    raise InputException(_('Invalid command'))

        except InputException as exception:
            print(exception)
            print_error(peer, str(exception))
        except Exception as exception:
            print(exception)
            print_error(peer, _('Some internal error happens'))
コード例 #17
0
class GtmHubScheduler(threading.Thread):

    days_list = {
        '0': _('Monday'),
        '1': _('Tuesday'),
        '2': _('Wednesday'),
        '3': _('Thursday'),
        '4': _('Friday'),
        '5': _('Saturday'),
        '6': _('Sunday')
    }

    def __init__(self, allowed_usernames, callback):
        threading.Thread.__init__(self)
        self.crons = {}
        self.allowed_usernames = allowed_usernames
        self.callback = callback
        self.loop = asyncio.get_event_loop()
        self.scheduler = Scheduler(loop=self.loop)
        self.loop.create_task(self.scheduler.start())
        self.start()

    def run(self):
        self.loop.run_forever()

    @staticmethod
    def is_valid_day(day):
        return day in GtmHubScheduler.days_list.keys()

    @staticmethod
    def is_valid_hour(hour):
        try:
            time.strptime(hour, '%H:%M')
            return True
        except ValueError:
            return False

    def is_valid_username(self, username):
        return username in self.allowed_usernames

    def has_cron(self, peer_id):
        return peer_id in self.crons.keys()

    def need_params(self, peer_id):
        return self.has_cron(
            peer_id) and not self.crons[peer_id].is_configured()

    def create_cron(self, peer_id):
        if self.has_cron(peer_id):
            raise InputException(_('You have a reminder curently'))

        self.crons[peer_id] = Cron(peer_id)

    def set_value(self, peer_id, value):
        if not self.has_cron(peer_id):
            raise InputException(_('You have not a reminder to configure'))
        if self.crons[peer_id].is_configured():
            raise InputException(_('Your reminder is already configured'))
        if self.crons[peer_id].day is None:
            if not GtmHubScheduler.is_valid_day(value):
                raise InputException(_('Invalid day'))
            self.crons[peer_id].day = value
        elif self.crons[peer_id].hour is None:
            if not GtmHubScheduler.is_valid_hour(value):
                raise InputException(_('Invalid hour'))
            self.crons[peer_id].hour = value
        else:
            if not self.is_valid_username(value):
                raise InputException(
                    _('Invalid username\nAvailable usernames : {}').format(
                        '\n' + '\n'.join(self.allowed_usernames)))
            self.crons[peer_id].username = value

    def enable_cron(self, peer_id):
        day = int(self.crons[peer_id].day)
        hour = self.crons[peer_id].hour
        username = self.crons[peer_id].username

        job = CronJob(name=peer_id, loop=self.loop).weekday(day).at(hour).go(
            self.callback, peer_id, username)

        self.scheduler.add_job(job)

    def cancel_cron(self, peer_id):
        try:
            self.scheduler.del_job(peer_id)
            del self.crons[peer_id]
        except KeyError:
            InputException(_("You don't have a reminder currently"))