/
echo.php
131 lines (97 loc) · 2.98 KB
/
echo.php
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
$data = file_get_contents("php://input");
$query = json_decode( $data );
$guid = '66acd610-159d-4881-bb04-b48f9452b98c';
$userid = 'AFPPR46VI4HFCERSD2ENKTJBTCGHF6J6ERFIWCEI7GP4YDXFRBEJI';
include('../validate-echo-request-php/valid_request.php');
$valid = validate_request( $guid, $userid );
$help = "Welcome to St. Andrew Orthodox Info. Ask me who todays saint is, whether today is a fast day, or for today's reading. What would you like to know?";
if ( $valid['success'] ) {
if ( $query ) {
$action = $query->request->intent->name;
$response = '';
if ( $action == "GetSaint" ) {
$response = getsaint();
}
elseif ( $action == "GetFast" ) {
$response = getfast();
}
elseif ( $action == "GetReading" ) {
$response = getreading();
}
elseif ( $action = 'AMAZON.HelpIntent' ) {
sendresponse( $help, false );
} else {
sendresponse( $help, false );
}
if ( $response ) {
sendresponse( $response, true );
}
} else {
sendresponse( $help, true );
}
} else {
error_log( 'Request failed: ' . $valid['message'] );
// print $valid['message'];
die();
}
/*
Get today's saint from oca.org
*/
function getsaint() {
# What's today
$today = date( 'Y/m/d' );
# Fetch, for example, https://oca.org/saints/lives/2016/02/14
$url = 'https://oca.org/saints/lives/' . $today;
$context = [
'http' => [
'method' => 'GET',
]
];
$context = stream_context_create($context);
$page = file_get_contents( $url, false, $context );
# The saint of today is in the second <h2> - the first is just
# today's date. So ...
$pattern = '/^.*?<h2 ?.*?>(.+?)<\/h2>/si';
# Throw the first one away ...
$page = preg_replace( $pattern, '', $page );
# and then capture the second one.
preg_match( $pattern, $page, $matches );
return "Today's saint or feast is: " . $matches[1];
}
function getfast() {
return "Sorry, that function isn't implemented yet. Check back in a day or two.";
}
/*
Get today's reading from oca.org
Note that the page layout will change some day and break this.
*/
function getreading() {
# The reading is fetched periodically, and stashed in
# today_reading.php
include 'today_reading.php';
$reading = reading();
return $reading;
}
/*
Format and return the response back to Alexa
*/
function sendresponse( $response, $endsession = true ) {
$response = array (
"version" => '1.0',
'response' => array (
'outputSpeech' => array (
'type' => 'PlainText',
'text' => $response
),
'card' => array (
'type' => 'Simple',
'title' => 'StAndrew',
'content' => $response
),
'shouldEndSession' => $endsession
),
);
echo json_encode($response);
}
?>