telegrator 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/telegrator/cli.rb +6 -0
- data/lib/telegrator/generators/bot/generator.rb +14 -1
- data/lib/telegrator/generators/bot/templates/app/services.rb.tt +3 -0
- data/lib/telegrator/generators/bot/templates/app/services/processor.rb.tt +49 -0
- data/lib/telegrator/generators/bot/templates/app/workers/processor.rb.tt +1 -35
- data/lib/telegrator/generators/bot/templates/config.ru.tt +17 -0
- data/lib/telegrator/generators/bot/templates/config/boot.rb.tt +1 -0
- data/lib/telegrator/generators/bot/templates/config/deploy.rb.tt +1 -1
- data/lib/telegrator/version.rb +1 -1
- data/telegrator.gemspec +1 -1
- metadata +8 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e1ad76a03640833177bec21dd4edccb7c8ac5027
|
4
|
+
data.tar.gz: 4c1b58e2ed91b1e2919fd72e7f727eea75789e92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3139ea3b7a0b80928f39a8bde4f55883960ceda918254f968b22142e38b244f87bbafc6a05ce93d57ca735d22bfef219d8be5dff355007d0125de2c4027fd66a
|
7
|
+
data.tar.gz: dcf50f2af18f666b9c55d6d97d9ebd6a31b6b7ccada47b1fb42c7457088697fb4327d3f4c876965f7b2ca51c122a10f7f0e4be6f0551f0888b115ff8504bf53c
|
data/lib/telegrator/cli.rb
CHANGED
@@ -18,6 +18,9 @@ module Telegrator
|
|
18
18
|
class_option :database, type: :string, aliases: '-d', default: 'postgresql', enum: DATABASES,
|
19
19
|
desc: 'Preconfigure for selected database'
|
20
20
|
|
21
|
+
class_option :skip_bundler, type: :boolean, default: false,
|
22
|
+
desc: 'Skip Bundler'
|
23
|
+
|
21
24
|
class_option :skip_capistrano, type: :boolean, default: false,
|
22
25
|
desc: 'Skip Capistrano'
|
23
26
|
|
@@ -37,7 +40,6 @@ module Telegrator
|
|
37
40
|
|
38
41
|
def create_root_files
|
39
42
|
template 'gitignore.tt', '.gitignore'
|
40
|
-
inside { run 'git init' }
|
41
43
|
|
42
44
|
template 'env.tt', '.env.sample'
|
43
45
|
template 'env.tt', '.env'
|
@@ -45,6 +47,7 @@ module Telegrator
|
|
45
47
|
template 'Gemfile.tt'
|
46
48
|
template 'Rakefile.tt'
|
47
49
|
template 'Capfile.tt' unless options[:skip_capistrano]
|
50
|
+
template 'config.ru.tt' unless options[:skip_webhook]
|
48
51
|
end
|
49
52
|
|
50
53
|
# === app/ directory ===
|
@@ -70,6 +73,11 @@ module Telegrator
|
|
70
73
|
directory 'app/models'
|
71
74
|
end
|
72
75
|
|
76
|
+
def create_services
|
77
|
+
template 'app/services.rb'
|
78
|
+
directory 'app/services'
|
79
|
+
end
|
80
|
+
|
73
81
|
def create_workers
|
74
82
|
template 'app/workers.rb'
|
75
83
|
directory 'app/workers'
|
@@ -109,6 +117,11 @@ module Telegrator
|
|
109
117
|
create_file 'log/.keep'
|
110
118
|
end
|
111
119
|
|
120
|
+
def init
|
121
|
+
inside { run 'git init' }
|
122
|
+
inside { run 'bundle install' } unless options[:skip_bundler]
|
123
|
+
end
|
124
|
+
|
112
125
|
private
|
113
126
|
|
114
127
|
DATABASES.each do |db|
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Services
|
2
|
+
class Processor
|
3
|
+
HANDLERS = {
|
4
|
+
'\A\/start(\s.*|\z)' => Commands::Start,
|
5
|
+
'\A\/stop\z' => Commands::Stop
|
6
|
+
}.freeze
|
7
|
+
|
8
|
+
def self.call(*args)
|
9
|
+
new(*args).call
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(message_dump)
|
13
|
+
@message_dump = message_dump
|
14
|
+
end
|
15
|
+
|
16
|
+
def call
|
17
|
+
process_message YAML.load(@message_dump)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def process_message(message)
|
23
|
+
user = user_by_message(message)
|
24
|
+
command_klass(message).invoke(message, user)
|
25
|
+
end
|
26
|
+
|
27
|
+
def user_by_message(message)
|
28
|
+
from = message.from
|
29
|
+
user = Models::User.find_or_initialize_by(telegram_id: from.id)
|
30
|
+
if user.new_record?
|
31
|
+
user.update(first_name: from.first_name, last_name: from.last_name, username: from.username)
|
32
|
+
end
|
33
|
+
user
|
34
|
+
end
|
35
|
+
|
36
|
+
def command_klass(message)
|
37
|
+
command_klass_by_text(message.text) || Commands::Missing
|
38
|
+
end
|
39
|
+
|
40
|
+
def command_klass_by_text(text)
|
41
|
+
key = HANDLERS.keys.find { |regex| /#{regex}/ =~ prepare_text(text) }
|
42
|
+
HANDLERS[key]
|
43
|
+
end
|
44
|
+
|
45
|
+
def prepare_text(text)
|
46
|
+
text.strip
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -1,41 +1,7 @@
|
|
1
1
|
module Workers
|
2
2
|
class Processor < Base
|
3
|
-
HANDLERS = {
|
4
|
-
'\A\/start(\s.*|\z)' => Commands::Start,
|
5
|
-
'\A\/stop\z' => Commands::Stop
|
6
|
-
}.freeze
|
7
|
-
|
8
3
|
def perform(message_dump)
|
9
|
-
|
10
|
-
end
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def process_message(message)
|
15
|
-
user = user_by_message(message)
|
16
|
-
command_klass(message).invoke(message, user)
|
17
|
-
end
|
18
|
-
|
19
|
-
def user_by_message(message)
|
20
|
-
from = message.from
|
21
|
-
user = Models::User.find_or_initialize_by(telegram_id: from.id)
|
22
|
-
if user.new_record?
|
23
|
-
user.update(first_name: from.first_name, last_name: from.last_name, username: from.username)
|
24
|
-
end
|
25
|
-
user
|
26
|
-
end
|
27
|
-
|
28
|
-
def command_klass(message)
|
29
|
-
command_klass_by_text(message.text) || Commands::Missing
|
30
|
-
end
|
31
|
-
|
32
|
-
def command_klass_by_text(text)
|
33
|
-
key = HANDLERS.keys.find { |regex| /#{regex}/ =~ prepare_text(text) }
|
34
|
-
HANDLERS[key]
|
35
|
-
end
|
36
|
-
|
37
|
-
def prepare_text(text)
|
38
|
-
text.strip
|
4
|
+
Services::Processor.call(message_dump)
|
39
5
|
end
|
40
6
|
end
|
41
7
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rack/contrib'
|
2
|
+
require File.expand_path('../config/boot', __FILE__)
|
3
|
+
|
4
|
+
class Listener
|
5
|
+
def call(env)
|
6
|
+
req = Rack::Request.new(env)
|
7
|
+
|
8
|
+
update = Telegram::Bot::Types::Update.new(req.params)
|
9
|
+
Services::Processor.call YAML.dump(update.current_message)
|
10
|
+
|
11
|
+
[200, {}, []]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
use Rack::PostBodyContentTypeParser
|
16
|
+
|
17
|
+
run Listener.new
|
@@ -18,4 +18,4 @@ set :default_env, path: '~/.rbenv/shims:~/.rbenv/bin:$PATH'
|
|
18
18
|
|
19
19
|
set :sidekiq_pid, File.join(shared_path, 'tmp', 'pids', 'sidekiq.pid')
|
20
20
|
set :sidekiq_config, File.join(current_path, 'config', 'sidekiq.yml')
|
21
|
-
set :sidekiq_require, File.join(current_path, '
|
21
|
+
set :sidekiq_require, File.join(current_path, 'config', 'boot.rb')
|
data/lib/telegrator/version.rb
CHANGED
data/telegrator.gemspec
CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
23
|
spec.require_paths = ['lib']
|
24
24
|
|
25
|
-
spec.add_runtime_dependency 'thor', '~> 0.
|
25
|
+
spec.add_runtime_dependency 'thor', '~> 0.20.0'
|
26
26
|
|
27
27
|
spec.add_development_dependency 'bundler', '~> 1.14'
|
28
28
|
spec.add_development_dependency 'rake', '~> 12.0'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: telegrator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aleksey Ivanov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 0.20.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
26
|
+
version: 0.20.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -117,10 +117,13 @@ files:
|
|
117
117
|
- lib/telegrator/generators/bot/templates/app/models.rb.tt
|
118
118
|
- lib/telegrator/generators/bot/templates/app/models/base.rb.tt
|
119
119
|
- lib/telegrator/generators/bot/templates/app/models/user.rb.tt
|
120
|
+
- lib/telegrator/generators/bot/templates/app/services.rb.tt
|
121
|
+
- lib/telegrator/generators/bot/templates/app/services/processor.rb.tt
|
120
122
|
- lib/telegrator/generators/bot/templates/app/workers.rb.tt
|
121
123
|
- lib/telegrator/generators/bot/templates/app/workers/base.rb.tt
|
122
124
|
- lib/telegrator/generators/bot/templates/app/workers/processor.rb.tt
|
123
125
|
- lib/telegrator/generators/bot/templates/bin/bot
|
126
|
+
- lib/telegrator/generators/bot/templates/config.ru.tt
|
124
127
|
- lib/telegrator/generators/bot/templates/config/boot.rb.tt
|
125
128
|
- lib/telegrator/generators/bot/templates/config/deploy.rb.tt
|
126
129
|
- lib/telegrator/generators/bot/templates/config/deploy/production.rb.tt
|
@@ -157,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
157
160
|
version: '0'
|
158
161
|
requirements: []
|
159
162
|
rubyforge_project:
|
160
|
-
rubygems_version: 2.6.
|
163
|
+
rubygems_version: 2.6.13
|
161
164
|
signing_key:
|
162
165
|
specification_version: 4
|
163
166
|
summary: A generator to set up new Telegram bot easily
|