telegram_workflow 1.0.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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +72 -0
- data/LICENSE.txt +21 -0
- data/README.md +360 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/telegram_workflow.rb +38 -0
- data/lib/telegram_workflow/action.rb +44 -0
- data/lib/telegram_workflow/client.rb +140 -0
- data/lib/telegram_workflow/config.rb +48 -0
- data/lib/telegram_workflow/errors.rb +23 -0
- data/lib/telegram_workflow/params.rb +61 -0
- data/lib/telegram_workflow/rspec.rb +80 -0
- data/lib/telegram_workflow/session.rb +46 -0
- data/lib/telegram_workflow/stores/file.rb +18 -0
- data/lib/telegram_workflow/stores/in_memory.rb +13 -0
- data/lib/telegram_workflow/updates.rb +18 -0
- data/lib/telegram_workflow/version.rb +3 -0
- data/lib/telegram_workflow/workflow.rb +95 -0
- data/telegram_workflow.gemspec +28 -0
- metadata +111 -0
@@ -0,0 +1,46 @@
|
|
1
|
+
class TelegramWorkflow::Session
|
2
|
+
def initialize(params)
|
3
|
+
@session_id = params.user_id
|
4
|
+
@store = TelegramWorkflow.config.session_store
|
5
|
+
|
6
|
+
@session = if serialized_session = @store.read(@session_id)
|
7
|
+
Marshal.load(serialized_session)
|
8
|
+
else
|
9
|
+
{}
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def read(key)
|
14
|
+
@session[key]
|
15
|
+
end
|
16
|
+
|
17
|
+
def write(key, value)
|
18
|
+
@session[key] = value
|
19
|
+
end
|
20
|
+
|
21
|
+
def delete(key)
|
22
|
+
@session.delete(key)
|
23
|
+
end
|
24
|
+
|
25
|
+
def clear
|
26
|
+
@session.clear
|
27
|
+
end
|
28
|
+
|
29
|
+
def dump
|
30
|
+
@store.write(@session_id, Marshal.dump(@session))
|
31
|
+
end
|
32
|
+
|
33
|
+
# this is a user space to store some session data separately from the gem session
|
34
|
+
def user_session
|
35
|
+
@session[:user_session] ||= {}
|
36
|
+
end
|
37
|
+
|
38
|
+
# this is a temporary per-action store
|
39
|
+
def flash
|
40
|
+
@session[:flash] ||= {}
|
41
|
+
end
|
42
|
+
|
43
|
+
def reset_flash
|
44
|
+
flash.clear
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "dbm"
|
2
|
+
|
3
|
+
class TelegramWorkflow::Stores::File
|
4
|
+
StorePath = Pathname.new("tmp/telegram_workflow/file_store")
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
StorePath.dirname.mkpath unless StorePath.exist?
|
8
|
+
@store = DBM.open(StorePath.to_s)
|
9
|
+
end
|
10
|
+
|
11
|
+
def read(key)
|
12
|
+
@store[key.to_s]
|
13
|
+
end
|
14
|
+
|
15
|
+
def write(key, value)
|
16
|
+
@store[key.to_s] = value
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class TelegramWorkflow::Updates
|
2
|
+
def initialize(params)
|
3
|
+
@params = params
|
4
|
+
end
|
5
|
+
|
6
|
+
def enum
|
7
|
+
Enumerator.new do |y|
|
8
|
+
loop do
|
9
|
+
updates = TelegramWorkflow::Client.new.get_updates(@params)["result"]
|
10
|
+
updates.each do |update|
|
11
|
+
y << update
|
12
|
+
end
|
13
|
+
|
14
|
+
@params.merge! offset: updates.last["update_id"] + 1
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
class TelegramWorkflow::Workflow
|
2
|
+
attr_reader :params, :client
|
3
|
+
|
4
|
+
def initialize(raw_params)
|
5
|
+
@params = TelegramWorkflow::Params.new(raw_params)
|
6
|
+
@session = TelegramWorkflow::Session.new(@params)
|
7
|
+
@logger = TelegramWorkflow.config.logger
|
8
|
+
|
9
|
+
if @params.start?
|
10
|
+
@session.clear
|
11
|
+
end
|
12
|
+
|
13
|
+
chat_id = @session.read(:chat_id) || @session.write(:chat_id, @params.chat_id)
|
14
|
+
@client = TelegramWorkflow.config.client.new(chat_id)
|
15
|
+
end
|
16
|
+
|
17
|
+
def process
|
18
|
+
# run the shared step
|
19
|
+
shared_step_result = current_action.shared
|
20
|
+
|
21
|
+
if shared_step_result == :__continue
|
22
|
+
@logger.info "[TelegramWorkflow] Processing by #{current_action.class.name}##{current_step}"
|
23
|
+
current_action.public_send(current_step) # setup callbacks
|
24
|
+
current_action.__run_on_message # run a callback
|
25
|
+
else
|
26
|
+
@logger.info "[TelegramWorkflow] Processing by shared handler"
|
27
|
+
end
|
28
|
+
|
29
|
+
while @redirect_to
|
30
|
+
do_redirect
|
31
|
+
end
|
32
|
+
|
33
|
+
@session.dump
|
34
|
+
end
|
35
|
+
|
36
|
+
def redirect_to(action_or_step, session_params = nil)
|
37
|
+
raise TelegramWorkflow::Errors::DoubleRedirect if @redirect_to
|
38
|
+
raise TelegramWorkflow::Errors::SharedRedirect if action_or_step == :shared
|
39
|
+
|
40
|
+
@redirect_to = action_or_step
|
41
|
+
@session_params = session_params
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def current_action
|
47
|
+
@current_action ||= begin
|
48
|
+
action_class = if action = @session.read(:current_action)
|
49
|
+
Object.const_get(action)
|
50
|
+
else
|
51
|
+
TelegramWorkflow.config.start_action
|
52
|
+
end
|
53
|
+
|
54
|
+
action_class.new(self, @session.user_session, @session.flash)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def set_current_action(action_class)
|
59
|
+
@session.write(:current_action, action_class.to_s)
|
60
|
+
set_current_step(nil)
|
61
|
+
@session.reset_flash
|
62
|
+
|
63
|
+
@current_action = action_class.new(self, @session.user_session, @session.flash)
|
64
|
+
end
|
65
|
+
|
66
|
+
def current_step
|
67
|
+
@session.read(:current_step) || :initial
|
68
|
+
end
|
69
|
+
|
70
|
+
def set_current_step(step)
|
71
|
+
@session.write(:current_step, step)
|
72
|
+
end
|
73
|
+
|
74
|
+
def do_redirect
|
75
|
+
action_or_step = @redirect_to
|
76
|
+
session_params = @session_params
|
77
|
+
@redirect_to = @session_params = nil
|
78
|
+
|
79
|
+
# reset on_message and on_redirect callbacks
|
80
|
+
current_action.__reset_callbacks
|
81
|
+
|
82
|
+
if action_or_step.is_a?(Class)
|
83
|
+
set_current_action(action_or_step)
|
84
|
+
else
|
85
|
+
set_current_step(action_or_step)
|
86
|
+
end
|
87
|
+
|
88
|
+
if session_params
|
89
|
+
@session.flash.merge!(session_params)
|
90
|
+
end
|
91
|
+
|
92
|
+
current_action.public_send(current_step) # setup callbacks
|
93
|
+
current_action.__run_on_redirect # run a callback
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'lib/telegram_workflow/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "telegram_workflow"
|
5
|
+
spec.version = TelegramWorkflow::VERSION
|
6
|
+
spec.authors = ["Roman Samoilov"]
|
7
|
+
|
8
|
+
spec.summary = %q{A simple library to create Telegram Bots in Ruby.}
|
9
|
+
spec.homepage = "https://github.com/rsamoilov/telegram_workflow"
|
10
|
+
spec.license = "MIT"
|
11
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
12
|
+
|
13
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
14
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
15
|
+
|
16
|
+
spec.add_dependency "http"
|
17
|
+
spec.add_development_dependency "rspec", "~> 3.9"
|
18
|
+
spec.add_development_dependency "activesupport", "~> 6.0.0"
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: telegram_workflow
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Roman Samoilov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-05-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: http
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 6.0.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 6.0.0
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- ".gitignore"
|
62
|
+
- ".rspec"
|
63
|
+
- ".travis.yml"
|
64
|
+
- CODE_OF_CONDUCT.md
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/telegram_workflow.rb
|
73
|
+
- lib/telegram_workflow/action.rb
|
74
|
+
- lib/telegram_workflow/client.rb
|
75
|
+
- lib/telegram_workflow/config.rb
|
76
|
+
- lib/telegram_workflow/errors.rb
|
77
|
+
- lib/telegram_workflow/params.rb
|
78
|
+
- lib/telegram_workflow/rspec.rb
|
79
|
+
- lib/telegram_workflow/session.rb
|
80
|
+
- lib/telegram_workflow/stores/file.rb
|
81
|
+
- lib/telegram_workflow/stores/in_memory.rb
|
82
|
+
- lib/telegram_workflow/updates.rb
|
83
|
+
- lib/telegram_workflow/version.rb
|
84
|
+
- lib/telegram_workflow/workflow.rb
|
85
|
+
- telegram_workflow.gemspec
|
86
|
+
homepage: https://github.com/rsamoilov/telegram_workflow
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata:
|
90
|
+
homepage_uri: https://github.com/rsamoilov/telegram_workflow
|
91
|
+
source_code_uri: https://github.com/rsamoilov/telegram_workflow
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: 2.3.0
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubygems_version: 3.1.2
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: A simple library to create Telegram Bots in Ruby.
|
111
|
+
test_files: []
|