BernhardSchlegel / BierBot

Raspberry Pi powered, Brewing Software for iOS, Android and Desktop (Mash and Ferment)
Other
17 stars 5 forks source link

Started implementing Telegram bot #14

Closed jonathanschneider closed 3 years ago

jonathanschneider commented 3 years ago

So I was wondering how BierBot could let a user know that the brew is finished or user action is required when the user isn't within hearing distance. I thought a Telegram bot might be a great idea so I started implementing it.

What I've done so far:

If you're interested, this is still missing because I haven't quite figured out completely how everything plays together:

Since this is not yet function you might not want to merge it into your master branch. If you're interested and want to create a develop branch I will create another pull request.

BernhardSchlegel commented 3 years ago

Haha, how awsome is that?! I created a develop branch, but feel free to commit to your PR and we merge straight into master as soon as it's ready. Here are some entry points for your questions:

  1. Add fields to settings page: have a look at Settings.html, you can pretty much copy & paste from a different section. The settings object itself is transmitted "as whole" to the nodeJS backend (no need to adjust the socket). The logic resides in the (way too big, I know...) app.js file, starting line 1218.
  2. Storing resolves pretty much along with the first issue / question. As soon as the token, chatID, and telegram_enabled-state are fields of the settings object, storing will work just fine (it's noSQL mongo db). Just do not forget to add a field in the mongoose object appsettings.js
  3. That's easy, there is one global option in in server.js, called setNextStep, integrate it somewhere there, but ...
  4. do not forget to condition on appSettings.telegram_enabled (as you said :) )

Feel free to reach out again, anytime!

Cheers

jonathanschneider commented 3 years ago

@BernhardSchlegel any idea if updating the request package to v2.88.2 will break something?

jonathanschneider commented 3 years ago
Screenshot 2021-01-13 at 20 39 43
BernhardSchlegel commented 3 years ago

@BernhardSchlegel any idea if updating the request package to v2.88.2 will break something?

There is a fair chance that it will work since the major version stays the same. Give it a try and let me know. I'm happy to help with debugging if something goes sideways...

jonathanschneider commented 3 years ago

Okay, I added the required settings to the front end, created the model and set default settings. I am not sure how to get the settings from the data base in telegram.js. I wanted the Telegram implementation in its own file even though it isn't much code. Since all settings are already available in server.js would it make more sense to implement Telegram there than in its own file? Could you have a look?

BernhardSchlegel commented 3 years ago

Sure, I'm happy to! Putting it in a separate file is cleaner way to go - even if it's just a few lines.

How I would do it:

Backup: If importing the Settings module in telegram.js won't work, you can pass the a settings object to the telegram::init() function on every call. In this case, you cannot delete the token and chatId variables.

jonathanschneider commented 3 years ago

Thanks for the hints and the idea with the init function @BernhardSchlegel. That was really helpful. I managed to access the settings in telegram.js but after implementing the init method, I decided to just go with your backup plan. Wherever the init method is called, the settings are "right there", so why not just pass them as arguments?

I believe I am done with this enhancement then. Check it out.

BernhardSchlegel commented 3 years ago

I'll have a look at the weekend !

jonathanschneider commented 3 years ago

So, I brewed my first beer with BierBot yesterday and the Telegram messages came in really handy. Since currently messages are only sent when BierBot enters a manual step I added a few more manual steps to my recipe just to go the reminders. I now think it’d be better if BierBot sent messages on every step transition. What do you think?

BernhardSchlegel commented 3 years ago

Hi Jonathan! I think that's a great idea. Can you open a new PR for this (I merged this one as of today)? Also, as I tested the telegram integration today I noticed two things: 1st: it worked pretty great out of the box 2nd: The option to provide multiple chat ids would come in super handy (we are a group of 4 hobby brewers). A simple way to implement it would be to allow CSV in the SettingsPage (chatID1,chatID2) and then do a simple settingsObject.chatID.split(',') to iterate over all chat ids (nice side effect of split is, that if there is no , at all in the string, it returns (still) an array of size 1). Let me know what you think!

jonathanschneider commented 3 years ago

I’ll look into it. In the meantime you could also create a Telegram group between the four of you (and IDBot) and provide the group‘s chat ID.

Am 23.01.2021 um 16:35 schrieb Bernhard Schlegel notifications@github.com:

 Hi Jonathan! I think that's a great idea. Can you open a new PR for this (I merged this one as of today)? Also, as I tested the telegram integration today I noticed two things: 1st: it worked pretty great out of the box 2nd: The option to provide multiple chat ids would come in super handy (we are a group of 4 hobby brewers). A simple way to implement it would be to allow CSV in the SettingsPage (chatID1,chatID2) and then do a simple settingsObject.chatID.split(',') to iterate over all chat ids (nice side effect of split is, that if there is no , at all in the string, it returns (still) an array of size 1). Let me know what you think!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

jonathanschneider commented 3 years ago

Plus the bot you created of course!

Am 23.01.2021 um 17:00 schrieb Jonathan Schneider jonathan.schneider@gmx.de:

 I’ll look into it. In the meantime you could also create a Telegram group between the four of you (and IDBot) and provide the group‘s chat ID.

Am 23.01.2021 um 16:35 schrieb Bernhard Schlegel notifications@github.com:

 Hi Jonathan! I think that's a great idea. Can you open a new PR for this (I merged this one as of today)? Also, as I tested the telegram integration today I noticed two things: 1st: it worked pretty great out of the box 2nd: The option to provide multiple chat ids would come in super handy (we are a group of 4 hobby brewers). A simple way to implement it would be to allow CSV in the SettingsPage (chatID1,chatID2) and then do a simple settingsObject.chatID.split(',') to iterate over all chat ids (nice side effect of split is, that if there is no , at all in the string, it returns (still) an array of size 1). Let me know what you think!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

BernhardSchlegel commented 3 years ago

ahhh ! Didn't know that, my feature request does not make soo much sense than after all

jonathanschneider commented 3 years ago

I don’t know. With your suggestion you could just change the chat IDs for the brewers present on that day without having to edit the Telegram group. Since it has no disadvantage for single chat IDs, why not? I’ll look into it.

Am 23.01.2021 um 17:08 schrieb Bernhard Schlegel notifications@github.com:

 ahhh ! Didn't know that, my feature request does not make soo much sense than after all

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

BernhardSchlegel commented 3 years ago

I appreciate that!