From de3b39f3ca175cc826a99361dd0c6cfd500f58e5 Mon Sep 17 00:00:00 2001 From: Daniel Dayley Date: Mon, 17 Apr 2023 17:07:43 -0600 Subject: [PATCH] Minor Improvements --- persona/skills/chicken.py | 16 ++++++++++++++-- persona/skills/greeter.py | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/persona/skills/chicken.py b/persona/skills/chicken.py index 52b79f9..388deb5 100644 --- a/persona/skills/chicken.py +++ b/persona/skills/chicken.py @@ -3,11 +3,24 @@ from typing import List import persona from persona import PersonaBaseSkill import datetime +import logging import re +BACKOFF_SEC = 30 + class PersonaSkill(PersonaBaseSkill) : """A simple test skill that responds to the joke 'Why did ____ cross the road?''""" + + def __init__(self) : + self.last_check = datetime.datetime.now().timestamp() - BACKOFF_SEC + self.log = logging.getLogger(__name__) + self.log = logging.LoggerAdapter(self.log,{'log_module':'chicken'}) + def match_intent(self,message: Message) -> Bool : + # Don't respond if you've responded already recently + if datetime.datetime.now().timestamp() < (self.last_check + BACKOFF_SEC) : + self.log.warn('Responding too fast, not responding again.') + return False matches = re.search('^Why did the .* cross the road\?', message.text) if matches : return True @@ -15,5 +28,4 @@ class PersonaSkill(PersonaBaseSkill) : def respond(self, message: Message) -> Message : """Respond to a message by generating another message.""" - message.text = 'To get to the other side!' - return message + return persona.Message(text='To get to the other side!',sender_identifier=message.sender_identifier,chat_identifier=message.chat_identifier,attachments=[],timestamp=datetime.datetime.now(), recipients=[message.sender_identifier], identifier=None, meta={}) diff --git a/persona/skills/greeter.py b/persona/skills/greeter.py index 6dc48c0..8bc1a92 100644 --- a/persona/skills/greeter.py +++ b/persona/skills/greeter.py @@ -4,13 +4,26 @@ import persona from persona import PersonaBaseSkill from dataclasses import dataclass import datetime +import logging import random import re +BACKOFF_SEC = 30 + class PersonaSkill(PersonaBaseSkill) : """A simple test skill that responds to the message 'Hello' with 'Hello!'""" + + def __init__(self) : + self.last_check = datetime.datetime.now().timestamp() - BACKOFF_SEC + self.log = logging.getLogger(__name__) + self.log = logging.LoggerAdapter(self.log,{'log_module':'greeter'}) + def match_intent(self,message: Message) -> Bool : - matches = re.search('^Hello', message.text) + # Don't respond if you've responded already recently + if datetime.datetime.now().timestamp() < (self.last_check + BACKOFF_SEC) : + self.log.warn('Responding too fast, not responding again.') + return False + matches = re.search('^Hello$', message.text) if matches : return True return False