Tutorial: a poll
A one-question poll, start to finish: save the choice, post it with the caller’s phone number, and handle a repeat vote.
What you will build
A one-question poll. The caller picks an answer, and your API receives the choice with the caller's phone number, so each number counts once. It takes about fifteen minutes and uses four kinds of step: a menu, Send Data, a Smart Router and End Session.
Which road should be fixed first? 1. Spintex Road 2. Kaneshie-Mallam 3. Adenta-Dodowa
Thank you. Your vote is recorded.
| Step | Type | What it does |
|---|---|---|
| Welcome | Entry Point | Says what the poll is and offers Vote or Exit. |
| Question | Choice Menu | Saves the choice as road. |
| Submit vote | Send Data | Posts road and the caller's number to your API. |
| Already voted? | Smart Router | Reads your API’s answer and picks the closing screen. |
| Three closing screens | End Session | Recorded, already voted, and try again later. |
What your API needs
One address that accepts a vote and says whether it was new. Asterisks will send:
{
"phone": "233241234567",
"choice": "spintex"
}and expects one of these back, within 2 seconds:
{ "status": "recorded" }
{ "status": "duplicate" }duplicate for the second. A caller whose network drops may dial and vote again, and your count stays right.Build it
Create an app with New app and Start from a blank canvas, as in the Quickstart. Then add the steps.
Set up the welcome screen
Open the Entry Point. In What the phone shows typeAccra Roads Poll. Under Menu options, name the optionsVoteandExit.Add the question
Click Choice Menu in the toolbox. Set What the phone shows toWhich road should be fixed first?and add three options. Give each a label and a Value, the short code your API will receive:In Save the choice as, typeLabel Value Spintex Road spintexKaneshie-Mallam kaneshie_mallamAdenta-Dodowa adenta_dodowaroad.Send the vote to your API
Click Send Data. In its Request section choose POST and enter your address. Then fill in the body:Under Save From Response, press Add and pairRequest body{ "phone": "{{MobileNumber}}", "choice": "{{road}}" }$.statuswithvoteStatus.Branch on the answer
Click Smart Router. In Branches, set the first branch tovoteStatus == duplicate. Everything else will leave by ELSE.Add the closing screens
Click End Session three times and set the text of each:Thank you. Your vote is recorded.You have already voted. Thank you.We could not record your vote. Please try again later.
Goodbye.for the Exit option.
Join the steps
Drag from the dot on the left-hand step to the top of the right-hand one:
| From | To |
|---|---|
| Welcome, option Vote | The question |
| Welcome, option Exit | Goodbye. |
| The question (each option) | Submit vote |
| Submit vote, bottom dot | The Smart Router |
| Submit vote, FAIL | We could not record your vote. |
| Router, first branch | You have already voted. |
| Router, ELSE | Thank you. Your vote is recorded. |
Press Check flow, fix anything it names, then Save.
Test it
Open the phone preview
Press Test in the top bar. In Dial from, type the number the test should call from, such as0241234567. Your app receives it as{{MobileNumber}}.Vote once
Reply1, then2. You should see the recorded message.Vote again from the same number
Press Dial again and repeat. Your API answersduplicateand you see the already-voted message. The Smart Router never appears: it picks the branch and moves on.Vote from another number
Change Dial from to a different number and dial again. The vote is recorded.Break it on purpose
Put a typo in the Send Data address and dial again. You should land on the try-again screen, by the FAIL route. Fix the address afterwards.
https:// and allow requests from the dashboard (CORS). On a real phone the call comes from Asterisks' servers and CORS does not apply. Testing against your laptop? An ngrok address works: the preview adds the header ngrok asks for.Make it yours
- More than one question: add another Choice Menu after the first, save it under its own name, and add it to the request body.
- A typed answer: use Ask a Question in place of the menu and send
{{comment}}. - Options from your system: switch the menu's Options come from to Your API. See Logic and data.
- Show the tally: have your API return it, save it with Save From Response, and write
{{tally}}on the closing screen.