Remember those old jukeboxes where people used to insert a coin for it to take a record out and play the selected song? In this tutorial, we will implement our own digital jukebox that will allow you or your friends to search and add music to a playlist using the Twilio API for WhatsAppSpotipy (a lightweight Python library for the Spotify Web API) and the Flask framework for Python.

The jukebox chatbot

Tutorial Requirements

For this tutorial, we will need the following:

  • Python 3.6 or newer. You can download it from this link
  • ngrok. We will use this handy utility to connect the Flask application running on your system to a public URL that Twilio can connect to. This is necessary for the development version of the application because your computer is likely behind a router or firewall, so it isn’t directly reachable on the Internet. If you don’t have ngrok installed, you can download a copy for Windows, MacOS or Linux
  • A smartphone with an active phone number and WhatsApp installed
  • A Twilio account. If you are new to Twilio create a free account now. You can review the features and limitations of a free Twilio account

Create a Python Virtual Environment

Following Python best practices, we are going to make a separate directory for our chatbot project, and inside it we are going to create a virtual environment. We then are going to install the Python packages that we need for our chatbot on it.

If you are using a Unix or Mac OS system, open a terminal and enter the following commands to do the tasks described above:

$ mkdir spotwilio-bot
$ cd spotwilio-bot
$ python3 -m venv spotwilio-bot
$ source spotwilio-bot-venv/bin/activate
(spotwilio-bot-venv) $ pip install twilio flask spotipy python-dotenv

For those of you following the tutorial on Windows, enter the following commands in a command prompt window:

$ md spotwilio-bot
$ cd spotwilio-bot
$ python -m venv spotwilio-bot-venv
$ spotwilio-bot-venvScripts\activate
(spotwilio-bot-venv) $ pip install twilio flask spotipy python-dotenv

The last command uses pip, the Python package installer, to install the five packages that we are going to use in this project, which are:

  • The Flask framework, to create the web application that responds to incoming WhatsApp messages with it
  • The Twilio Python Helper library, to work with the Twilio APIs
  • The Spotipy library for the Spotify Web API that provides for us the basic functionality that we will be needing for this tutorial
  • The Dotenv package, to load environment variables from a .env file

For your reference, at the time this tutorial was released these were the versions of the above packages and their dependencies tested:

certifi==2020.6.20
chardet==3.0.4
click==7.1.2
python-dotenv==0.14.0
flask==1.1.2
idna==2.10
itsdangerous==1.1.0
jinja2==2.11.2
markupsafe==1.1.1
pyjwt==1.7.1
pytz==2020.1
requests==2.24.0
six==1.15.0
spotipy==2.13.0
twilio==6.44.1
urllib3==1.25.9
werkzeug==1.0.1

Configure the Twilio WhatsApp Sandbox

Twilio provides a WhatsApp sandbox where you can easily develop and test your application. Once your application is complete you can request production access for your Twilio phone number, which requires approval by WhatsApp.

Let’s connect your smartphone to the sandbox. From your Twilio Console, select Programmable Messaging, then click on “Try it Out”, and finally click on “Try WhatsApp”. The WhatsApp sandbox page will show you the sandbox number assigned to your account, and a join code.

Whatsapp sandbox

To enable the WhatsApp sandbox for your smartphone, send a WhatsApp message with the given code to the number assigned to your account. The code is going to begin with the word join, followed by a randomly generated two-word phrase. Shortly after you send the message you should receive a reply from Twilio indicating that your mobile number is connected to the sandbox and can start sending and receiving messages.

Note that this step needs to be repeated for any additional phones you’d like to have connected to your sandbox.

Getting Spotify ready for our application

Now we are going to be doing a series of tasks that need to be done before we start working with the Spotify API, like creating our playlist, authorizing our app and testing both the Spotify Web API directly and through the spotipy library.

#code #tutorials and hacks #python

Build a Digital Jukebox for WhatsApp using Spotify, Python and Twilio
2.35 GEEK