action_bot 0.1.15 → 0.1.19

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6de4a5c2c1fe6a086128a827eccb0fa734c7f6fa889542ef42d20255938baca0
4
- data.tar.gz: f02b78ccad516424b5e373d96884289a0fee5c12d1cfeaf1c0d05c6a9cf3bfed
3
+ metadata.gz: 802cec2057aa409c4b36ec18635cda38a33bce66bcbfeba7fd3d6cb4f4f4a992
4
+ data.tar.gz: d78cffe09c272f262700ee7c21fd6dd9708584cebd7d375a5528fed166928a7a
5
5
  SHA512:
6
- metadata.gz: 37dd1778d7b62fdfec43c3517eaea7e955cc3dfd013409ba993a15401a8a2cbc3c0c1d09b05d59cd2fa76f5b5ab9ebb5ac572648cd54348ff4409be10f536a1f
7
- data.tar.gz: b449f676506be929e261beb4f60ea558c4b1a06ccbb51221ea6669b8af1eb117c525cf0d33fe1cf49e3b7472ccfd0d1d8c51a04d13dad045aae379a1262ca315
6
+ metadata.gz: 0e6155339bfee95e3f73d282498d2089c975c8f0c444f572848645620bb48b39bdda40dff0cda32cfa68bdb5b6d8aead3a5f7260b26f9f1e6bdd37b9f62c34dc
7
+ data.tar.gz: a50600a07b806be3ff5c86ff64cf8edb412d9734466cffd46eef44c48178eeb5290aaa31d06f97278d0c578380390facdc9b2a84fa53bd9ded2c91ae81d4586e
@@ -3,12 +3,16 @@ module ActionBot
3
3
  include Configurable.with(:api, :bot)
4
4
 
5
5
  def on(condition, **kwargs)
6
+ puts "#{Time.now}: Received:- #{kwargs}"
6
7
  return unless condition
7
8
 
8
9
  params = kwargs.delete(:params) || {}
9
10
  controller, action = kwargs.first
10
11
  controller_object = "#{controller.to_s.camelize}Controller".constantize.new
12
+ puts "controller: #{controller.to_s.camelize}Controller, action: #{action}"
11
13
  raise "No such method: #{action} for #{controller}" unless controller_object.respond_to?(action)
14
+
15
+ puts "Receiving..."
12
16
  controller_object.params = ActionController::Parameters.new params
13
17
  controller_object.public_send(action)
14
18
  throw(:done)
@@ -12,7 +12,7 @@ class AppConfigurator
12
12
  def self.configure_db
13
13
  DBConfigurator.configure do |configuration|
14
14
  configuration.db_url = config.db_url
15
- end
15
+ end if config.db_url
16
16
  end
17
17
 
18
18
  def self.configure_i18n
@@ -2,5 +2,5 @@ spec = Gem::Specification.find_by_name 'action_bot'
2
2
 
3
3
  desc 'Generate files for the bot'
4
4
  task :bot_scaffold do
5
- FileUtils.cp_r "#{spec}/scaffolds/.", "#{Dir.pwd}"
5
+ FileUtils.cp_r "#{spec.gem_dir}/scaffolds/.", "#{Dir.pwd}"
6
6
  end
