tbot-generator v0.0.7
tbot-generator
A generator for Telegram Bots in nodeJS
Installation
Install the package globally :
npm install -g tbot-generator
Usage
Go to your project folder :
cd myProjectFolder/
Make sure to have a package.json
file, if you havn't use the following command :
npm init
To run it you'll need a version of node over 6.3.1, if you don't have it you can upgrade your node
version using nvm doing the following :
echo 6.3.1 > .nvmrc
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.4/install.sh | bash
nvm install
nvm use
To generate your bot use the command :
tbot-gen
Follow the instructions.
Your package.json
file has been updated, so install the new packages with :
npm install
Your bot is now ready to run !
Run it with :
node app/bot.js
Generation
During the generation you must give your Bot Key that you can get from @BotFather.
Basic Bot
No particularity for this bot, just basics.
The structure of this bot is :
app/ --> all of the source files for the bot
bot.js --> entry point, contains the router
config.template.json --> example versionned file for the configuration
config.json --> configuration file, not versionned
controllers/ --> all the controllers
HelloWorldController.js --> A Controller that replies "Hello World !"
PingController.js --> A Controller that replies "ping" or "pong"
.gitignore --> ignore 'app/config.json' file
This bot replies to :
- the command
/ping
, replies 'pong' - the command
/pong
, replies 'ping' - the word
hello
, replies 'Hello World' - any other message by default with 'Default Message'
Bot with MongoDB
This bot is the same as the Basic Bot but having MongoDB set up.
If you don't have MongoDB, you can get it from here.
Make sure to have the server up and running before starting the bot.
The structure of this bot is :
app/ --> all of the source files for the bot
bot.js --> entry point, contains the router and DB connection
config.template.json --> example versionned file for the configuration
config.json --> configuration file, not versionned
controllers/ --> all the controllers
HelloWorldController.js --> A Controller that replies "Hello World !"
PingController.js --> A Controller that replies "ping" or "pong"
MessageController.js --> A Controller that manage Message in the DB
models/ --> all the DB Models
MessageModel.js --> A Message MongoDB Model
.gitignore --> ignore 'app/config.json' file
This bot replies to :
- the command
/ping
, replies 'pong' - the command
/pong
, replies 'ping' - the command
/save myMessage
, which save the Message 'myMessage' in the database - the command
/list
, which displays all the Messages stored - the command
/clear
, which remove all the Messages stored - the word
hello
, replies 'Hello World' - any other message by default with 'Default Message'
Programming
The structure depends on the bot you are using and are explained above for the Basic bot and the MongoDB Bot.
To program please use the following documentation :
- For the communication with the Telegram API and understanding the bot structure, read telegram-node-bot documentation
- To use the MongoDB database, explore the mongoose website