Integrating Telegram Bots with Ruby on Rails

21
Integrating Telegram Bots with Rails Chris Downey [email protected] 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

Transcript of Integrating Telegram Bots with Ruby on Rails

Page 1: Integrating Telegram Bots with Ruby on Rails

Integrating Telegram Bots with Rails

Chris Downey [email protected]

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

Page 2: Integrating Telegram Bots with Ruby on Rails

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

Page 3: Integrating Telegram Bots with Ruby on Rails

Create a DCRug Telegram bot Start by using a bot to create your bot

3

Page 4: Integrating Telegram Bots with Ruby on Rails

Now that DCRug bot is created,how do we communicate with it?

4

Page 5: Integrating Telegram Bots with Ruby on Rails

With bots, we can have 2 types of communication:

1. Subscriber initiated2. Bot (Rails app) initiated

5

Page 6: Integrating Telegram Bots with Ruby on Rails

Subscriber initiated communication

6

Page 7: Integrating Telegram Bots with Ruby on Rails

Bot (Rails app) initiated communication

7

Page 8: Integrating Telegram Bots with Ruby on Rails

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

Page 9: Integrating Telegram Bots with Ruby on Rails

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

Page 10: Integrating Telegram Bots with Ruby on Rails

10

Page 11: Integrating Telegram Bots with Ruby on Rails

Setup dev machine to accept webhooks from Telegram with ngrok

11

Page 12: Integrating Telegram Bots with Ruby on Rails

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

Page 13: Integrating Telegram Bots with Ruby on Rails

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

Page 14: Integrating Telegram Bots with Ruby on Rails

Intercept a webhook

14

Page 15: Integrating Telegram Bots with Ruby on Rails

Extract bot command from webhook payload

15

Page 16: Integrating Telegram Bots with Ruby on Rails

Create a subscriber in the database

16

Page 17: Integrating Telegram Bots with Ruby on Rails

Send response to the new subscriber

use telegram-bot-ruby gem to wrap and simplify Telegram API

17

Page 18: Integrating Telegram Bots with Ruby on Rails

18

Pop Quiz!

how can we broadcast DCRug events, right now, to all active subscribers?

Page 19: Integrating Telegram Bots with Ruby on Rails

Broadcast DCRug events to all subscribers

19

Page 20: Integrating Telegram Bots with Ruby on Rails

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)

Page 21: Integrating Telegram Bots with Ruby on Rails

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