Class: Rubydium::Bot

Inherits:
Object
  • Object
show all
Includes:
Mixins
Defined in:
lib/rubydium/bot.rb,
lib/rubydium/config.rb

Overview

:nodoc:

Constant Summary collapse

COMMAND_REGEXP =
%r{\A/}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins

included

Constructor Details

#initialize(client, update) ⇒ Bot

Returns a new instance of Bot.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubydium/bot.rb', line 40

def initialize(client, update)
  @update = update
  @client = client
  @api = client.api
  @msg = update.is_a?(Telegram::Bot::Types::Message) ? update : update.message
  @user = update.from
  @chat = @msg.chat
  @topic_id = @msg.message_thread_id if @chat.is_forum
  @replies_to = @msg.reply_to_message unless @msg.reply_to_message&.message_id == @topic_id
  @target = @replies_to&.from
  @text = (@msg.text || @msg.caption).to_s
  @message_id = @msg.message_id
  @command = get_command(@msg.text || @msg.caption)
  @text_without_command = @text.gsub(@command.to_s, "").gsub(/@#{config.bot_username}\b/,
                                                             "").strip
  @text_without_bot_mentions = @text.gsub(/@#{config.bot_username}\b/, "")
end

Class Method Details

.configObject



9
10
11
# File 'lib/rubydium/config.rb', line 9

def self.config
  @config ||= Config.new
end

.config=(config_hash) ⇒ Object



13
14
15
16
17
# File 'lib/rubydium/config.rb', line 13

def self.config=(config_hash)
  config_hash.each_pair do |key, value|
    config.public_send("#{key}=", value)
  end
end

.configure {|config| ... } ⇒ Object

Yields:



5
6
7
# File 'lib/rubydium/config.rb', line 5

def self.configure
  yield config
end

.fetch_and_set_bot_id(client) ⇒ Object



12
13
14
15
16
# File 'lib/rubydium/bot.rb', line 12

def self.fetch_and_set_bot_id(client)
  configure do |config|
    config.bot_id = client.api.get_me.dig("result", "id") unless config.respond_to? :bot_id
  end
end

.runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubydium/bot.rb', line 18

def self.run
  Telegram::Bot::Client.run(config.token) do |client|
    fetch_and_set_bot_id(client)

    Async do |task|
      client.listen do |update|
        task.async do
          # Not all `update`s here are messages (`listen` yields `Update#current_message`,
          # which can be `ChatMemberUpdated` etc.)
          # TODO: rework to allow for clean handling of other types.
          if update.is_a?(Telegram::Bot::Types::Message) || update.is_a?(Telegram::Bot::Types::CallbackQuery)
            new(client, update).handle_update
          end
        end
      rescue StandardError => e
        puts e.detailed_message, e.backtrace
        retry
      end
    end
  end
end

Instance Method Details

#configObject



19
20
21
# File 'lib/rubydium/config.rb', line 19

def config
  self.class.config
end

#handle_updateObject



58
59
60
61
62
63
# File 'lib/rubydium/bot.rb', line 58

def handle_update
  execute_on_every_message
  execute_on_mention if @text.split(/\s/).first == "@#{config.bot_username}"
  # execute_on_reply if @target&.username == config.bot_username
  execute_command
end