TL;DR

This article shows an example of how to implement an e-health challenge using a Bot for Telegram Messenger based on a Node.js server running on a Raspberry Pi. The Bot brings a challenge in existing group chats and it is meant to motivate colleagues to do some workout whenever they crave candy or go for a coffee. On the technical side: It listens to predefined commands and is able to trigger time-based events.

pullup_bot_example

Why introducing a stay fit challenge?

At the moment the e-health market is getting more and more popular, one can say the whole world is obsessed by tracking steps, workouts and calories. Most fabricants say reaching 10.000 steps per day is the goal to stay fit and make sure one moves enough. Especially if you are working in an office, most of the time you are sitting in front of your computer which is not quite good for your body. But keeping an eye on your movements all day can be quite difficult for various reasons. To overcome one’s weaker self after work and doing some sports is not always easy. So I thought about how we can use the eight hours per day at work more efficient to do something good for our bodies. The solution: A group challenge in the office with gamification elements. Every colleague plays against the others and at the end of every week the most active one gets rewarded. Such a challenge brings many advantages: As it is a group challenge, the atmosphere at work gets more co-operative. Doing some exercises every now and then helps to regain energy after a mentally exhausting task and – the most obvious reason – it contributes to everyone’s health. It can even lead to fewer sick days caused by insufficient movement and increase productivity due to compensation of mental effort. In our case, I implemented a pullup challenge using a Telegram Bot account for me and my workmates, but of course many other exercises are possible, even combining multiple ones in a single challenge is thinkable.

What the Bot does

  • The whole group has a single account were all pullups are registered.
  • Everyone can do some pullups and increase the group account balance.
  • Also everyone has a personal account balance where his or her pullups were registered.
  • If someone consumes things (coffee, sweets, etc.) the group balance decreases by a specified amount.
  • The goal is to never reach a negative account balance at the end of the day.
  • Every evening, the group member with the most pullups is announced.
  • At the end of every week, the Bot generates a ranking comparing personal pullup balances and resets the stats.
 

Setup

Telegram provides a useful API for software-controlled user accounts called Bots. These accounts can be used in groups or in a private chat to fulfill a variety of automation tasks like implementing a continuous challenge in your work group or doing tasks remotely. Node.js allows us to easily setup a server which is able to run our Bot. In our specific case we wanted a challenge in our group chat where everyone gets motivated to move more often during work and to add some gamification into our workday.

So the first step is to create a Bot-Account using BotFather and configure it. Important:
  • You will receive an API Token in the last step which is required to authenticate your application.
  • Make sure you have Node.js and npm installed.
  • Additionally we need to install node-telegram-bot-api as a dependency ( npm install node-telegram-bot-api --save ).
Creating a new Bot is now simple as follows:  

Listen to commands

The Bot now gets every message sent to it (or sent in a group chat) and processes its routine. Telegram allows us to teach the Bot some commands to process different tasks. So we can implement various commands for committing pullups, consuming things (coffee, sweets, fruits,…) and so on.

Remember: Command messages should always start with a “/”. The Bot is now ready to receive your commands, run the file with Node.js and send some messages to it!

Use custom keyboards

custom_keyboard To get more control of what input is send to our Bot, we can make use of custom and inline keyboards. We will focus on inline keyboards here. They can be added to every message, the Bot sends and as soon as the user clicks one of them, the Bot gets notified. Perfect! Now we are able to determine the things that can be consumed.  

Implement daily routines

Now we need to implement the daily routine to check, if the group account balance is not negative. Thanks to Node.js we can use node-cron for that. Tasty! So this is what our daily routine looks like in general:  

What if I want my bot to be private?

Although in Telegram there is no official bot list, all Bots can be found by searching its username. As a consequence, everyone is able to interact with the Bot. Telegram does not provide a setting here to make Bots private only, which means you have to do this on your own. The fact that the Bot appears in the global search list is immutable but we can set explicitly which user accounts or chats can interact with it. Every message our Bot receives contains the sender ID and the group chat ID (if the Bot is in a group). All the Bot needs to do now, is to check if the message comes from a known user/chat. Quite simple, isn’t it? 😉  

Setup MongoDB

Last but not least we need a backend where we can perform CRUD (Create, Read, Update, Delete) operations, otherwise all data would be lost, whenever the bot crashes. To perform CRUD operations, we use mongoose, an object modeling package for Node.js that works basically like an ORM. To setup MongoDB the following steps should be performed:
  1. Download and install the MongoDB Node.js driver from here.
  2. Run  npm install mongoose --save
  3. Define your schema:
  4. Now we are ready to connect to the Database
 

Run both on the Raspberry Pi

Great! We can start our Bot with Node.js now and send messages to it. But if we shutdown our PC the Bot will stop working, too. How to solve that in a cool way? We found a Raspberry Pi in the basement! And because our Bot should interact with just a single group chat, we could be sure not to deal with a huge number of queries.

In our case, we installed Node.js on the Raspberry Pi, transferred our Bot files to it and gave it a go. To copy your files, you can use the scp protocol: scp -rv my_file.js pi@192.168.0.1:/path/to/destination

MongoDB can be installed by simply: $ sudo apt-get install mongodb-server

The Bot runs on an independent machine and our tiny “server” costs almost nothing.

 

Summary

The digital transformation in the past few years leads to an increasing rate in office workstations and therefore causes a lower amount of movement at work. To prevent negative long-term effects should be a important for every company nowadays. People are spending a significant amount of time at work, so keeping an eye on a healthy work environment is important. Surely, ergonomic office chairs, mice and keyboards as well as height adjustable desks act supporting, but they cannot replace body movement and exercises completely. Therefore introducing a challenge on top of ergonomic gadgets can support a healthy amount of movement during the workday and keep a company’s employees healthy and fit.

Related links:    

Leave a Reply