Working alone is so exhausting so I created my own assistant
Tags: javascript, node, codenewbie, showdev
Published: 2022-03-15 02:18:47



Working alone is great... but... I am so tired of dealing with all of these.. these shitty uninspiring, repetitive, tedious tasks...

I didn't realize how many emails I had to reply to and how many invoices I had to make on my own. How many tasks do I have to manage and schedule?!

I sometimes freaked out alone because I was too busy, and sometimes I freaked out because suddenly there's nothing to do..



```
Console.log( my_life )
```



I need something stabilizes me all the time no matter what happens to me.  Maybe I need a manager. Manage me! (Funny to say that because I left work to not be managed by others.LOL)

For a sec, I think about hiring somebody, but I don't have money for that.(of course)

So here is my attempt to create my personal assistant with discord and notion API and, what I learned from this project.

If there is anything incorrect, please let me know. I'd love to learn from you!

---


## 1. I learned NLP

The first thing in my head to create a chatbot is how a chatbot analyze the intention of my text. People say the same thing in various forms of expression

- How are you?
- How's it going?
- What's up?

I can write the code like `if( text.includes('how are you') )` for every expression, but how inefficient would it be? This is the time when NLP is useful.

>  Natural language processing (NLP) is **the ability of a computer program to understand human language as it is spoken and written** -- referred to as natural language. It is a component of artificial intelligence (AI). NLP has existed for more than 50 years and has roots in the field of linguistics. from [techtarget.com](https://www.techtarget.com/searchenterpriseai/definition/natural-language-processing-NLP#:~:text=Natural%20language%20processing%20(NLP)%20is,in%20the%20field%20of%20linguistics.)


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rd9sdc9ykfalnrb4i5r5.gif)

While I still don't fully understand how NLP works, my understanding is so far like this.

1. A developer submits a set of sentences future users might send

2. There are three types of variables A user can assign for each text.
 (1) intension: The most important part of the sentence. Only one can be assigned for one sentence. ex) I need a new iPhone! -> Request_add_wishlist
