Class: Rubydium::Bot
- Inherits:
-
Object
- Object
- Rubydium::Bot
- 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
- .config ⇒ Object
- .config=(config_hash) ⇒ Object
- .configure {|config| ... } ⇒ Object
- .fetch_and_set_bot_id(client) ⇒ Object
- .run ⇒ Object
Instance Method Summary collapse
- #config ⇒ Object
- #handle_update ⇒ Object
-
#initialize(client, update) ⇒ Bot
constructor
A new instance of Bot.
Methods included from Mixins
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. @user = update.from @chat = @msg.chat @topic_id = @msg. if @chat.is_forum @replies_to = @msg. unless @msg.&. == @topic_id @target = @replies_to&.from @text = (@msg.text || @msg.).to_s @message_id = @msg. @command = get_command(@msg.text || @msg.) @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
.config ⇒ Object
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
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 |
.run ⇒ Object
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., e.backtrace retry end end end end |
Instance Method Details
#config ⇒ Object
19 20 21 |
# File 'lib/rubydium/config.rb', line 19 def config self.class.config end |
#handle_update ⇒ Object
58 59 60 61 62 63 |
# File 'lib/rubydium/bot.rb', line 58 def handle_update execute_on_mention if @text.split(/\s/).first == "@#{config.bot_username}" # execute_on_reply if @target&.username == config.bot_username execute_command end |