bluebubbles-bot/persona/skills/greeter.py

36 lines
1.4 KiB
Python
Raw Normal View History

2023-04-10 20:46:32 +00:00
from __future__ import annotations
from typing import List
import persona
from persona import PersonaBaseSkill
from dataclasses import dataclass
import datetime
2023-04-17 23:07:43 +00:00
import logging
2023-04-10 20:46:32 +00:00
import random
import re
2023-04-17 23:07:43 +00:00
BACKOFF_SEC = 30
2023-04-10 20:46:32 +00:00
class PersonaSkill(PersonaBaseSkill) :
"""A simple test skill that responds to the message 'Hello' with 'Hello!'"""
2023-04-17 23:07:43 +00:00
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'})
2023-04-10 20:46:32 +00:00
def match_intent(self,message: Message) -> Bool :
2023-04-17 23:07:43 +00:00
# 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)
2023-04-10 20:46:32 +00:00
if matches :
return True
return False
def respond(self, message: Message) -> Message :
"""Respond to a message by generating another message."""
response_options = ['Hello!','Howdy!','Hello there!','What\'s up!','Hi there!']
response_text = random.choice(response_options)
return persona.Message(text=response_text,sender_identifier=message.sender_identifier,chat_identifier=message.chat_identifier,attachments=[],timestamp=datetime.datetime.now()+datetime.timedelta(seconds=15), recipients=[message.sender_identifier], identifier=None, meta={})