Twilio's Unofficial Vote for the Eurovision Song Contest
Time to read: 4 minutes
Eurovision is near and while you can't officially vote just yet, we thought we'd allow the public to vote already - and not just for this year but for ALL Eurovision's that have happened since the beginning of Eurovision. That includes 2020 - the year Eurovision was cancelled.
How do I vote?
Text '2022' to one of the following numbers to get the list of all the 2022 participants and their youtube videos. Once you have picked a winner, text “2022 Country” to the same number (e.g. 2022 Germany).
What about the other years... Could we maybe vote for those too?
You can text any year from 1956 to 2022 to any of the above numbers and then vote for your favourite song.
How do I see the results?
You can see the results for 2022 here but you can also text “Results 2022” (or “Results YYYY”) to any of the above numbers to see who is currently in the lead.
Want to run your own Eurovision voting between your friends?
Alright, let me show you how to set this up in 5 steps:
Step 1: Get the data set.
The dataset for this project is publicly available at github.com/Spijkervet/eurovision_dataset. Download it here. If you would like to also add data for 2021 and 2022, you can get that data here and here.
Step 2: Set up the database.
MongoDB Atlas is the multi-cloud database service for MongoDB, and while we don’t need this service to be multi-cloud, Atlas makes it easy to host the database for this project in a fully managed environment - and it’s free. If you don’t have an account, sign up here.
Once you have created your account, follow these steps to create your Starter cluster (M0). You can select any available region, ideally one that is close to you. Name the cluster eurovision
.
After the cluster is deployed, click on the connect button and follow the steps to connect with the mongo shell. It will ask you to create a username and password to access the database. You will also need to enable Network Access. Since this database does not require production-level settings, you can enable access from anywhere (0.0.0.0/0).
After you have confirmed that you can successfully connect to the mongo shell, exit out of the shell and issue the following command:
Replace <db_username>
with the username you created for your database in the MongoDB Atlas UI. Replace <path_to_file>
with the path to where you stored your contestants.csv file. Replace <your_cluster_id>
with the alphanumeric characters from the MongoDB connection string you used to connect with the mongo shell. Enter the password for the database user when prompted.
You should see output like this:
Your database is now set up.
Step 3: Get a Twilio Phone number
If you don’t have a Twilio account, sign up. Next, use the Twilio Console to acquire a Twilio number with texting capability. We’ll use this as our event number.
Step 4: Set up the Twilio Function
Twilio functions is a serverless event-driven framework that allows you to run Node.js scripts. Go to the functions page. Add a new function and select the blank template.
Name the function “Eurovision Song Contest” and enter /esc
in the path field.
In the code section, copy and paste the following code:
Click save. Once we connect this function to your Twilio phone number in Step 5, the function will run whenever someone sends a message to your number. The function parses the incoming message:
- If the message contains only a year, it gets the list of countries and the corresponding YouTube links for the performances for that year from the database and sends both in the reply to the user.
- If a user sends a message containing a year and a country, the code checks whether there is an entry from that country in that year and if so it saves the vote in the database. To ensure users can only vote once, the code stores a hash of the user‘s phone number with the vote.
- If a user sends a message with a year and “Results“, the function gets the current top 10 from the database and sends it to the user.
- If the message doesn‘t match any of the above, it sends a welcome message with instructions.
- If an error occurs, the function informs the user of the error.
Next, go to the Functions configure page. Add MDB_URI
as an environment variable. This is the same connection string you used above - it looks something like this:
Then, add the MongoDB node.js module as a dependency by entering mongodb
as the name and 3.6.6
as the version. Also, add the crypto-js module with the name crypto-js
and the version 4.0.0
. Click save. The mongodb module will allow you to connect to the database. The crypto-js module will allow you to hash phone numbers to keep everyone’s phone numbers private.
Step 5: Connect the Twilio Function to your Twilio Phone number
Go to the Twilio Console and click on your phone number. In the configuration screen, scroll to the Messaging options and configure it to use a Function when a message comes in. Select the function path /esc
in the dropdown menu.
You have now connected your Twilio function to your Twilio phone number.
Bonus Step: Let your friends know.
That’s it! Give your friends the number and instructions and select your own winners!
I hope this project will bring you some joy, and I would love to hear who your winners are. Let me know on Twitter @naomi_pen.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.