create account

[SockoBot] Adding Database Functionality to the messenger bot. by jestemkioskiem

View this thread on: hive.blogpeakd.comecency.com
· @jestemkioskiem · (edited)
$73.62
[SockoBot] Adding Database Functionality to the messenger bot.
![sockobotlogo.jpg](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516714139/dylwfwpmazxjxkgzvqzq.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](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/>
👍  , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorjestemkioskiem
permlinksockobot-adding-database-functionality-to-the-messenger-bot
categoryutopian-io
json_metadata{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":118528363,"name":"steem-sockobot-fb","full_name":"Jestemkioskiem/steem-sockobot-fb","html_url":"https://github.com/Jestemkioskiem/steem-sockobot-fb","fork":false,"owner":{"login":"Jestemkioskiem"}},"pullRequests":[],"platform":"github","type":"development","tags":["utopian-io","sockobot","python"],"users":["jestemkioskiem"],"links":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1516714139/dylwfwpmazxjxkgzvqzq.jpg","https://github.com/Jestemkioskiem/steem-sockobot-fb","https://github.com/Jestemkioskiem/","https://github.com/Jestemkioskiem/steem-sockobot-fb/commit/61d0ac5a785e1d4a9d1b01284506c94b8d869c71","https://github.com/Jestemkioskiem/steem-sockobot-fb/commit/85184535f91b52b909bbdf66092b8a67f93e3832","https://github.com/Jestemkioskiem/steem-sockobot-fb/commit/ec2f039442049759a2663eeab914c990f71e0b3d"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1516714139/dylwfwpmazxjxkgzvqzq.jpg"],"moderator":{"account":"justyy","time":"2018-01-26T20:15:30.684Z","reviewed":true,"pending":false,"flagged":false}}
created2018-01-26 15:52:21
last_update2018-01-26 20:15:30
depth0
children3
last_payout2018-02-02 15:52:21
cashout_time1969-12-31 23:59:59
total_payout_value51.570 HBD
curator_payout_value22.047 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,284
author_reputation41,292,066,961,817
root_title"[SockoBot] Adding Database Functionality to the messenger bot."
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,505,293
net_rshares10,131,478,233,127
author_curate_reward""
vote details (26)
@justyy ·
Thank you for the contribution. It has been approved.

Is it safe/ok to expose the PAGE_ACCESS_TOKEN   [here](https://github.com/Jestemkioskiem/steem-sockobot-fb/commit/85184535f91b52b909bbdf66092b8a67f93e3832)?

You can contact us on [Discord](https://discord.gg/uTyJkNm).
**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authorjustyy
permlinkre-jestemkioskiem-sockobot-adding-database-functionality-to-the-messenger-bot-20180126t201602390z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-26 20:16:06
last_update2018-01-26 20:16:06
depth1
children1
last_payout2018-02-02 20:16:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length330
author_reputation280,616,224,641,976
root_title"[SockoBot] Adding Database Functionality to the messenger bot."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,556,919
net_rshares0
@jestemkioskiem · (edited)
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!
properties (22)
authorjestemkioskiem
permlinkre-justyy-re-jestemkioskiem-sockobot-adding-database-functionality-to-the-messenger-bot-20180126t204317739z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-01-26 20:43:18
last_update2018-01-26 20:49:39
depth2
children0
last_payout2018-02-02 20:43:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length275
author_reputation41,292,066,961,817
root_title"[SockoBot] Adding Database Functionality to the messenger bot."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,562,254
net_rshares0
@utopian-io ·
### 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. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**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**
properties (22)
authorutopian-io
permlinkre-jestemkioskiem-sockobot-adding-database-functionality-to-the-messenger-bot-20180127t160539646z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-27 16:05:39
last_update2018-01-27 16:05:39
depth1
children0
last_payout2018-02-03 16:05:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,090
author_reputation152,955,367,999,756
root_title"[SockoBot] Adding Database Functionality to the messenger bot."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,773,471
net_rshares0