Integrating Telegram Bots
with Rails
Chris Downey
cdowney@protonmail.com
Please install Telegram on your phone or laptop for
hands-on experience during the presentation
https://telegram.org/
it will also be more interesting when we get into managing subscriptions
1
Create a DCRug Telegram bot
Bots are third-party apps that run
inside Telegram.
Our Rails app controls the bot,
providing the content, managing
subscribers, interactions, and
broadcasts.
Our bot will send DCRug event listings
2
Create a DCRug Telegram bot
Start by using a bot to create your bot
3
Now that DCRug bot is created,
how do we communicate with it?
4
With bots, we can have 2 types of communication:
1. Subscriber initiated
2. Bot (Rails app) initiated
5
Subscriber initiated communication
6
Bot (Rails app) initiated communication
7
Capture real-time Telegram bot events
Just for fun: first article on web hooks (may have coined term):
http://progrium.com/blog/2007/05/03/web-hooks-to-revolutionize-the-web/
8
We will setup DCRug bot with a webhook, and use
ngrok to create a secure tunnel to my dev machine
and to inspect the webhook payload
9
10
Setup dev machine to accept webhooks from
Telegram with ngrok
11
If you have Telegram installed, please subscribe
Via browser: http://telegram.me/DCRug_bot
Via Telegram: go into “Chats” and search for DCRug
(please call out if this isn’t working for you)
secret command /count provides number of subscribers
12
What just happened?
Our bot (Rails app!):
• intercepted a webhook
• extracted the bot command
• created a subscriber in the database
• sent a response to the new subscriber
13
Intercept a webhook
14
Extract bot command from webhook payload
15
Create a subscriber in the database
16
Send response to the new subscriber
use telegram-bot-ruby gem to wrap and simplify Telegram API
17
18
Pop Quiz!
how can we broadcast DCRug events,
right now, to all active subscribers?
Broadcast DCRug events to all subscribers
19
The End
Questions, comments, thoughts?
20
(See me after the meeting for an example of another very different bot if interested, it
enables searches across 2 websites and a database of tweets going back 6 years)
A bot serving a small
educational non-profit
https://telegram.me/VaxCalc_bot
Searches a database of more than 1,100 “foundational
knowledge” tweets produced since 2010
Combines results with custom Google search of 2
content-heavy websites with articles going back 30 years
good search: allergy
21
(to be fixed) Subscribers receive 4:10 pm notification of “trending” tweet past 24 hours

Integrating Telegram Bots with Ruby on Rails

  • 1.
    Integrating Telegram Bots withRails Chris Downey cdowney@protonmail.com Please install Telegram on your phone or laptop for hands-on experience during the presentation https://telegram.org/ it will also be more interesting when we get into managing subscriptions 1
  • 2.
    Create a DCRugTelegram bot Bots are third-party apps that run inside Telegram. Our Rails app controls the bot, providing the content, managing subscribers, interactions, and broadcasts. Our bot will send DCRug event listings 2
  • 3.
    Create a DCRugTelegram bot Start by using a bot to create your bot 3
  • 4.
    Now that DCRugbot is created, how do we communicate with it? 4
  • 5.
    With bots, wecan have 2 types of communication: 1. Subscriber initiated 2. Bot (Rails app) initiated 5
  • 6.
  • 7.
    Bot (Rails app)initiated communication 7
  • 8.
    Capture real-time Telegrambot events Just for fun: first article on web hooks (may have coined term): http://progrium.com/blog/2007/05/03/web-hooks-to-revolutionize-the-web/ 8
  • 9.
    We will setupDCRug bot with a webhook, and use ngrok to create a secure tunnel to my dev machine and to inspect the webhook payload 9
  • 10.
  • 11.
    Setup dev machineto accept webhooks from Telegram with ngrok 11
  • 12.
    If you haveTelegram installed, please subscribe Via browser: http://telegram.me/DCRug_bot Via Telegram: go into “Chats” and search for DCRug (please call out if this isn’t working for you) secret command /count provides number of subscribers 12
  • 13.
    What just happened? Ourbot (Rails app!): • intercepted a webhook • extracted the bot command • created a subscriber in the database • sent a response to the new subscriber 13
  • 14.
  • 15.
    Extract bot commandfrom webhook payload 15
  • 16.
    Create a subscriberin the database 16
  • 17.
    Send response tothe new subscriber use telegram-bot-ruby gem to wrap and simplify Telegram API 17
  • 18.
    18 Pop Quiz! how canwe broadcast DCRug events, right now, to all active subscribers?
  • 19.
    Broadcast DCRug eventsto all subscribers 19
  • 20.
    The End Questions, comments,thoughts? 20 (See me after the meeting for an example of another very different bot if interested, it enables searches across 2 websites and a database of tweets going back 6 years)
  • 21.
    A bot servinga small educational non-profit https://telegram.me/VaxCalc_bot Searches a database of more than 1,100 “foundational knowledge” tweets produced since 2010 Combines results with custom Google search of 2 content-heavy websites with articles going back 30 years good search: allergy 21 (to be fixed) Subscribers receive 4:10 pm notification of “trending” tweet past 24 hours