@@ -0,0 +1,7 @@
1
+ require 'action_bot'
2
+
3
+ require_relative '../config/configuration'
4
+ require_relative '../bots/example_long_polling_bot'
5
+
6
+
7
+ ExampleLongPollingBot.call
@@ -0,0 +1,20 @@
1
+ require 'sinatra'
2
+ require 'action_bot'
3
+
4
+ require_relative '../config/configuration'
5
+ require_relative '../bots/example_webhook_bot'
6
+ use Rack::Logger
7
+
8
+ helpers do
9
+ def logger
10
+ request.logger
11
+ end
12
+ end
13
+
14
+ # Telegram requires GET endpoint to setup a webhook
15
+ get '/webhook' do
16
+ end
17
+
18
+ post '/webhook' do
19
+ # Do stuff with request here
20
+ end
@@ -0,0 +1,35 @@
1
+ class ExampleLongPollingBot < ActionBot::Base
2
+ def call
3
+ bot_runner.run do |bot|
4
+ ActionBot::Base.configure do |config|
5
+ config.bot = bot
6
+ end
7
+
8
+ bot.listen do |message|
9
+ @message = message
10
+ run
11
+ end
12
+ end
13
+ end
14
+
15
+ def self.call
16
+ new.call
17
+ end
18
+
19
+ def run
20
+ return unless message_legal?
21
+
22
+ on @message == 'hello_world', hello: :world, params: {
23
+ tg_chat_id: tg_chat_id
24
+ }
25
+ end
26
+
27
+ private
28
+
29
+ def message_legal?
30
+ @message.respond_to?(:text) && @message.text
31
+
32
+ def tg_chat_id
33
+ @message.chat.id
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ class ExampleWebhookBot < ActionBot::Base
2
+ def initialize
3
+ @api = Telegram::Bot::Api.new(AppConfigurator.config.token)
4
+ ActionBot::Base.configure do |config|
5
+ config.api = @api
6
+ end
7
+ end
8
+
9
+ def run(message)
10
+ @message = message
11
+
12
+ return unless message_legal?
13
+ catch(:done) do
14
+ on message == 'hello_world', hello: :world
15
+ end
16
+ end
17
+
18
+ def message_legal?
19
+ @message.respond_to?(:text) && @message.text
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ AppConfigurator.configure do |config|
2
+ # Configure bot
3
+ config.token = begin
4
+ YAML::load(IO.read('config/secrets/token.yml'))['telegram_bot_token']
5
+ rescue Errno::ENOENT
6
+ ENV["TOKEN"]
7
+ end
8
+
9
+ # Configure database
10
+ # config.db_url = begin
11
+ # YAML::load(IO.read('config/secrets/database.yml'))["database_url"]
12
+ # rescue Errno::ENOENT
13
+ # ENV["DATABASE_URL"]
14
+ # end
15
+
16
+ # Configure i18n
17
+ config.i18n_locale = :en
18
+ config.i18n_load_path = Dir['config/secrets/locales.yml']
19
+ config.i18n_available_locales = %i[en]
20
+ end
@@ -0,0 +1 @@
1
+ database_url: postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]
@@ -0,0 +1,2 @@
1
+ en:
2
+ hello_world: "Hello world!"
@@ -0,0 +1 @@
1
+ telegram_bot_token: your-tg-bot-token
@@ -0,0 +1,2 @@
1
+ class BotsController < ActionBot::Controller
2
+ end
@@ -0,0 +1,5 @@
1
+ class HelloController < BotsController
2
+ def world
3
+ notify(params[:tg_chat_id], t('hello_world'))
4
+ end
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.15
4
+ version: 0.1.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Barseek
@@ -126,6 +126,16 @@ files:
126
126
  - lib/configuration/configurators/db_configurator.rb
127
127
  - lib/configuration/configurators/i18n_configurator.rb
128
128
  - lib/tasks/bot_scaffold.rake
129
+ - scaffolds/bin/example_long_polling_bot_runner.rb
130
+ - scaffolds/bin/example_webhook_bot_runner.rb
131
+ - scaffolds/bots/example_long_polling_bot.rb
132
+ - scaffolds/bots/example_webhook_bot.rb
133
+ - scaffolds/config/configuration.rb
134
+ - scaffolds/config/secrets/database.yml
135
+ - scaffolds/config/secrets/locales.yml
136
+ - scaffolds/config/secrets/token.yml
137
+ - scaffolds/controllers/bots_controller.rb
138
+ - scaffolds/controllers/hello_controller.rb
129
139
  homepage: https://rubygems.org/gems/hola
130
140
  licenses:
131
141
  - MIT