[SockoBot] Adding Database Functionality to the messenger bot.
utopian-io·@jestemkioskiem·
0.000 HBD[SockoBot] Adding Database Functionality to the messenger bot.
 >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](https://github.com/Jestemkioskiem/steem-sockobot-fb) [author](https://github.com/Jestemkioskiem/) # 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**. ```python 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. ```python 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``` ```python 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. ```python 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](https://github.com/Jestemkioskiem/steem-sockobot-fb/commit/61d0ac5a785e1d4a9d1b01284506c94b8d869c71) , [8518453](https://github.com/Jestemkioskiem/steem-sockobot-fb/commit/85184535f91b52b909bbdf66092b8a67f93e3832), [ec2f039](https://github.com/Jestemkioskiem/steem-sockobot-fb/commit/ec2f039442049759a2663eeab914c990f71e0b3d); The code was properly commented in the last commit and prepared for future improvements. <br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@jestemkioskiem/sockobot-adding-database-functionality-to-the-messenger-bot">Utopian.io - Rewarding Open Source Contributors</a></em><hr/>