DiscountBot, Your Bot To Create And Send A Discount To Customers

You are the founder of a SaaS product and lots of your users create an account, but don’t buy the service? What about sending them a 20% discount after a while, maybe one week, in order to motivate them to subscribe? You don’t do anything and dream they’ll come back. Or you try your last call… and convert some of them!

Some context

The original idea of this bot was motivated by a Tweet by Peter Levels, a.k.a @levelsio, Serial Entrepreneur and the Founder of NomadList, RemoteOK and lots of other websites dedicated to the Nomad and Remote workers. Many thanks to him to share his business ideas on a regular basis.


DiscountBot, a Free Software written in Python, is able to connect to your database (all types supported by the SQLAlchemy library), get a list of customers to send a discount to (thanks to the SQL request you provide), create a discount (thanks to another SQL request you also provide) and send the discount to the customer by email.

The Project DiscountBot

Example configuration of DiscountBot

Here is the example of a configuration of DiscountBot you could use to automate sending discounts for your SaaS product:


sql_get_users=select from customerinfos where not in (select customer_id from payments)
sql_create_discount=insert into discounts (code,percentage,start,end,status,created) values ('{code}',20.00,'{start}','{end}',1,'{created}')

email_subject=Your Incredible Discount!


Let’s explain a bit:

  • the [discount] section needs code, the prefix of your discount code (suffix will be generated with the current time) and duration, the time-to-live of your discount offer.
  • the [database] section needs db_connector, db_name, db_user and db_pass to connect to your database. It also needs two complete and valid SQL requests :
    • sql_get_users provide a single column with all the emails of the customers to send discount to
    • sql_create_discount allows to create a discount in your database. You can use placeholders for dynamic values. Read the official documentation of DiscountBot about it.
  • the [email] section defines the needed values to send emails with the discount. The email_template_path provides the path to the HTML template of your discount email. The email_subject value is the subject of your email. Customize the sender email addres with email_from.

Less Talk More walk

Ok, ok jeeeeeez, here is a full example of how to setup and configure DiscountBot on your server.

The first step is to install DiscountBot from PyPI

# pip3 install discountbot[mysql] #if you use mysql, postgresql also supported or you'll have to install the driver manually

Create a dedicated user in order to safely manipulate your data

# adduser --home /var/lib/discountbot discountbot

Create a /etc/discountbot directory to store your DiscountBot configuration and configure the good permissions of the directory and file

# mkdir -p /etc/discountbot
# vim /etc/discountbot/discountbot.ini
# chown -R discountbot:root /etc/discountbot/
# chmod 640 /etc/discountbot/discountbot.ini

Create a /usr/share/discountbot directory to store the html template

# mkdir -p /usr/share/discountbot
# vim /usr/share/discountbot/
# chown -R discountbot:root /usr/share/discountbot
# chmod 640 /usr/share/discountbot/

Now lets configure a cron job in order to execute DiscountBot each day at 10:30 am

30 10 * * 1-5 discountbot discountbot

If you recently already sent manually some discounts to your customers, you should populate the DiscountBot database with the current entries, avoiding resending discounts to all customers returned by your SQL query at the first launch of DiscountBot

# su - discountbot
$ discountbot -p


DiscountBot is still a really young project and will improve given discovered bugs and your feedbacks 😉 Do not hesitate to open bug reports on the Gitlab of the project.

… and finally

You can help the Retweet Bot by donating anything through Liberaypay (also possible with cryptocurrencies). That’s a big factor motivation 😉

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *