action_bot 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cb5381e7a0343d392ae387ec297ddb1e51309819aaaa1abefd305a0385ab8925
4
+ data.tar.gz: '0738604b83764800959d3484c514a562c08e2d8333ad49c5f1f41a4fa73bfb91'
5
+ SHA512:
6
+ metadata.gz: 8ec06a3a604d8dfe52c48f2e6c155c6b74ae3879e54dafa96034e82c88acbd8318f865ed042590c1facd613a04e20cc1ab2058d19ed66f85cd287239fe5f815f
7
+ data.tar.gz: e874cda140830bc1c9ed8560fd7cf15f17a50077b1444554bfb300afc3ad879a55a06e8c29793c65c907865e32adf7dc4e25b73469be4162edf48ed59e9d5d5c
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'telegram-bot-ruby'
4
+ gem 'pg'
5
+ gem 'activesupport'
6
+ gem 'activerecord'
7
+ gem 'actionpack'
8
+ gem 'rake'
9
+ gem 'i18n'
@@ -0,0 +1,30 @@
1
+ module ActionBot
2
+ class Base
3
+ include Configurable.with(:api, :bot)
4
+
5
+ def on(condition, **kwargs)
6
+ return unless condition
7
+
8
+ params = kwargs.delete(:params) || {}
9
+ controller, action = kwargs.first
10
+ controller_object = "#{controller.to_s.camelize}Controller".constantize.new
11
+ raise "No such method: #{action} for #{controller}" unless controller_object.respond_to?(action)
12
+ controller_object.params = ActionController::Parameters.new params
13
+ controller_object.public_send(action)
14
+ throw(:done)
15
+ end
16
+
17
+ def action(**kwargs)
18
+ params = kwargs.delete(:params) || {}
19
+ controller, action = kwargs.first
20
+ controller_object = "#{controller.to_s.camelize}Controller".constantize.new
21
+ raise "No such method: #{action} for #{controller}" unless controller_object.respond_to?(action)
22
+ controller_object.params = ActionController::Parameters.new params
23
+ controller_object.public_send(action)
24
+ end
25
+
26
+ def bot_runner
27
+ ActionBot::Runner
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,51 @@
1
+ module ActionBot
2
+ class Controller
3
+ attr_accessor :params
4
+
5
+ def notify(tg_chat_id, text, mode = nil)
6
+ return notify_chat(tg_chat_id, text, mode) unless text.respond_to?(:each)
7
+
8
+ text.each do |message|
9
+ sleep(0.5)
10
+ notify_chat(tg_chat_id, message, mode)
11
+ end
12
+ end
13
+
14
+ def mention(id, name)
15
+ "[#{name}](tg://user?id=#{id})"
16
+ end
17
+
18
+ def t(*args)
19
+ I18n.locale = load_chat.locale
20
+ I18n.t(*args)
21
+ end
22
+
23
+ def api
24
+ ActionBot::Base.config.api || bot.api
25
+ end
26
+
27
+ def bot
28
+ ActionBot::Base.config.bot
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :error
34
+
35
+ def notify_chat(chat_id, text, mode)
36
+ api.send_message(chat_id: chat_id, text: text, parse_mode: mode)
37
+ rescue Telegram::Bot::Exceptions::ResponseError => error
38
+ @error = error
39
+ return sleep(error.response.env.response_headers["retry-after"].to_i) if error_too_many_requests?
40
+ return Chat.find_by(tg_chat_id: chat_id).destroy! if error_chat_does_not_exist?
41
+ end
42
+
43
+ def error_too_many_requests?
44
+ error.response.status == 429
45
+ end
46
+
47
+ def error_chat_does_not_exist?
48
+ error.instance_variable_get("@data")["description"] == "Forbidden: the group chat was deleted"
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,7 @@
1
+ module ActionBot
2
+ class BotRunner < Telegram::Bot::Client
3
+ def self.run(*_, &block)
4
+ super(BotConfigurator.config.token, &block)
5
+ end
6
+ end
7
+ end
data/lib/action_bot.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'telegram/bot'
2
+ require 'pry'
3
+ require 'action_controller'
4
+
5
+ require 'concerns/configurable'
6
+
7
+ require 'configuration/configurators/i18n_configurator'
8
+ require 'configuration/configurators/db_configurator'
9
+ require 'configuration/configurators/bot_configurator'
10
+ require 'configuration/configurators/app_configurator'
11
+
12
+ require 'action_bot/runner'
13
+ require 'action_bot/base'
14
+ require 'action_bot/controller'
15
+
16
+ Dir["#{Dir.pwd}/controllers/**/*.rb", "#{Dir.pwd}/models/**/*.rb", "#{Dir.pwd}/controllers/bots_controller.rb"].each { |file| require file }
@@ -0,0 +1,22 @@
1
+ require 'active_support/all'
2
+
3
+ module Configurable
4
+ def self.with(*args)
5
+ config_class = Class.new do
6
+ attr_accessor *args
7
+ end
8
+
9
+ Module.new do
10
+ extend ActiveSupport::Concern
11
+ class_methods do
12
+ define_method :configure do |&block|
13
+ block.call config
14
+ end
15
+
16
+ define_method :config do
17
+ @config ||= config_class.new
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'bot_configurator'
2
+ require_relative 'i18n_configurator'
3
+ require_relative 'db_configurator'
4
+
5
+ class AppConfigurator
6
+ ATTRIBUTES = %i( token db_url pashalka i18n_load_path i18n_locale i18n_available_locales ).freeze
7
+ include Configurable.with(*ATTRIBUTES)
8
+
9
+ def self.configure(*args)
10
+ super
11
+ configure_i18n
12
+ configure_db
13
+ configure_bot
14
+ end
15
+
16
+ def self.configure_db
17
+ DBConfigurator.configure do |configuration|
18
+ configuration.db_url = config.db_url
19
+ end
20
+ end
21
+
22
+ def self.configure_i18n
23
+ I18nConfigurator.configure do |configuration|
24
+ configuration.load_path = config.i18n_load_path
25
+ configuration.locale = config.i18n_locale
26
+ configuration.available_locales = config.i18n_available_locales
27
+ end
28
+ end
29
+
30
+ def self.configure_bot
31
+ BotConfigurator.configure do |configuration|
32
+ configuration.token = config.token
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,13 @@
1
+ require_relative '../../concerns/configurable'
2
+
3
+ class BotConfigurator
4
+ ATTRIBUTES = %i( token ).freeze
5
+ include Configurable.with(*ATTRIBUTES)
6
+
7
+
8
+ def self.configure(*args)
9
+ puts 'Configuring bot...'
10
+ super *args
11
+ puts 'Finished'
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ require 'active_record'
2
+
3
+ class DBConfigurator
4
+ ATTRIBUTES = %i( db_url ).freeze
5
+ include Configurable.with(*ATTRIBUTES)
6
+
7
+ def self.configure(*args)
8
+ puts 'Configuring db...'
9
+ super *args
10
+ connection_details = config.db_url
11
+ connection = ActiveRecord::Base.establish_connection(connection_details)
12
+ puts 'Finished'
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ class I18nConfigurator
2
+ ATTRIBUTES = %i( load_path locale available_locales ).freeze
3
+ include Configurable.with(*ATTRIBUTES)
4
+
5
+ def self.configure(*args)
6
+ puts 'Configuring i18n...'
7
+ super *args
8
+ puts 'Finished'
9
+ ATTRIBUTES.each do |attr|
10
+ I18n.public_send("#{attr}=", config.public_send("#{attr}"))
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action_bot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Barseek
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-11-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Create telegram bots with rails like structure with ease
14
+ email: sergey.b@hey.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - Gemfile
20
+ - lib/action_bot.rb
21
+ - lib/action_bot/base.rb
22
+ - lib/action_bot/controller.rb
23
+ - lib/action_bot/runner.rb
24
+ - lib/concerns/configurable.rb
25
+ - lib/configuration/configurators/app_configurator.rb
26
+ - lib/configuration/configurators/bot_configurator.rb
27
+ - lib/configuration/configurators/db_configurator.rb
28
+ - lib/configuration/configurators/i18n_configurator.rb
29
+ homepage: https://rubygems.org/gems/hola
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 3.2.29
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Telegram Bots gem for ruby
52
+ test_files: []