Updated joke skill with spiltted providers

This commit is contained in:
Mathieu 2021-09-08 14:53:09 +02:00
parent 232ba70bbf
commit f28b927bc5
11 changed files with 82 additions and 53 deletions

View File

@ -1,44 +1,35 @@
import requests
from adapt.intent import IntentBuilder
from jarvis.skills import Skill, SkillRegistering
from jarvis.skills.decorators import intent_handler
from jarvis.utils import languages_utils, config_utils
def speak_joke():
# french jokes
if languages_utils.get_language().startswith("fr-"):
# please register on www.blagues-api.fr and set a token in your secret
response = requests.get('https://www.blagues-api.fr/api/random', headers={
'Authorization': 'Bearer ' + config_utils.get_in_secret('JOKES_FRENCH_API_TOKEN')})
data = response.json()
joke = data['joke']
answer = data['answer']
return joke + " /pause:2s/ " + answer
# english jokes
elif languages_utils.get_language().startswith("en-"):
response = requests.get('https://v2.jokeapi.dev/joke/Any?type=twopart')
data = response.json()
joke = data['setup']
answer = data['delivery']
return joke + " /pause:2s/ " + answer
else:
return "I don't know any jokes in your language..."
from jarvis.skills.decorators import intent_file_handler
from jarvis.skills.entertainement.jokes.providers import jokes_french_provider, jokes_english_provider
from jarvis.utils import languages_utils
class JokesSkill(Skill, metaclass=SkillRegistering):
def __init__(self, data=dict):
super().__init__("JokesSkill", data)
@intent_handler(IntentBuilder("JokingIntent").require("Joke"))
@intent_file_handler("tell_a_joke.intent", "TellAJokeIntent")
def handle_joke(self, data):
print(speak_joke())
self.speak(get_joke(False))
def get_joke(nsfw, lang=languages_utils.get_language()):
"""
Returns a joke in the good language
Args:
lang: use language from config by default, you can specify a custom language here
nsfw: should include nsfw jokes
Returns:
array
"""
if lang.startswith("fr-"):
return jokes_french_provider.get_joke(nsfw)
elif lang.startswith("en-"):
return jokes_english_provider.get_joke(nsfw)
else:
return ['Error', "I don't know any jokes in your language..."]
def create_skill(data):

View File

@ -0,0 +1,26 @@
import requests
def get_joke(nsfw=False):
"""
Returns a joke in 2 parts
Args:
nsfw: include nsfw jokes?
Returns: array
"""
url = 'https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,sexist,explicit&type=twopart'
if nsfw:
url = "https://v2.jokeapi.dev/joke/Any?type=twopart"
# please register on www.blagues-api.fr and set a token in your secrets file
response = requests.get(url)
data = response.json()
joke = data['setup']
answer = data['delivery']
return [joke, answer]

View File

@ -0,0 +1,29 @@
import requests
from jarvis.utils import config_utils
def get_joke(nsfw=False):
"""
Returns a joke in 2 parts
Args:
nsfw: include nsfw jokes?
Returns: array
"""
url = 'https://www.blagues-api.fr/api/random'
if nsfw:
url = url + "?disallow=dark&disallow=limit"
# please register on www.blagues-api.fr and set a token in your secrets file
response = requests.get(url, headers={
'Authorization': 'Bearer ' + config_utils.get_in_secret('JOKES_FRENCH_API_TOKEN')})
data = response.json()
joke = data['joke']
answer = data['answer']
return [joke, answer]

View File

@ -1,2 +0,0 @@
chuck
chuck norris

View File

@ -1,5 +0,0 @@
joke
jokes
make me laugh
brighten my day
tell me joke

View File

@ -1,3 +0,0 @@
neutral
non-offensive
geeky

View File

@ -1,2 +0,0 @@
chuck
chuck norris

View File

@ -1,5 +0,0 @@
blague
blagues
fais moi rire
illumine ma journée
raconte moi une blague

View File

@ -1,3 +0,0 @@
neutre
non offensant
geek

View File

@ -0,0 +1,3 @@
raconte (moi|nous|) (voir|) une blague
fais (moi|nous) rire
illumine (ma|notre) journée