- Twilio Cookbook(Second Edition)
- Roger Stringer
- 311字
- 2021-07-16 12:06:39
Sending a survey to your users
Ok, we've built the survey. Now how do we send it to our subscribers?
When you're ready to send the survey to your subscribers, this recipe will gather all active subscribers together, build the message, and send it.
It will then record the response for tracking so that we can see which answer is the popular choice or not.
Getting ready
The complete source code for this recipe can be found in the Chapter3/
folder.
How to do it...
Ok, let's send our survey to our users. In the same place you uploaded the files in the previous recipe, create a new file and name it send-survey.php
:
<?php include("config.php"); include("pdo.class.php"); include 'Services/Twilio.php'; $qid = $_GET['qid']; $_SESSION['survey'] = $qid; // we store the survey in session so we can retrieve it later $pdo = Db::singleton(); $client = new Services_Twilio($accountsid, $authtoken); $survey = $pdo->query("SELECT * FROM survey WHERE ID='{$qid}'"); if( $survey->rowCount() >= 1 ){ $survey = $survey->fetch(); $message = array(); $message[] = $survey['question']; for($i = 1;$i<= 6;$i++){ $k = 'answer'.$i; if( !empty($survey[ $k ]) ){ $message[] = $i." - ".$survey[ $k ]; } } $message[] = "Reply with the number corresponding to your answer"; $cnt = count( $message ); $res = $pdo->query("SELECT ID,phone_number FROM subscribers WHERE status='1'"); while( $row = $res->fetch() ){ $ph = $row['phone_number']; $i = 1; foreach($message as $m){ $m = $m . "({$i} / {$cnt})"; $smsg = $client->account->sms_messages->create( $fromNumber, $ph, $m ); $sid = $smsg->sid; $sql = "INSERT INTO responses SET phone_number='{$ph}',question_id='{$qid}',sms_sid='{$sid}',answer=''"; $pdo->exec( $sql ); $i++; } } } ?> <h2>Survey sent!</h2> <p><a href="survey-builder.php">Return to home</a></p>
How it works...
We can choose to send a survey from the survey builder we built in the first part of this chapter.
When we send it, it loads this recipe and grabs a list of subscribers. Each subscriber is sent a survey with a list of answers.
When we send the survey, each text message is given a unique session ID that we save in the Responses
table alongside the receiver's phone number and the ID of the survey that was sent.
Each answer has a number in front of it; when your subscribers reply, they will send the number back to listener.php
. This is then recorded in the database and we can build stats based on it.
- 物聯(lián)網(wǎng)(IoT)基礎(chǔ):網(wǎng)絡(luò)技術(shù)+協(xié)議+用例
- 物聯(lián)網(wǎng)工程規(guī)劃技術(shù)
- 數(shù)字烏托邦
- PLC、現(xiàn)場(chǎng)總線及工業(yè)網(wǎng)絡(luò)實(shí)用技術(shù)速成
- 城市治理一網(wǎng)統(tǒng)管
- Microsoft Dynamics CRM 2011 Applications(MB2-868) Certification Guide
- CCNP TSHOOT(642-832)認(rèn)證考試指南
- OMNeT++與網(wǎng)絡(luò)仿真
- 網(wǎng)絡(luò)環(huán)境中基于用戶視角的信息質(zhì)量評(píng)價(jià)研究
- INSTANT KineticJS Starter
- AIoT應(yīng)用開(kāi)發(fā)與實(shí)踐
- 轉(zhuǎn)化:提升網(wǎng)站流量和轉(zhuǎn)化率的技巧
- Implementing NetScaler VPX?
- 新媒體交互藝術(shù)
- 人際網(wǎng)絡(luò)