This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module BotCommand | |
| class Born < Base | |
| def should_start? | |
| text =~ /\A\/born/ | |
| end | |
| def start | |
| send_message("You have been just born! It’s time to learn some programming stuff. Type /accomplish_tutorial to start learning Rails from simple tutorial!") | |
| user.set_next_bot_command('BotCommand::AccomplishTutorial') | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module BotCommand | |
| class Undefined < Base | |
| def start | |
| send_message('Unknown command. Type /start if you are a new user or you have finished the game, else type the appropriate command.') | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module BotCommand | |
| class WriteBlog < Base | |
| def should_start? | |
| text =~ /\A\/write_blog/ | |
| end | |
| def start | |
| send_message(‘Hmm, looks cool! Seems like you really know Rails! A real rockstar!’) | |
| user.reset_next_bot_command | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module BotCommand | |
| class AccomplishTutorial < Base | |
| def should_start? | |
| text =~ /\A\/accomplish_tutorial/ | |
| end | |
| def start | |
| send_message("It was hard, but it’s over! Models, controllers, views, wow, a lot stuff! Let’s practice now. What do you think about writing a Rails blog? Type /write_blog to continue.") | |
| user.set_next_bot_command(‘BotCommand::WriteBlog’) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module BotCommand | |
| class Start < Base | |
| def should_start? | |
| text =~ /\A\/start/ | |
| end | |
| def start | |
| send_message('Hello! Here is a simple quest game! Type /born to start your interesting journey to the Rails rockstar position!') | |
| user.reset_next_bot_command | |
| user.set_next_bot_command('BotCommand::Born') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require ‘telegram/bot’ | |
| module BotCommand | |
| class Base | |
| attr_reader :user, :message, :api | |
| def initialize(user, message) | |
| @user = user | |
| @message = message | |
| token = Rails.application.secrets.bot_token |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| development: | |
| bot_token: bot220521163:AAHNZe-njGuJz_6-zwOyR1BKkhyJoGpNPyo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BotMessageDispatcher | |
| attr_reader :message, :user | |
| def initialize(message, user) | |
| @message = message | |
| @user = user | |
| end | |
| def process | |
| if user.get_next_bot_command |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class User < ActiveRecord::Base | |
| validates_uniqueness_of :telegram_id | |
| def set_next_bot_command(command) | |
| self.bot_command_data[‘command’] = command | |
| save | |
| end | |
| def get_next_bot_command | |
| bot_command_data[‘command’] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class WebhooksController < ApplicationController | |
| skip_before_action :verify_authenticity_token | |
| def callback | |
| dispatcher.new(webhook, user).process | |
| render nothing: true, head: :ok | |
| end | |
| def webhook | |
| params['webhook'] |