[SockoBot] Adding Database Functionality to the messenger bot.

in utopian-io •  7 years ago  (edited)

sockobotlogo.jpg

SockoBot is trying to be the only tool you'll ever need on your Discord Server or Facebook Page when it comes to steem, while also being easily expandable to anyone that knows a bit of Python.

SockoBot FB

github repository
author

Languages

  • Python 3.6
  • SQL

New Features

What feature(s) did you add?

In this important update, the whole reason for porting SockoBot to a more personal environment - Facebook's messenger - is about to show. A database (which for the purpose of this bot is SQLite, but can easily be expanded to any other) was introduced, which allows a lot of future functionality. For now, thanks to it 4 new functionalities were provided:

  • The register command was added, which allows the user to tie his messengerID (which has no connection to their facebook account - so their personal information doesn't get stored anywhere) to any steem account by just providing its username.
  • The unregister command was added, which allows the user to remove his messengerID from the database, as well as register under a new nickname.
  • The vote command now automatically pulls the username from the database if the user doesn't provide any other username as an argument.
  • The payout command now automatically pulls the username from the database if the user doesn't provide any other username as an argument.

On top of that, some other small changes were made:

  • The bot will now correctly reply with "Too few arguments provided" if a command requires an argument, but didn't receive one.
  • The calculate_estimated_upvote will now correctly calculate the upvote if the user delegates some of his SP.
  • The command function now takes user_id as an argument, which is supposed to be used with the messengerID.

How did you implement it/them?

A total of 4 functions were added, and their returns were used in the command function. They are all a mix of Python 3 and SQL.

def data_entry(username, user_id):
    c.execute('CREATE TABLE IF NOT EXISTS users(unix REAL, datestamp TEXT, username TEXT, user_id INTEGER)')
    unix = time.time()
    datestamp = str(datetime.datetime.fromtimestamp(unix).strftime('%Y-%m-%d %H:%M:%S'))
    c.execute("INSERT INTO users (unix, datestamp, username, user_id) VALUES (?, ?, ?, ?)", (unix, datestamp, username, user_id))
    conn.commit()

The data_entry function pairs the messengerID and the provided username in the database, giving it an UNIX and a datestamp as well.

def data_check(user_id):
    c.execute("SELECT user_id FROM users WHERE user_id=?", (user_id,))
    data = c.fetchall()
    if not data:
        return False
    else:
        return True

The data_check function checks if the user_id is already stored in the database and returns True or False

def data_removal(user_id):
    c.execute("DELETE FROM users WHERE user_id = ?", (user_id,))
    conn.commit()

The data_removal function removes the messengerID and the whole row tied to it from the database.

def data_name_check(user_id):
    row = c.execute("SELECT username FROM users where user_id = ?", (user_id,)).fetchone()
    username = row[0]
return username

The data_name_check function returns the username that the user is registered under.

Finale

On top of that, the command function has changed a lot to welcome these changes. All the changes can be found in these commits: 61d0ac5 , 8518453, ec2f039;

The code was properly commented in the last commit and prepared for future improvements.



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
  ·  7 years ago 

Thank you for the contribution. It has been approved.

Is it safe/ok to expose the PAGE_ACCESS_TOKEN here?

You can contact us on Discord.
[utopian-moderator]

It is safe as it can only be used if the bot is registered to a webhook using developers.facebook.com, which is tied to my personal Facebook account. That being said, just to be sure, I'll be changing the page once the development process is over. Thank you for your concern!

Hey @jestemkioskiem I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x