-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtafseer-skill.py
83 lines (64 loc) · 2.73 KB
/
tafseer-skill.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
import logging
from flask import Flask, render_template
from flask_ask import Ask, statement, question, session
from services import QuranTafseerService
app = Flask(__name__)
ask = Ask(app, "/")
logging.getLogger("flask_ask").setLevel(logging.DEBUG)
@ask.launch
def welcome():
welcome_msg = render_template('welcome')
return question(welcome_msg)
@ask.intent("AyahTafseerIntent", convert={'verse_number': int,
'chapter_number': int})
def ayah_tafseer(chapter_number, verse_number):
if verse_number is None:
return question(render_template('missing_verse_slot'))
if chapter_number is None:
return question(render_template('missing_chapter_slot'))
tafseer_response = QuranTafseerService.ayah_tafseer(
chapter_number=chapter_number,
ayah_number=verse_number,
tafseer_number=9)
tafseer_text = tafseer_response.json()['text']
has_next = 'X-Next-Ayah' in tafseer_response.headers
if not has_next:
msg = render_template('ayah_tafseer', verse_number=verse_number,
chapter_number=chapter_number,
tafseer_text=tafseer_text)
return statement(msg)
session.attributes['next_ayah'] = tafseer_response.headers['X-Next-Ayah']
msg = render_template('ayah_tafseer_next', verse_number=verse_number,
chapter_number=chapter_number,
tafseer_text=tafseer_text)
return question(msg)
@ask.intent('YesNextAyah')
def next_ayah_tafseer():
chapter_number, verse_number = session.attributes['next_ayah'].split(':')
tafseer_response = QuranTafseerService.ayah_tafseer(
chapter_number=chapter_number,
ayah_number=verse_number,
tafseer_number=9)
tafseer_text = tafseer_response.json()['text']
has_next = 'X-Next-Ayah' in tafseer_response.headers
if not has_next:
msg = render_template('ayah_tafseer', verse_number=verse_number,
chapter_number=chapter_number,
tafseer_text=tafseer_text)
return statement(msg)
session.attributes['next_ayah'] = tafseer_response.headers['X-Next-Ayah']
msg = render_template('ayah_tafseer_next', verse_number=verse_number,
chapter_number=chapter_number,
tafseer_text=tafseer_text)
return question(msg)
@ask.intent("AMAZON.StopIntent")
def stop_intent():
return statement(render_template('thanks'))
@ask.intent("AMAZON.CancelIntent")
def cancel_intent():
return statement(render_template('thanks'))
@ask.intent("AMAZON.HelpIntent")
def help_intent():
return question(render_template('help'))
if __name__ == '__main__':
app.run()