(2) Entities: This is a smaller assembly piece, basically a critical keyword to define the intention. ex) I need shopping! -> need, shopping.
(3) Traits : ( I'll ignore this part for now )

3. An NLP program analyzes what kind of entity composition is for specific intension.

4. Next time a user send complete new text, a trained NLP will spit out what a user means.

Among multiple different services to provide NLP, I chose to use wit.ai service, which is an open-source service developed by Facebook, Meta.

The useful thing about wit.ai is chatting text I sent by discord also showed up on the wit.ai training session, so even if a bot failed to understand what I meant, I can always go to wit and correct it.

The downside is documentation is not so clear, so... anyway... it's alright. I will take it...



---



## 2. I learned Discord.js

In the end, I chose to create a discord bot over a Slack bot even tho I am not really a discord user. 

Some servers I joined to get help usually has too many channels on the sidebar, and too many discussion is going on. It just intimates too much. Like you entered in a club and see full of people dancing a Michael Jackson thriller perfectly, but I don't know that damn dance. "Quick, get out from that club!"
![](https://media.giphy.com/media/Z4IXspU3iCHlK/giphy.gif)

Anyway, during this project, I could learn about discord.js and at the same time a little bit more about discord itself...

```
npm install discord.js
```

The exciting thing about discord js is it gives so many options to interact with other users. Can create buttons, slash commands, interact with emoji etc.. way more than just sending a message.

The most useful message type is **Embed Message**.


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ey6cm3ltlka1iy33xfa.JPG)

( I set my coordinator as Tahani from the TV series 'The good place' because I love her character on the show so much. so jolly!)




Embed message returns the beautifully formatted information. You can directly add the field like this.

```jsavascript
_embed.addFields({name : "Count", value : Count })
```

or you can create the dictionary and loop for each keys

```
Object.keys(style).forEach( k=>{
    _newEmbed.addFields({name : k , value :style[k].toString() })
})
```

The useful reference for discord bot -> [Discord JS Guide](https://discordjs.guide/)


---



## 3. I learned Notion API

I love notionHQ. Actually, I am obsessed with it. I've been using it since it was not the mainstream. Recently I heard it acquired automate.io. Alight, NotionHQ, Let's go! Bigger and bigger! But there were a few moments.. when I was considering leaving Notion... it's because Notion doesn't have a recurring task option!

Like I wrote in the intro, repeating and repeating and repeating tasks is already so boring, and **I have to create the checkbox of it every time?** Oh shit, I don't want that.

I left the Notion and came back again over and over, like a troubled teenager arguing with a parent. A year ago, Notion released notion API; "This is the final, guys. I am going to build an automation system for you and going to be with you happily ever after 4everr."

``` npm install @notionhq/client``` 

Notion API is great! but unfortunately, there are a few limits for (v1.0.3) right now.

1. Editing a block is not available yet. Basically, if you want to tick the checkbox block, you should delete the current checkbox item and append a duplicated block again. It's not impossible but kinda annoying. Duplicating a single block is not such a bad case, but how about if you want to duplicate a whole page? Sadly you still need to delete a whole page and duplicate it. If the page has a children element, the problem gets more annoying.

2. Get children to block only return the top-level items on the hierarchy.

   ```javascript
   var children =  await NOTION.blocks.children.list({ block_id: _block.id });
   ```

   so for example, if there's a notion block like this


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gich1zcv6zv64jz63xek.JPG)

   The code output will be only [block_A, block_B]. So even if you duplicate the parent with its children, [block_Aa, block_Ab, block_Ba,block_Bb] won't be duplicated.

   If you want all the children under **parent**, there's no other way than keep asking children if they has grand-grand children…	

   ​	

   ```javascript
   if( block_A.hasChildren ){
   	children.push( await NOTION.blocks.children.list({ block_id: block_A.id }) )
   }
   ```

---


3. Some block type is not supported. Just think non-native notion blocks such as embed tweet or embed Figma are not supported, which is totally fine, but. butttt. **code blocks** are also not supported, so I felt kinda sad. :(

All i want is block to save the code so I can run it by ‘eval()’ which can be just any text block. Just not pretty as I wished, that’s all. Haha

```
var scripts = blocks.filter( block => Object.keys(block)[0] == "callout"  )
await eval(scripts[0])
```




![Every lunch time, recipe inspiration function runs.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpi89knhb3cqhazg4bej.JPG)





---



## 4. I learned about Cron 

One of the must-have features for the coordinator bot was sending scheduled messages, so I can do the task on time. 

To do so, I got to know [Cron](https://www.npmjs.com/package/cron).

```
npm i cron
```


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7yuc6fgbrxh9b2r9q61d.gif)


>  A CRON expression is a string comprising five or six fields separated by white space[[16\]](https://en.wikipedia.org/wiki/Cron#cite_note-17) that represents a set of times, normally as a schedule to execute some routine. 
>  from [wikipedia](https://en.wikipedia.org/wiki/Cron)

***??... Another regular expression all over again.. horror..  But actually, the breakdown of cron time is pretty simple, `min + hour + date + month + week.`

Now I should generate cron time by ‘Date()’. This time, instead of writing in js, I wrote as notion built-in formula so even if I update the schedule by notion, the clon time value also can be updated at the same time.

```
min : 
empty(prop("Unit")) ? if(minute(prop("Date")) + minute(prop("Date")) == 0, format(minute(prop("Edited"))), format(minute(prop("Date")))) : if(prop("Unit") == "minute", "*/" + format(if(empty(prop("Recurring")), 1, prop("Recurring"))), if(prop("Unit") != "hour" or prop("Unit") != "minute", if(empty(prop("Date")), if(empty(prop("Date")), format(minute(prop("Edited"))), format(minute(prop("Date")))), format(minute(prop("Date")))), "*"))
```

Such a ugly code, for now but so far it seems working as wish. (full code is on my [repo](https://github.com/happping/discord-coordinator-bot#formula-for-clon-time))

While I can create a new scheduled event by Notion, I wished to create the new reminder by chat as well. Luckily Wit.ai has built-in entities called "duration" and "datetime", such a lifesaver! 


![Text to clean format](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2lhmx6t0r85czxg3mjm7.JPG)




---


## 5. I learned Puppeteer

I started to feel more greedy and needy. Hey, bot, you can give weather and different time zone time, my today's tasks and project due date now.. how about.. recommendation for today's meal?? M.M.M

First, I found the biggest recipe API called [Spoonacular API](https://spoonacular.com/food-api), but .. the food pictures look kinda.. not really inspiring..

So instead of food API, I decided to fetch the information directly from my favourite cooking site. I heard the Puppeteer is good for web scraping, I always wanted to try, but this is my first time trying Puppeteer!

```
npm i puppeteer
```

[Puppeteer](https://www.npmjs.com/package/puppeteer) is such an amazing package, but there were a few confusing parts though.

When I tried to get element by document.querySelector() .this returned `undefined` or `null` object. I was panicked, and I thought Puppeteer failed to find the element, but it's there.. when I specifically ask textContent or src, href?

```javascript
await page.evaluate(()=>{
	return document.querySelectorAll(".selector").map( sel => sel.textContent)
});
```

As I understand is what Puppeteer gives me is not an HTML element but something else..  ( is there anybody explain a bit easy for me? i am helpless lol)

Anyway, now I get a recommendation for a recipe. yay


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0dmynt42pq3dnm8l5miy.JPG)






## 6. I learned Heroku

Because my bot will be deactivated when I don't run bot.js, I needed Heroku to serve it on the cloud.

While I am hosting my sites on Netlify, I am still not familiar with the hosting service. I misunderstood Heroku and Netlify as the same kind of service, but this time, I learned they are not. Like this whole post is such lack of professional knowledge, this is my short understanding about Netlify and Heroku so far

- Netlify is more for the front-end, and Heroku is more for the back-end
- Netlify is always active, but Heroku fall asleep...zzz
- Netlify is site hosting online, but Heroku is run the script on Linux
- Netlify start charging by build time, Heroku charging with something called **Dyno hours** 

Dyno is a container that runs the command in Linux. The busier the app will be, the more dyno is required to be purchased. Because I am a free user, I have a single dyno. 

Free user has 1000 free dyno hours. It means that even if my app runs consistently without resting for the whole month, it wouldn't get over 730 hours. Free is always good. It makes me feel cosy.😊



---


## Concluding

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zn90ov16cuu8kr1ufz7v.JPG)

[My Git Repo](https://github.com/happping/discord-coordinator-bot)

While I want to improve more and add more features, my Tahani bot works quite wonderfully right now. Great to have a companion! (sad to say that. lol)

I tried a lot of new things this time, I learned so much; however, I also feel like I didn't spend enough time to learn about each of them more precisely. Maybe that's what I'm going to do with next dev.to

Besides having a customised virtual assistant for my daily routine, the knowledge I earned was so valuable. There are so much potential to improve [my interactive character project](https://www.instagram.com/reel/CHSAiMij_S7/?utm_source=ig_web_copy_link)
Thank you for reading this messy post. I am not a trained developer (I a visual artist), but I am very excited to share thoughts and get to know more about creating something cool. 

Please comment on anything if you want to correct my post and educate me. It would be awesome to learn from all of you!

![](https://media.giphy.com/media/3oxHQuruoQPY5ndyiQ/giphy.gif)