Make a discord bot for qadjokes

I participated in Qiskit Hackathon Global, from October 5 to October 10.
The entire code can be found in the github repository shared: quantum-ugly-duckling.

In this project, I made a website which is linked to the database with IBM Cloud.
The database is for saving quantum-dadjoke, in short, qadjokes.
Then, connect the database to the discord bot printing qadjokes.

I used Goorm IDE for making things above.

Make a website with Cloud Foundry

Go to IBM Cloud and type Cloud Foundry in the search bar.
Then make an app with Python. (I chose Python.)

git clone https://github.com/IBM-Cloud/get-started-python
cd get-started-python

Change name in manifest.yml to your Cloud Foundry app name.
I used nano to modify the file.

sudo apt-get install nano

Install ibmcloud command.

curl -sL https://ibm.biz/idt-installer | bash

after

You can see the screen like this if installation succeeded.

Change HTML file for your website.

cd static
nano index.html

You can check our index.html codes here: quantum-ugly-duckling/quantum-ugly-duckling-main/static/index.html.

Then, come back to the main folder and let’s deploy it.

cd ..
ibmcloud target --cf
cf login
cf push

It takes time. Please wait a moment!

Connect a database to the website

Come back to IBM Cloud and type Cloudant in the search bar.
While making, it is important to check IAM and legacy credentials in Authentication method.

In resource list, go to Cloud Foundry and click connection.
Then, you can see the blue button with Create connection.
Click it and connect with Cloudant without any setting changes.

After a while, Restage app will pop up. Click Restage.


Get credentials from Cloudant and open vcap-local.template.json.

vcap-local

Change the part in "credentials" with the credentials and save the file with vcap-local.json.


Open hello.py and add a function below.

@app.route('/index.html', methods=['GET'])
def save_two():
    Qdad = request.args.get('Qdad')
    data = {'Qdad': Qdad}
    if client:
        my_document = db.create_document(data)
        data['_id'] = my_document['_id']
    else:
        print('No database')
    return app.send_static_file('index.html')

Check also form tag in index.html.

<form action="./index.html">

❗ If you want to download Cloudant data to local PC

  1. Go to Cloudant Dashboard.
  2. Click the {}JSON button and login with credentials.
  3. Copy all the texts: Ctrl + a
  4. Paste them all here: JSON to CSV Converter Online

Make a discord bot and host with Heroku

You can check our discord_bot.py codes here: quantum-ugly-duckling/quantum-ugly-duckling-main/discord_bot.py.

We choose Heroku for hosting our bot.
For uploading codes, we used Heroku CLI.
Install it using the code below.

curl https://cli-assets.heroku.com/install-ubuntu.sh | sh

After installing it, follow the codes below.


❗ Before following

Check that 4 files are in the folder.

  1. config.json
  2. requirements.txt
  3. Procfile
  4. runtime.txt

If runtime.txt doesn’t exist, you can meet RuntimeError.

error

You can follow all logs of your deployed app using heroku logs --tail.


heroku login -i
heroku git:clone -a <your app name>
cd <your app name>
git add .
git commit -am "<commit message>"
git push heroku master

Now you can see your bot is working well!

working

If you are interested in our qadjoke bot, invite our lovely duck to your Discord server!


💬 Any comments and suggestions will be appreciated.

Leave a comment