forked from Program-O/Program-O
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
183 lines (174 loc) · 7.04 KB
/
index.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/***************************************
* http://www.program-o.com
* PROGRAM O
* Version: 2.6.11
* FILE: index.php
* AUTHOR: Elizabeth Perreau and Dave Morton
* DATE: FEB 01 2016
* DETAILS: This is the interface for the Program O JSON API
***************************************/
$cookie_name = 'Program_O_JSON_GUI';
$botId = filter_input(INPUT_GET, 'bot_id');
$convoId = filter_input(INPUT_GET, 'convo_id');
$convo_id = (isset($_COOKIE[$cookie_name])) ? $_COOKIE[$cookie_name] : ($convoId !== false && $convoId !== null) ? $convoId : jq_get_convo_id();
if (empty($convo_id)) $convo_id = jq_get_convo_id();
$bot_id = (isset($_COOKIE['bot_id'])) ? $_COOKIE['bot_id'] : ($botId !== false && $botId !== null) ? $botId : 1;
if (is_nan($bot_id) || empty($bot_id))
{
$bot_id = 1;
}
setcookie('bot_id', $bot_id);
// Experimental code
$HXFP = (isset($_SERVER['HTTP_X_FORWARDED_PORT'])) ? $_SERVER['HTTP_X_FORWARDED_PORT'] : '';
$HSP = (isset($_SERVER['SERVER_PORT'])) ? $_SERVER['SERVER_PORT'] : '';
$HTTPS = (isset($_SERVER['HTTPS'])) ? $_SERVER['HTTPS'] : '';
$HHOST = (isset($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : '';
$protocol = ((!empty($HTTPS) && $HTTPS != 'off') || $HSP == 443 || $HXFP == 443) ? "https://" : "http://";
$base_URL = $protocol . $HHOST; // set domain name for the script
$this_path = str_replace(DIRECTORY_SEPARATOR, '/', realpath(dirname(__FILE__))); // The current location of this file, normalized to use forward slashes
$this_path = str_replace($_SERVER['DOCUMENT_ROOT'], $base_URL, $this_path); // transform it from a file path to a URL
$url = str_replace('gui/jquery', 'chatbot/conversation_start.php', $this_path); // and set it to the correct script location
/*
Example URL's for use with the chatbot API
$url = 'http://api.program-o.com/v2.3.1/chatbot/';
$url = 'http://localhost/Program-O/Program-O/chatbot/conversation_start.php';
$url = 'chat.php';
*/
$display = "The URL for the API is currently set as:<br />\n$url.<br />\n";
$display .= 'Test this to make sure it is correct by <a href="' . $url . '?say=hello">clicking here</a>. Then remove this message from gui/jquery/index.php' . PHP_EOL;
$display .= 'And don\'t forget to upload your AIML files in the admin area otherwise you will not get a response!' . PHP_EOL;
#$display = '';
/**
* Function jq_get_convo_id
*
*
* @return string
*/
function jq_get_convo_id()
{
global $cookie_name;
session_name($cookie_name);
session_start();
$convo_id = session_id();
session_destroy();
setcookie($cookie_name, $convo_id);
return $convo_id;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<meta name="Description" content="A Free Open Source AIML PHP MySQL Chatbot called Program-O. Version2"/>
<meta name="keywords" content="Open Source, AIML, PHP, MySQL, Chatbot, Program-O, Version2"/>
<title>Program O jQuery GUI Examples</title>
<link rel="stylesheet" type="text/css" href="main.css" media="all"/>
<link rel="icon" href="./favicon.ico" type="image/x-icon"/>
<link rel="shortcut icon" href="./favicon.ico" type="image/x-icon"/>
<script type="text/javascript">
var URL = '<?php echo $url ?>';
</script>
</head>
<body>
<h3>Program O JSON GUI</h3>
<p class="center">
This is a simple example of how to access the Program O chatbot using the JSON API. Feel free to change the HTML
code for this page to suit your specific needs. For more advanced uses, please visit us at <a
href="http://www.program-o.com/">
Program O</a>.
</p>
<hr class="center">
<p class="center">
Please check out the Multi-bot example GUI <a href="multibot_gui_with_chatlog.php">here</a>.
</p>
<!-- The DIV below is for debugging purposes, and can be safely removed, if desired. -->
<div id="convo_id">Conversion ID: <?php echo $convo_id; ?></div>
<div class="centerthis">
<div class="rightside">
<div class="manspeech">
<div class="triangle-border bottom blue">
<div class="botsay">Hey!</div>
</div>
</div>
<div class="man"></div>
</div>
<div class="leftside">
<div class="dogspeech">
<div class="triangle-border-right bottom orange">
<div class="usersay"> </div>
</div>
</div>
<br/>
<div class="dog"></div>
</div>
</div>
<div class="clearthis"></div>
<div class="centerthis">
<form method="post" name="talkform" id="talkform" action="index.php">
<div id="chatdiv">
<label for="submit">Say:</label>
<input type="text" name="say" id="say" size="60"/>
<input type="submit" name="submit" id="submit" class="submit" value="say"/>
<input type="hidden" name="convo_id" id="convo_id" value="<?php echo $convo_id; ?>"/>
<input type="hidden" name="bot_id" id="bot_id" value="<?php echo $bot_id; ?>"/>
<input type="hidden" name="format" id="format" value="json"/>
</div>
</form>
</div>
<div id="shameless_plug">
To get your very own chatbot, visit <a href="http://www.program-o.com">program-o.com</a>!
</div>
<div id="urlwarning"><?php echo $display ?></div>
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// put all your jQuery goodness in here.
$('#talkform').submit(function (e) {
e.preventDefault();
$('#urlwarning').empty();
$('.botsay').html('(Thinking...)');
var user = $('#say').val();
$('.usersay').text(user);
var formdata = $("#talkform").serialize();
$('#say').val('');
$('#say').focus();
$.get(URL, formdata, function (data) {
console.info('Data =', data);
var b = data.botsay;
if (b.indexOf('[img]') >= 0) {
b = showImg(b);
}
if (b.indexOf('[link') >= 0) {
b = makeLink(b);
}
var usersay = data.usersay;
if (user != usersay) {
$('.usersay').text(usersay);
}
$('.botsay').html(b);
$('#urlwarning').hide();
}, 'json').fail(function (xhr, textStatus, errorThrown) {
console.error('XHR =', xhr.responseText);
$('#urlwarning').html("Something went wrong! Error = " + errorThrown).show();
});
return false;
});
});
function showImg(input) {
var regEx = /\[img\](.*?)\[\/img\]/;
var repl = '<br><a href="$1" target="_blank"><img src="$1" alt="$1" width="150" /></a>';
var out = input.replace(regEx, repl);
console.log('out = ' + out);
return out
}
function makeLink(input) {
var regEx = /\[link=(.*?)\](.*?)\[\/link\]/;
var repl = '<a href="$1" target="_blank">$2</a>';
var out = input.replace(regEx, repl);
console.log('out = ' + out);
return out;
}
</script>
</body>
</html>