Voting system through asterisk


I was wondering if there is a way to implement a voting system on Asterisk. If possible, using the AMI? I want clients to be able to call a DID and then vote through their phone by choosing a number on their keypad. I’d then need to record their input.

Thank you

Sounds like more of a ‘dialplan’ or AGI kind of thing.

  1. Answer call.
  2. Play statement of issue.
  3. Capture valid keystroke.
  4. Write a row to a database including CID (and maybe a PIN) to reduce fraud and duplicate votes.


Thanks for the reply. Is there an astserisk wiki page that would help me out with how to capture a valid keystroke?


Basim Khan

There are several ways. Asterisk is a ‘Lego set’ – you get to choose :slight_smile:

A lot will be based on your skill level, your desired user experience, call volume, fraud controls, etc. A solution for ‘Where should we go for lunch today’ for members of your team is very different than ‘Vote for your favorite American Idol contestant.’

You should read up on Asterisk dialplans (extensions.conf), extension matching, the background() vs playback() applications, the read() application, and probably others.


Thank you. I was able to do this using read, and then I was able to store the input into a seperate file as well. I will look into the applications you reccommended for me and see if there is a better method. I also wanted to ask, is there a way to randomize with Asterisk? For example if I have 5 different messages to play, is it possible to randomize the order they play in?

Thank you

There is a RAND dialplan function[1] which returns a random number. It’s up to you to use this in dialplan logic to randomize playback.

[1] Asterisk 18 Function_RAND - Asterisk Project - Asterisk Project Wiki


Thanks for the reply. I went to the wiki page and it only shows min and max as parameters to Rand. Is there anywhere I can find examples of how it is used, I can’t seem to figure out how to use that along with Playback.

Thanks again

It will give you a random number between the min and max you specify, based on that you can determine which file to play back. I don’t have a pre-written example, but the bits required to implement such a thing are present - it’s up to you to put them together.

Including the ‘fence posts.’

Hello, Thanks again. I figured out how to use the RAND application with playback. What I’m now wondering is, is it possible to not play the message I just played? So for instance I named all my files vote1, vote 2, etc till 5. For the playback I put in vote and then a variable that randomizes from 1-5 and it works. I am not sure however if it’s possible to not repeat the message that’s already been played in the next 4 iterations of Playback(vote${var})

Sure. Delete the file after playback :slight_smile:

It depends how you define ‘not play.’ Do you mean ‘never again,’ ‘never again for the CID,’ or ‘just not in the current call.’

If you can describe what you are trying to accomplish you may get better suggestions.

You could save the ‘number played’ in a database (Asterisk or MySQL) or a channel variable.

I think the only way you can do that is if you play them in a fixed, sequence, so only the first time through is random. It doesn’t seem worth making even the first one random, in that case.


I mean ‘just not in the current call’. I want the 5 messages to be played in each call, but to randomize the order. So in one specific call, it should not repeat a message that’s already been played, but for a different call it should still play all 5 messages, just in a randomized order. Basically we don’t want the order they play in to be consistent, but for one call, we want to play all 5 messages exactly once.

Thank you

That’s really a general programming question, rather than one about asterisk. You could a variable for each one which you set when you choose it, and just loop on the selection until you find one not already chosen. It is not clear whether you can play more than five on call, but if so, as I already said you have to repeat the same sequence after the first round, so you can work out the whole sequence before the first question.

Asterisk will expand variable within the name of another variable, so you can achieve arrays that way.

I was not aware that I could make loops in asterisk. All the programming I’ve done previously to interact with Asterisk was using the AMI so I was able to use Java. That indeed made doing things much easier, would I be able to use the AMI in this instance too? I was told earlier that that would not be the best approach. Would the AGI work? I’d have to look into that.
I will try the method you said and create variables within another one to create Arrays. Thanks, I did not know Asterisk could do that.

If you ‘pre-compute’ your permutations like:

        votelists                       =
        votelists                       = ${votelists}v1&v2&v3&v4&v5-
        votelists                       = ${votelists}v1&v2&v3&v5&v4-
        votelists                       = ${votelists}v1&v2&v4&v3&v5-
        votelists                       = ${votelists}v1&v2&v4&v5&v3-
        votelists                       = ${votelists}v5&v4&v2&v1&v3-
        votelists                       = ${votelists}v5&v4&v2&v3&v1-
        votelists                       = ${votelists}v5&v4&v3&v1&v2-
        votelists                       = ${votelists}v5&v4&v3&v2&v1-

Then your executing code reduces to:

        same = n,                       verbose(1,${CUT(votelists,,${RAND(1,120)})})

Sample code using pure Asterisk and MYSQL

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.