Replace old joke skill by new one and a few tests in main.py

This commit is contained in:
Mathieu B 2021-07-28 23:15:27 +02:00
parent 5d7ff0c15c
commit a3e1da051b
12 changed files with 64 additions and 72 deletions

View File

@ -3,6 +3,7 @@ from flask import Flask, request, jsonify, Response
from jarvis.ia import process from jarvis.ia import process
from jarvis.skills import intent_manager from jarvis.skills import intent_manager
from jarvis.skills.entertainement.jokes import JokesSkill
from jarvis.skills.research.wikipedia import WikipediaSkill from jarvis.skills.research.wikipedia import WikipediaSkill
from utils import config_utils, flask_utils, intents_utils, utils from utils import config_utils, flask_utils, intents_utils, utils
@ -36,10 +37,12 @@ def process_request():
if __name__ == '__main__': if __name__ == '__main__':
# Tests # Tests
WikipediaSkill().register() WikipediaSkill().register()
JokesSkill().register()
intent_manager.process_handlers() intent_manager.process_handlers()
intent_manager.recognise("cherche sur wikipedia Elon Musk") intent_manager.recognise("cherche sur wikipedia Elon Musk")
intent_manager.recognise("raconte moi une blague")
# start the flask server # start the flask server
app.config['JSON_AS_ASCII'] = False app.config['JSON_AS_ASCII'] = False

View File

@ -0,0 +1,41 @@
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..."
class JokesSkill(Skill, metaclass=SkillRegistering):
def __init__(self):
super().__init__("JokesSkill")
@intent_handler(IntentBuilder("JokingIntent").require("Joke"))
def handle_joke(self, data):
print(speak_joke())

View File

@ -1,10 +0,0 @@
{
"name": "Jokes",
"languages": [
"FR-FR",
"EN-EN"
],
"intents": [
"tell_me_a_joke"
]
}

View File

@ -1,37 +0,0 @@
import requests as requests
from jarvis.utils import languages_utils, config_utils
def tell_me_a_joke():
tag = 'tell_me_a_joke'
# response = intents_utils.get_response(tag)
# 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..."

View File

@ -1,11 +0,0 @@
{
"tell_me_a_joke": {
"patterns": [
"Tell me a joke",
"Make me laugh",
"Brighten my day"
],
"responses": [
]
}
}

View File

@ -1,14 +0,0 @@
{
"tell_me_a_joke": {
"patterns": [
"raconte moi-voir une blague",
"dis moi une blague",
"raconte une blague",
"fait moi rire",
"donne une bonne blague",
"raconte moi une blagounette"
],
"responses": [
]
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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