Skip to main content

Command Palette

Search for a command to run...

Build and deploy a USSD application with Africa's Talking, Docker and Heroku [Python] - Part 2

Updated
0 min read
Build and deploy a USSD application with Africa's Talking, Docker and Heroku [Python] - Part 2
A

I am a developer, developer relations expert and a user experience designer at Africa's Talking and Elarian. I believe that the next wave of wealth for Africa will be unlocked through the digital economy.

Time to deploy 🚀

Welcome back! We just built an entire USSD application in the previous tutorial but we hadn't made it available over the open internet for it to access Africa's Talking services. Over the next few minutes we will:

  1. Deploy our application to Heroku
  2. Use our new URL to set up a USSD code on Africa's Talking

Let's get to it!

Setting up

Docker

Get docker installed for your machine. You can follow their guides here. Once installed run:

docker --version

Also, we'll create a new file Dockerfile.

Heroku

Getting started with Heroku is quick and easy; sign up or log in and we should be good. Once signed in, install the Heroku CLI here.

Head over to your terminal (on the same project as before) and type the following:

heroku login

This will prompt you to login on your browser. If you already have it should seamlessly login and we are set!

Creating a Docker image of our application

We are containerizing our application using Docker which allows us to:

  1. Ease our deployment process and configuration
  2. Build highly scalable applications
  3. Isolate services

To create a snapshot of our application, also called an image in docker speak we'll add the following into our Dockerfile:

FROM python:3
WORKDIR /code
ENV FLASK_APP app.py
ENV FLASK_RUN_HOST 0.0.0.0
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["python","app.py"]

This is what is happening:

  1. The first line creates a container built on the Python 3 Alpine image which a lightweight linux distribution.
  2. Line 2 sets the working directory as code
  3. Line 3 sets the default flask application as app.py. This is an environmental variable
  4. Line 4 sets the default host to 0.0.0.0. This is an environmental variable as well
  5. We then copy requirements.txt into the container
  6. We install the packages defined in the requirements.txt file into the container
  7. We copy all the project files into the container's working directory which is /code.
  8. We then execute a command python app.py in the container's terminal

We have essentially finished setting up our container. We just need to run a few commands to get up and running to talk to the Africa's Talking service.

Firstly, we will build our container into an image. We ask docker to build this container, tag it ussdapp and use the Dockerfile in this folder.

docker build -t ussdapp .

Secondly, we will create a heroku app.:

heroku create

We now have an project on Heroku that can actually host our application. The Heroku CLI displays it on your terminal. We then set up heroku to take our container for deployment:

heroku container:push web --app <YOUR_APP_NAME>

The container will be published but it's still not live. Next, we make it live with this command:

heroku container:release web --app <YOUR_APP_NAME>

We are now live!!!!

You can view your application on your Heroku dashboard or view logs from the container using:

heroku logs --app <YOUR_APP_NAME> --tail

Setting up your USSD Code on Africa's Talking

Head over to your Africa's Talking dashboard and create a USSD code. The sandbox only allows for two codes. However, you can delete or reuse them as per your preference.

Screen Shot 2019-09-07 at 10.21.36 PM.png

On your callback, get the URL from your Heroku app and add it as your callback URL.

Screen Shot 2019-09-07 at 10.22.57 PM.png

Once set, we can launch the simulator which will help us test our USSD app. To use the simulator, enter your phone number and click on USSD. Type in the code that you created earlier.

Screen Shot 2019-09-07 at 10.23.42 PM.png

Screen Shot 2019-09-07 at 10.24.57 PM.png

Screen Shot 2019-09-07 at 10.26.27 PM.png

Screen Shot 2019-09-07 at 10.26.19 PM.png

And voilà! You have a USSD application deployed and out in the world.

Final words

Reach out to me or the Africa's Talking USSD team if you're interested in setting up a USSD application.

It was fun writing this tutorial. Please leave a comment on articles you would like to see around the Africa's Talking platform and tech in general. Screen Shot 2019-09-07 at 10.26.19 PM.png

K

This is great Anthony, can we contribute to one using django and can allow payment using mpesa?

M

Hi !

Please I would like to know how we can retrieve an entered value and perform some processing on it.

For example, the user enters a password, the system needs to check whether the password is correct or not.

Please help me it's urgent

B

How do i solve this: ModuleNotFoundError: No module named 'africastalking'

S

Run the following command on your terminal: pip3 install africastalking Make sure you add it to your requirements.txt file by running the command: pip freeze> requirements.txt

P

Hi Anthony, thank you so much for this excellent tutorial.

I'm stuck with building an image from a Dockerfile. Please, help me on how to go about this.

Advise also where the Dockerfile should be and if it should be in the virtual environment as well. Help me also understand if the the app.py and the requirements.txt also should be in the virtual environment.

Looking forward to your support.

M
MAZAKPE6y ago

It is my first time creating a flask app and deploying a docker container to Heroku. This article explains it nicely, thank you. I got this error (no basic auth credentials ▸ Error: docker push exited with Error: 1)

this helped me solve it https://github.com/fiorix/freegeoip/issues/171#issuecomment-4640461

A

Oh yeah, you have to login to Heroku. I'll update the article accordingly. Thanks!

A

If you find any issues, don't mind raising them in the comments. Would love to make this article as accurate as possible. :)

K

HI i am a student am doing ma final year project i deployed my ussd app but it stucks at the first level doesent continue any help please

A

Hi Khalifa Khalid could you share a GitHub repo with your code? I'd be happy to take a look at your code