Module: TelegramBotEngine::SubscriberCommands

Extended by:
ActiveSupport::Concern
Defined in:
lib/telegram_bot_engine/subscriber_commands.rb

Instance Method Summary collapse

Instance Method Details

#help!Object

/help - list all available commands



51
52
53
54
55
56
57
58
# File 'lib/telegram_bot_engine/subscriber_commands.rb', line 51

def help!(*)
  TelegramBotEngine::Event.log(
    event_type: "command", action: "help",
    chat_id: chat["id"], username: from["username"]
  )

  respond_with :message, text: "📋 *Available Commands*\n\n#{available_commands_text}", parse_mode: "Markdown"
end

#start!Object

/start - create subscription if authorized



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/telegram_bot_engine/subscriber_commands.rb', line 13

def start!(*)
  subscription = TelegramBotEngine::Subscription.find_or_initialize_by(
    chat_id: chat["id"]
  )
  subscription.assign_attributes(
    user_id: from["id"],
    username: from["username"],
    first_name: from["first_name"],
    active: true
  )
  subscription.save!

  TelegramBotEngine::Event.log(
    event_type: "command", action: "start",
    chat_id: chat["id"], username: from["username"]
  )

  welcome = TelegramBotEngine.config.welcome_message % {
    username: from["first_name"] || from["username"],
    commands: available_commands_text
  }
  respond_with :message, text: welcome
end

#stop!Object

/stop - deactivate subscription



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/telegram_bot_engine/subscriber_commands.rb', line 38

def stop!(*)
  subscription = TelegramBotEngine::Subscription.find_by(chat_id: chat["id"])
  subscription&.update(active: false)

  TelegramBotEngine::Event.log(
    event_type: "command", action: "stop",
    chat_id: chat["id"], username: from["username"]
  )

  respond_with :message, text: "You've been unsubscribed. Send /start to resubscribe."
end