Yesterday, I announced the first release of a "Telegram Computer Trivia Bot" in this post:
Play Computer Trivia on Telegram
In this post, I will provide a high level overview of the application.
The main components in this application are:
- Node-RED
- LAMP (Linux, Apache2, MySQL, PHP)
- Telegram (the messaging app)
The key Node-RED modules / nodes used:
- exec : runs a system command and returns its output
- node-red-contrib-telegrambot
Please note, I
do not use the Node-RED nodes from
node-red-node-mysql because I find this approach less flexible (and more buggy) than just writing the queries in javascript in a Node-RED function. However, you are free to use the
node-red-node-mysql node if that moves your spirit and fits your style.
Here is the highest level overview of the current bot flow in Node-RED in this app. The little white squares are called "Link Nodes" and these are nodes which allow us to control inputs and outputs between flows and busy areas (so we can keep the actual flows easier to read and manage, and keep the wiring between nodes easier to read when there is a lot going on):
Next, is a screenshot of the trivia game play flow of this Telegram bot application:
In a future post, I will add some PHP and Javascript code used in the app, but in this post, I want to keep it high level.
So, in a nutshell and at a high level:
- The Telegram receiver listens for incoming Telegram messages on the designated "bot channel".
- All valid messages are logged in a MySQL DB.
- The valid messages go to a "big switch" node which uses a REGEX on the command strings to route them appropriately.
- Commands are processed using either a shell "exec" node or in a JS functions I wrote.
- Most of the input / output (but not all) are JSON formatted messages.
- The message replies from the APIs (PHP scripts) are formatted and sent back the sender.
The heart of the trivia game bot is illustrated in this second screenshot where:
- Requests for a new trivia question is checked against the DB calling a PHP script, which in turn, does all the MySQL queries to the trivia DB tables.
- If there are no unanswered questions for the player, a new question is retrieved and send to the user. Otherwise, the player is reminded to answer the unanswered question.
- When the player responds true or false, their answer is sent to either one of two PHP scripts which processes true or false, updated the DB, formats the reply and returns it to the player.
- If the player answers true or false when all questions have been answered, they are reminded to request another trivia question.
- Players can also check their score in a similar manner (there are no public leaderboards in this app, unlike the web-based version on the site).
Of course, as all experienced programmers know, there are myriad ways to build a Telegram bot and a trivial game like this in Node-RED; so let me tell you why did it this way. My way may not work the best for you.
When the MySQL DB is on the same server as the Node-RED application (as it is in this case) I prefer to do the DB query and all the "heavy lifting" in PHP. This provides me more error checking and more flexibility than doing the MySQL queries directly from Node-RED. This method is also easier to debug. However, we already have a ton of operational PHP code, so it "fits" our LAMP backend. If you don't have a big LAMP application running already, this method may not be appropriate.
I started using this "exec a PHP script to talk to MySQL" a bit later in the process of building this app, so there are still a few Javascript function (mysql query) nodes in the flow. These could easily be converted to an "exec a PHP script to talk to MySQL" or the other way around. Everyone has their own preferences (and I am not in the "clean-it-up" stage at this time).
As a side note, I have noticed from participating in the excellent Node-RED forum that a few very experienced Node-RED people are a bit "religious" about Node-RED nodes and some folks will spend a lot of time trying to make a "node" work when a javascript function or shell exec is faster to code and works just as well. Sometimes, I read posts like
"why don't you do this in this node or that node", when a few lines of Javascript or a shell exec works just as well.
From my perspective, I see a some good people miss the point about system engineerings and the true beauty of Node-RED as a systems integration, visual programming tool; because, as we often see, some folks are more worried about "node purity", or their "religious view of Node-RED" versus embracing the fact that we don't need to get to "wrapped around the axel" over "nodes".
Node are good. Coding is good. It's all good. That's my view. I don't have any religion about Node-RED (or any tech).
My advice / recommendation is to use "nodes" when they are convenient and work well for you; but don't waste your application development time trying to force a "node" to do a function which can easily be done another way. Time is precious. Don't waste it trying to force square pegs into round holes.
In addition, if you keep seeing bugs in a node / flow and cannot get a particular "node" to work as you expect, my advice is to quickly "move on" and write your own code / function / methods. That is what I do, and I can now prototype, build and test applications in a very short time period.
Node-RED is a great tool and I really love it. However, I do not get stuck in the "we must use a pre-packaged node" mode of thinking, and realize that many tasks can be also done in any of my (your) favorite programming languages and integrated into Node-RED.
After all, that is the true beauty of a system engineering / integration tool like Node-RED, right?
The downside is that you might find yourself on the opposite end of the barb from an experienced Node-RED lover / purist who sees all Node-RED tasks as a "third party node" application, but don't let that discourage you.
Code in your own style and enjoy!
Later on, I'll post some of the JS function code and some of the PHP scripts I used for this application, which is currently running and operational, as you can see from this earlier post.
Play Computer Trivia on Telegram
I am a bit busy with other tasks the rest of this week, but I'll try to find some time to color in more details in some follow-up posts.
Cheers!