Ruby Telegram Bot boilerplate
UPD (20.06.2016)
I've created an awesome list of resources related to bots (from tutorials and SDKs to the events and people). Be sure to check it out!
If you want to use Webhooks API instead of long-polling, be able to save state and create more scalable and powerful bot read the article below.
Full guide on creating statefull Telegram bot
Features
- Ability to save some data to a local database (Postgres by default)
- Automatic logging of received and sent message
- Easy internationalization using i18n gem
- Already created class for creating custom keyboards
- Database logging
- Separate classes for all the functional, so it's very easy to customize something
Usage
Defining responses
Use the on
method in message_responder.rb
like in the example below:
def respond
on /^\/start/ do
answer_with_greeting_message
end
on /^\/command (.+)/ do |arg| #supports up to two arguments but it is easily extendable
# do your stuff
end
end
Running the bot
For the first you need to install gems required to start a bot:
bundle install
Then you need to create secrets.yml
where your bot unique token will be stored and database.yml
where database credentials will be stored. I've already created samples for you, so you can easily do:
cp config/database.yml.sample config/database.yml
cp config/secrets.yml.sample config/secrets.yml
Then you need to fill your Telegram bot unique token to the secrets.yml
file and your database credentials to database.yml
.
After this you need to create and migrate your database:
rake db:create db:migrate
Great! Now you can easily start your bot just by running this command:
bin/bot
Directory layout
Source
└── ruby-telegram-bot-boilerplate
├── bin # executables folder
│ └── bot # main executable file
├── config # folder with configs
│ ├── database.yml.sample # sample database configuration
│ ├── secrets.yml.sample # sample credentials file
│ └── locales.yml # file with i18n locales
├── db # database related stuff
│ └── migrate # migrations
│ └── 001_create_users.rb # migration for creating table 'users'
├── lib # helper libs folder
│ ├── app_configurator.rb # class for application configuration
│ ├── database_connector.rb # class for connecting to database
│ ├── message_responder.rb # main class for responding to message
│ ├── message_sender.rb # simple class just for message sending
│ └── reply_markup_formatter.rb # class for creating custom keyboards
├── models # database models folder
│ └── user.rb # active record User model
├── Gemfile # Gemfile
├── Gemfile.lock # Gemfile.lock
├── README.md # Readme file
└── Rakefile # Rakefile with tasks for database management
Some more specific info you can also find here.
Contributing
If you have some proposals how to improve this boilerplate feel free to open issues and send pull requests!
- Fork it
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request