I'm not mantaining this project for a longggg while, feel free to fork or do a PR if you spot any issue. I hope it helps anybody out there who need a quick detailed guide to start with Telegram Bots.
This is an initial pilot project to create Telegram bots.
I'd like to share the project so it can help anybody else interested in getting into the development of bots for Telegram or any other platform.
I share my project under GPL License, so everybody can reuse and modify it.
This Bot is LIVE for Telegram under the name @GiveMeFoodBot.
But it's not intented to be used widely, due to the limitations on the implementation and deployment server I've choosen.
The Bot is running using a FREE Dyno on Heroku, which means it won't be up always, because FREE Dynos like sleeping .
In addition, it's in my plans to extend the bot further, so it will be for sure unstable in several moments.
All contributions and ideas are welcomed.
For the persons new to Ruby and BOTs, like me, here are some explanations about how I created the project from scratch.
I'll cover the following points, giving examples taken from this project:
-
Creating your bot in Telegram.
-
Creating the first ruby file for your bot.
-
Requiring and installing external ruby gems.
-
Generating Gemfile.
-
Generating Gemfile.lock.
-
Completing your first ruby file with meaningful code for running bot services.
-
Deploying your bot.
a. Registering to HEROKU.
b. Installing Heroku Command Line Interface (CLI), formerly HEROKU Toolbelt.
c. Creating your bot app in HEROKU using Toolbelt from the command line.
The first thing you need to do is to contact BotFather.
You can follow the link above for detailed explanations from the Telegram docs about how to create your app.
This will register your bot app with Telegram. At this moment is not required to have any code or anything ready, you just will tell BotFather that
you want a new bot to be created, he'll register the name you've choosen and will give you a TOKEN that is the first thing you need to connect
from your code to Telegram Bot API.
Here I'll present the minimum steps you need to perform at least.
- Start the bot tipying @BotFather in Telegram.
Or alternatively go to this URL and it will ask you if you want to start a conversation with botfather BOT FATHER URL
You can also start a new conversation, typing "botfather" as the name of the contact.
- Execute /newbot.
- Receive your TOKEN and save it.
- See more steps in the docs folder.
You can also query the bot father for more help with the /help command
Create an empty file in any aproppiate folder in your system. As example from this project:
../src/GiveMeFood.rb
We need to say which ruby gems we'll be using. Add the following lines at the beginning of GiveMeFood.rb
require 'telegram/bot'
require 'google_places'
In the case of this project, I'm using:
- Gem telegram-bot-ruby
- Gem google_places
gem 'google_places', '0.32.0'
gem 'telegram-bot-ruby', '~> 0.5.0.beta4'
RubyGems is a package management tool, if you don't have RubyGems installed in your system, follow this link and install it: https://rubygems.org/pages/download
After installing RubyGems, go ahead and install the gems required for this bot.
Open the terminal and type the following commands, one per time:
gem install google_places
gem install telegram-bot-ruby
The previous commands will install the gems in your system.
Go to the root folder of your ruby main file, GiveMeFoodBot.rb and execute:
bundle init
You will see a log like the next one:
In the same folder as the last step execute:
bundle install
You will see a log as the following:
See the code inside GiveMeFoodBot.rb as an example. Or check the github repos for the Ruby Telegram BOT API in Ruby I used to have an idea about what you can do.
These steps are specific to the server I've used for deploying my bot.
You can choose to deploy your bot anywhere else, on your personal PC is a good idea until you have it running, but you could also choose another server or a Raspberry Pi for example. Check out the useful links at the end of the README.
Go to HEROKU and sign up for a FREE account.
Go to Heroku Command Line Interface (CLI), download and install in your computer.
I did everything in OSX El Capitan with the command
brew install heroku
Here are the steps you need to follow using the Heroku CLI.
heroku login
The previous will install the CLI and store your credentials to make any future change with heroku to be already authenticated. See more information in the section Getting Started
heroku create 'yourAppName'
git clone 'yourRepoURLHerokuGAVEYou'
heroku buildpacks:set heroku/ruby
5. Go to your repo main folder, add all your code if it was not added before and execute push to the main repo.
The main repo can be renamed to heroku (the name you will see in all the documentation) but you can keep it to origin or anything else you want.
In my case I had not set the Ruby version. So I added it to my Gemfile:
ruby '1.9.3'
And I had not created a VERY IMPORTANT FILE SO YOUR BOT CAN RUN ON HEROKU so I created the Procfile.
To check your app is running do the following:
a. CHeck your app was created.
b. Check your app was recognized as a bot and the command you put in *Procfile* is OK.
c. Click EDIT and ENABLE your bot so it can start running.
d. CELEBRATE!! your bot is running.
In the doc folder you will see screenshots for all the steps and errors I got while creating my app in HEROKU using ToolBelt and
the troubleshooting I did for having my bot running in HEROKU.
Go there and check in order every image file you find. Are enumerated following the process I did.
#Useful links I've found in my way while creating the project
- Telegram Bot API
- BotFather: the first thing you need to do for creating your bot
- Ruby Language Tutorial
- BOT Wiki: a lot of resources for creating BOTS
- Heroku: Toolbelt
- Heroku: buildpacks
- Heroku: deploying with GIT
- Heroku: how to deploy your SLACK bots
- Bundler & RubyGems
- Quick instructions for creating a Telegram BOT in Ruby
- Example of a Twitter BOT
- Reddit question: where do you host your bots?
- Hosting your bots on a Raspberry Pi
- Python: where to host your bots