· 6 years ago · Apr 17, 2020, 07:46 PM
1<?php
2// gotoblock - basic redirect for ChatFuel
3// by Pat Friedl
4// https://braintrustinteractive.com/
5// https://clkths.us/why-bots
6
7header('Access-Control-Allow-Origin: *');
8header('Access-Control-Allow-Credentials: true');
9header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
10header("Cache-Control: post-check=0, pre-check=0", false);
11header("Pragma: no-cache");
12
13switch($_SERVER['REQUEST_METHOD']){
14 case 'GET':
15 $_request = &$_GET;
16 break;
17 case 'POST':
18 $_request = &$_POST;
19 break;
20 default:
21 $_request = &$_GET;
22}
23if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_SERVER['QUERY_STRING'])){
24 parse_str($_SERVER['QUERY_STRING'], $qs);
25 foreach($qs as $key => $value){
26 $_request[$key] = $value;
27 }
28}
29
30function getRequest($req = '',$fallback = null){
31 global $_request;
32 $val = null;
33 if(!empty($_request[$req])){
34 $val = trim($_request[$req]);
35 }
36 if(empty($val)){
37 return $fallback;
38 }
39 return $val;
40}
41
42$help = (!empty($_request['help']))? $_request['help'] : 0;
43
44$data = array();
45
46if(empty($help)){
47 foreach($_request as $key => $val){
48 if(!empty($val)){
49 $redirect = $val;
50 break;
51 }
52 }
53}
54
55if(!empty($redirect)){
56 if(strpos($redirect,',') !== false){
57 $redirect = explode (',',$redirect);
58 foreach($redirect as $redir){
59 $data['redirect_to_blocks'][] = trim($redir);
60 }
61 } else {
62 $data['redirect_to_blocks'][] = $redirect;
63 }
64}
65
66if(!$help){
67 echo json_encode($data);
68} else {
69?>
70
71<!DOCTYPE html>
72<html lang="en">
73<head>
74<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
75<meta charset="utf-8">
76<meta http-equiv="X-UA-Compatible" content="IE=edge">
77<meta name="viewport" content="width=device-width, initial-scale=1">
78<title>Redirect script for ChatFuel or ManyChat</title>
79
80<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
81</head>
82<body>
83 <div class="container">
84 <div class="row">
85 <div class="col">
86 <h2>Redirect script for ChatFuel</h2>
87 <h6>© 2018 Pat Friedl, <a href="https://braintrustinteractive.com" target="_blank">Braintrust Interactive</a></h6>
88 </div>
89 </div>
90 <div class="row">
91 <div class="col">
92 <p>
93 <strong>What does this script do?</strong>
94 </p>
95 <p>
96 ChatFuel Go To Blocks don't allow you to use attributes to redirect to a block, so you can't redirect to {{last visited block name}} or other attribute.
97
98 This script allows bot builders to create a post via JSON API Plugin and redirect to a block from any posted value instead of using a Go To Block.
99 </p>
100 <p>
101 The service accepts a number of parameters and returns JSON code formatted for use with ChatFuel's JSON API.
102 </p>
103 </div>
104 </div>
105 <div class="row">
106 <div class="col">
107 <p>
108 <strong>Sample data:</strong>
109 </p>
110<code><pre>{
111 "redirect_to_blocks": [
112 "Welcome message",
113 "Default answer"
114 ]
115}</pre></code>
116 </div>
117 </div>
118 <div class="row">
119 <div class="col">
120 <p>
121 <strong>Posting to the service:</strong>
122 </p>
123 <p>
124 Values may be sent to the service via POST or GET. The endpoint of the script is:<br>
125 <strong>https://braintrustinteractive.com/chatfuel/scripts/gotoblock.php</strong>
126 </p>
127
128 <table class="table">
129 <thead>
130 <tr>
131 <th scope="col">Field</th>
132 <th scope="col">Value</th>
133 <th scope="col">Required/Optional</th>
134 <th scope="col">Default</th>
135 <th scope="col">Description</th>
136 </tr>
137 </thead>
138 <tbody>
139 <tr>
140 <th scope="row">*</th>
141 <td>Any string</td>
142 <td>required</td>
143 <td> </td>
144 <td>
145 <p>
146 You can pass any user attribute with any name from ChatFuel and it will be processed as a redirect to blocks. It's assumed that the value of the parameter you pass in is the name of a block in your bot. Note that only the first non-empty parameter will be processed.
147 </p>
148 <p>
149 If the value of the attribute you pass contains commas, then it will be parsed as redirecting to blocks in a series (bot init,welcome message,default answer).
150 </td>
151 </tr>
152 <tr>
153 <th scope="row">help</th>
154 <td>0/1</td>
155 <td>optional</td>
156 <td>0</td>
157 <td>When true, dispays this help file</td>
158 </tr>
159 </tbody>
160 </table>
161 </div>
162 </div>
163 <div class="row">
164 <div class="col">
165 <h6>
166 Need more bot integration or bot development? Check out:<br>
167 <a href="https://braintrustinteractive.com" target="_blank">Braintrust Interactive</a>
168 </h6>
169 </div>
170 </div>
171 </div>
172<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
173<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script>
174<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
175</body>
176</html>
177
178<?php } ?>