-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpokemon_info.py
95 lines (72 loc) · 2.31 KB
/
pokemon_info.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# =========================================================
# Author:
# Rodolfo Ferro Pérez
# ferro(at)cimat(dot)mx
#
# Alexa skill to gather Pokemon info from PokeAPI
# =========================================================
from flask_ask import Ask, statement, question, session
from flask import Flask
import unidecode
import requests
import logging
import json
import time
# Set global variables
name = 'Rodolfo'
log = logging.getLogger()
log.addHandler(logging.StreamHandler())
log.setLevel(logging.DEBUG)
logging.getLogger("flask_ask").setLevel(logging.DEBUG)
# Create Flask, Ask apps
app = Flask(__name__) # Standard Flask app
ask = Ask(app, "/poke_info") # App endpoint
# App route
@app.route("/")
def homepage():
return "Hey there {}! Flask is running with no problems!".format(name)
# Alexa initial message (starting app...)
@ask.launch
def start_skill():
welcome_msg = "Hi, would you like me to give you any Pokemon's info?"
return question(welcome_msg)
# If answer is yes
@ask.intent("YesIntent")
def yes_intent():
poke_msg = "What Pokemon's info would you like? Give me an ID number."
return question(poke_msg)
# Ask for Pokémon
@ask.intent("PokeIntent", convert={'pokemonid': int})
def poke_intent(pokemonid):
poke_msg = get_poke_info(pokemonid)
return statement(poke_msg)
# If answer is no
@ask.intent("NoIntent")
def no_intent():
bye_text = "Okay, good bye."
return statement(bye_text)
# End session
@ask.session_ended
def session_ended():
log.debug("Session ended.")
return "", 200
# Get poke info
def get_poke_info(pokemonid):
# Set url and do request
url = "http://pokeapi.co/api/v2/pokemon/"
headers = {'User-Agent': 'Pokémon info with Alexa'}
response = requests.get(url + str(pokemonid), headers=headers)
poke_data = ""
print(url + str(pokemonid))
print(response)
# Construct answer
if response.ok:
data = response.json()
poke_data += "The Pokemon is " + data['name'].title() + "... "
poke_data += "It's height is " + str(data['height']/10) + " meters... "
poke_data += "It's weight is " + str(data['weight']/10) + " kilograms."
else:
poke_data += "Sorry, I couldn't find any Pokemon's info with that ID."
return poke_data
if __name__ == "__main__":
app.run(debug=True)