Skip to content

Instantly share code, notes, and snippets.

View MaksimAbramchuk's full-sized avatar

Maksim Abramchuk MaksimAbramchuk

View GitHub Profile
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
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
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
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
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')
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
development:
bot_token: bot220521163:AAHNZe-njGuJz_6-zwOyR1BKkhyJoGpNPyo
class BotMessageDispatcher
attr_reader :message, :user
def initialize(message, user)
@message = message
@user = user
end
def process
if user.get_next_bot_command
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’]
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']