flow_chat 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 77864972a34ad8761c6ce77a4f3574b9f45b4f8ef3210c34ca376812d0b54a41
4
+ data.tar.gz: 47b860cf8170d1cda13fa280ba92ba731c3060ca7d955b727b22ce8367ee8e97
5
+ SHA512:
6
+ metadata.gz: fd997d03b94ce1051ab2ad6483b5864fb2b223b510d96d45833c7908a6928b419fdc4c5512730c80bf86e5f1b9741f2fc3d7c377a1f4ba8ea2b7baa0ed2244bf
7
+ data.tar.gz: c39477b7915c3abe9f2b1ff5c31b99eb3a618ecd2ab53e5405fb34e8564822510ad16dd94a2ceb664e33ada5549910c096b38e1a925e8609a3f73c855b392d3d
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # Ignore Gemfile.lock for our gem
14
+ /Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in flow_chat.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Stefan Froelich
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # FlowChat
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/flow_chat`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'flow_chat'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install flow_chat
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/flow_chat.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "flow_chat"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/flow_chat.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ require_relative "lib/flow_chat/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "flow_chat"
5
+ spec.version = FlowChat::VERSION
6
+ spec.authors = ["Stefan Froelich"]
7
+ spec.email = ["sfroelich01@gmail.com"]
8
+
9
+ spec.summary = "Enable USSD processing support in Rails."
10
+ spec.description = "Enable USSD processing support in Rails."
11
+ spec.homepage = "https://github.com/ussd-engine/api-base"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = spec.homepage
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "zeitwerk"
31
+ spec.add_dependency "activesupport", ">= 6"
32
+ spec.add_dependency "actionpack", ">= 6"
33
+ spec.add_dependency "phonelib"
34
+ spec.add_dependency "ibsciss-middleware", "~> 0.4.2"
35
+ end
@@ -0,0 +1,16 @@
1
+ module FlowChat
2
+ module Config
3
+ mattr_accessor :logger, default: Logger.new($stdout)
4
+ mattr_accessor :cache, default: nil
5
+
6
+ mattr_accessor :pagination_page_size, default: 140
7
+ mattr_accessor :pagination_back_option, default: "0"
8
+ mattr_accessor :pagination_back_text, default: "Back"
9
+ mattr_accessor :pagination_next_option, default: "#"
10
+ mattr_accessor :pagination_next_text, default: "More"
11
+
12
+ mattr_accessor :resumable_sessions_enabled, default: false
13
+ mattr_accessor :resumable_sessions_global, default: true
14
+ mattr_accessor :resumable_sessions_timeout_seconds, default: 300
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ module FlowChat
2
+ class Context
3
+ def initialize
4
+ @data = {}.with_indifferent_access
5
+ end
6
+
7
+ def [](key)
8
+ @data[key]
9
+ end
10
+
11
+ def []=(key, value)
12
+ @data[key] = value
13
+ end
14
+
15
+ def session = @data["session"]
16
+
17
+ def controller = @data["controller"]
18
+
19
+ # def request = controller.request
20
+
21
+ def flow = @data["flow.class"]
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module FlowChat
2
+ module Interrupt
3
+ class Base < Exception
4
+ attr_reader :prompt
5
+
6
+ def initialize(prompt)
7
+ @prompt = prompt
8
+ super
9
+ end
10
+ end
11
+
12
+ class Prompt < Base
13
+ attr_reader :choices
14
+
15
+ def initialize(*args, choices: nil)
16
+ @choices = choices
17
+ super(*args)
18
+ end
19
+ end
20
+
21
+ class Terminate < Base; end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ module FlowChat
2
+ module Session
3
+ class Middleware
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(context)
9
+ context["session.id"] = session_id context
10
+ context["session"] = context["session.store"].new context
11
+ @app.call(context)
12
+ end
13
+
14
+ private
15
+
16
+ def session_id(context)
17
+ context["request.id"]
18
+ # File.join(
19
+ # context["PATH_INFO"],
20
+ # (Config.resumable_sessions_enabled && Config.resumable_sessions_global) ? "global" : context["ussd.request"][:provider].to_s,
21
+ # context["ussd.request"][:msisdn]
22
+ # )
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,33 @@
1
+ module FlowChat
2
+ module Session
3
+ class RailsSessionStore
4
+ def initialize(context)
5
+ @session_id = context["session.id"]
6
+ @session_store = context.controller.session
7
+ @session_data = (session_store[session_id] || {}).with_indifferent_access
8
+ end
9
+
10
+ def get(key, default = nil)
11
+ session_data[key] || default
12
+ end
13
+
14
+ def set(key, value)
15
+ session_data[key] = value
16
+ session_store[session_id] = session_data
17
+ value
18
+ end
19
+
20
+ def delete(key)
21
+ set key, nil
22
+ end
23
+
24
+ def destroy
25
+ session_store[session_id] = nil
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :session_id, :session_store, :session_data
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ module FlowChat
2
+ module Ussd
3
+ class App
4
+ attr_reader :session, :input, :context, :navigation_stack
5
+
6
+ def initialize(context)
7
+ @context = context
8
+ @session = context["session"]
9
+ @input = context["request.input"]
10
+ @navigation_stack = []
11
+ end
12
+
13
+ def screen(key)
14
+ raise ArgumentError, "a block is expected" unless block_given?
15
+ raise ArgumentError, "screen has been presented" if navigation_stack.include?(key)
16
+
17
+ navigation_stack << key
18
+ return session.get(key) if session.get(key).present?
19
+
20
+ prompt = FlowChat::Ussd::Prompt.new input
21
+ @input = nil # input is being submitted to prompt so we clear it
22
+
23
+ value = yield prompt
24
+ session.set(key, value)
25
+ value
26
+ end
27
+
28
+ def terminate!(msg)
29
+ raise FlowChat::Interrupt::Terminate.new(msg)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,45 @@
1
+ require "phonelib"
2
+
3
+ module FlowChat
4
+ module Ussd
5
+ module Gateway
6
+ class Nalo
7
+ def initialize(app)
8
+ @app = app
9
+ end
10
+
11
+ def call(context)
12
+ params = context.controller.request.params
13
+
14
+ context["request.id"] = params["USERID"]
15
+ context["request.gateway"] = :nalo
16
+ context["request.network"] = nil
17
+ context["request.msisdn"] = Phonelib.parse(params["MSISDN"]).e164
18
+ # context["request.type"] = params["MSGTYPE"] ? :initial : :response
19
+ context["request.input"] = params["USERDATA"].presence
20
+
21
+ type, msg, choices = @app.call(context)
22
+
23
+ context.controller.render json: {
24
+ USERID: params["USERID"],
25
+ MSISDN: params["MSISDN"],
26
+ MSG: build_message(msg, choices),
27
+ MSGTYPE: type == :prompt
28
+ }
29
+ end
30
+
31
+ private
32
+
33
+ def build_message(msg, choices)
34
+ [msg, build_choices(choices)].compact.join "\n\n"
35
+ end
36
+
37
+ def build_choices(choices)
38
+ return unless choices.present?
39
+
40
+ choices.map { |i, c| "#{i}. #{c}" }.join "\n"
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,58 @@
1
+ require "phonelib"
2
+
3
+ module FlowChat
4
+ module Ussd
5
+ module Gateway
6
+ class Nsano
7
+ def initialize(app)
8
+ @app = app
9
+ end
10
+
11
+ def call(context)
12
+ controller = context["controller"]
13
+ request = controller.request
14
+
15
+ # input = context["rack.input"].read
16
+ # context["rack.input"].rewind
17
+ # if input.present?
18
+ # params = JSON.parse input
19
+ # if params["network"].present? && params["UserSessionID"].present?
20
+ # request_id = "nsano::request_id::#{params["UserSessionID"]}"
21
+ # context["ussd.request"] = {
22
+ # provider: :nsano,
23
+ # network: params["network"].to_sym,
24
+ # msisdn: Phonelib.parse(params["msisdn"]).e164,
25
+ # type: Config.cache&.read(request_id).present? ? :response : :initial,
26
+ # input: params["msg"].presence,
27
+ # network: params["network"]
28
+ # }
29
+ # end
30
+ # end
31
+
32
+ # status, headers, response = @app.call(context)
33
+
34
+ # if context["ussd.response"].present? && context["ussd.request"][:provider] == :nsano
35
+ # if context["ussd.response"][:type] == :terminal
36
+ # Config.cache&.write(request_id, nil)
37
+ # else
38
+ # Config.cache&.write(request_id, 1)
39
+ # end
40
+
41
+ # status = 200
42
+ # response =
43
+ # {
44
+ # USSDResp: {
45
+ # action: (context["ussd.response"][:type] == :terminal) ? :prompt : :input,
46
+ # menus: "",
47
+ # title: context["ussd.response"][:body]
48
+ # }
49
+ # }.to_json
50
+ # headers = headers.merge({"Content-Type" => "application/json", "Content-Length" => response.bytesize.to_s})
51
+ # response = [response]
52
+ # end
53
+ # [status, headers, response]
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,28 @@
1
+ module FlowChat
2
+ module Ussd
3
+ module Middleware
4
+ class Executor
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(context)
10
+ ussd_app = build_ussd_app context
11
+ flow = context.flow.new ussd_app
12
+ flow.send context["flow.action"]
13
+ rescue FlowChat::Interrupt::Prompt => e
14
+ [:prompt, e.prompt, e.choices]
15
+ rescue FlowChat::Interrupt::Terminate => e
16
+ context.session.destroy
17
+ [:terminate, e.prompt, nil]
18
+ end
19
+
20
+ private
21
+
22
+ def build_ussd_app(context)
23
+ FlowChat::Ussd::App.new(context)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,155 @@
1
+ module FlowChat
2
+ module Ussd
3
+ module Middleware
4
+ class Pagination
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(context)
10
+ @context = context
11
+ @session = context.session
12
+
13
+ if intercept?
14
+ @context["ussd.response"] = handle_intercepted_request
15
+ [200, {}, [""]]
16
+ else
17
+ @session.delete "ussd.pagination"
18
+ res = @app.call(context)
19
+
20
+ if @context["ussd.response"].present?
21
+ @context["ussd.response"] = maybe_paginate @context["ussd.response"]
22
+ end
23
+
24
+ res
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def intercept?
31
+ pagination_state.present? &&
32
+ (pagination_state[:type].to_sym == :terminal ||
33
+ ([Config.pagination_next_option, Config.pagination_back_option].include? @context["ussd.request"][:input]))
34
+ end
35
+
36
+ def handle_intercepted_request
37
+ Config.logger&.info "FlowChat::Middleware::Pagination :: Intercepted to handle pagination"
38
+ start, finish, has_more = calculate_offsets
39
+ type = (pagination_state[:type].to_sym == :terminal && !has_more) ? :terminal : :prompt
40
+ body = pagination_state[:body][start..finish].strip + build_pagination_options(type, has_more)
41
+ set_pagination_state(current_page, start, finish)
42
+
43
+ {body: body, type: type}
44
+ end
45
+
46
+ def maybe_paginate(response)
47
+ if response[:body].length > Config.pagination_page_size
48
+ Config.logger&.info "FlowChat::Middleware::Pagination :: Response length (#{response[:body].length}) exceeds page size (#{Config.pagination_page_size}). Paginating."
49
+ body = response[:body][0..single_option_slice_size]
50
+ # Ensure we do not cut words and options off in the middle.
51
+ current_pagebreak = response[:body][single_option_slice_size + 1].blank? ? single_option_slice_size : body.rindex("\n") || body.rindex(" ") || single_option_slice_size
52
+ set_pagination_state(1, 0, current_pagebreak, response[:body], response[:type])
53
+ response[:body] = body[0..current_pagebreak].strip + "\n\n" + next_option
54
+ response[:type] = :prompt
55
+ end
56
+ response
57
+ end
58
+
59
+ def calculate_offsets
60
+ page = current_page
61
+ offset = pagination_state[:offsets][page]
62
+ if offset.present?
63
+ Config.logger&.debug "FlowChat::Middleware::Pagination :: Reusing cached offset for page: #{page}"
64
+ start = offset[:start]
65
+ finish = offset[:finish]
66
+ has_more = pagination_state[:body].length > finish
67
+ else
68
+ Config.logger&.debug "FlowChat::Middleware::Pagination :: Calculating offset for page: #{page}"
69
+ # We are guaranteed a previous offset because it was set in maybe_paginate
70
+ previous_offset = pagination_state[:offsets][page - 1]
71
+ start = previous_offset[:finish] + 1
72
+ has_more, len = (pagination_state[:body].length > start + single_option_slice_size) ? [true, dual_options_slice_size] : [false, single_option_slice_size]
73
+ finish = start + len
74
+ if start > pagination_state[:body].length
75
+ Config.logger&.debug "FlowChat::Middleware::Pagination :: No content exists for page: #{page}. Reverting to page: #{page - 1}"
76
+ page -= 1
77
+ has_more = false
78
+ start = previous_offset[:start]
79
+ finish = previous_offset[:finish]
80
+ else
81
+ body = pagination_state[:body][start..finish]
82
+ current_pagebreak = pagination_state[:body][finish + 1].blank? ? len : body.rindex("\n") || body.rindex(" ") || len
83
+ finish = start + current_pagebreak
84
+ end
85
+ end
86
+ [start, finish, has_more]
87
+ end
88
+
89
+ def build_pagination_options(type, has_more)
90
+ options_str = ""
91
+ has_less = current_page > 1
92
+ if type.to_sym == :prompt
93
+ options_str += "\n\n"
94
+ next_opt = has_more ? next_option : ""
95
+ back_opt = has_less ? back_option : ""
96
+ options_str += [next_opt, back_opt].join("\n").strip
97
+ end
98
+ options_str
99
+ end
100
+
101
+ def next_option
102
+ "#{Config.pagination_next_option} #{Config.pagination_next_text}"
103
+ end
104
+
105
+ def back_option
106
+ "#{Config.pagination_back_option} #{Config.pagination_back_text}"
107
+ end
108
+
109
+ def single_option_slice_size
110
+ unless @single_option_slice_size.present?
111
+ # To display a single back or next option
112
+ # We accomodate the 2 newlines and the longest of the options
113
+ # We subtract an additional 1 to normalize it for slicing
114
+ @single_option_slice_size = Config.pagination_page_size - 2 - [next_option.length, back_option.length].max - 1
115
+ end
116
+ @single_option_slice_size
117
+ end
118
+
119
+ def dual_options_slice_size
120
+ unless @dual_options_slice_size.present?
121
+ # To display both back and next options
122
+ # We accomodate the 3 newlines and both of the options
123
+ @dual_options_slice_size = Config.pagination_page_size - 3 - [next_option.length, back_option.length].sum - 1
124
+ end
125
+ @dual_options_slice_size
126
+ end
127
+
128
+ def current_page
129
+ current_page = pagination_state[:page]
130
+ if @context["ussd.request"][:input] == Config.pagination_back_option
131
+ current_page -= 1
132
+ elsif @context["ussd.request"][:input] == Config.pagination_next_option
133
+ current_page += 1
134
+ end
135
+ [current_page, 1].max
136
+ end
137
+
138
+ def pagination_state
139
+ @context.session.get("pagination", {})
140
+ end
141
+
142
+ def set_pagination_state(page, offset_start, offset_finish, body = nil, type = nil)
143
+ offsets = pagination_state[:offsets] || {}
144
+ offsets[page] = {start: offset_start, finish: offset_finish}
145
+ @session["ussd.pagination"] = {
146
+ page: page,
147
+ offsets: offsets,
148
+ body: body || pagination_state[:body],
149
+ type: type || pagination_state[:type]
150
+ }
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,52 @@
1
+ module FlowChat
2
+ module Ussd
3
+ module Middleware
4
+ class ResumableSession
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(context)
10
+ if Config.resumable_sessions_enabled && context["ussd.request"].present?
11
+ request = Rack::Request.new(context)
12
+ session = request.session
13
+
14
+ context["ussd.resumable_sessions"] = {}
15
+
16
+ # If this is a new session but we have the flag set, this means the call terminated before
17
+ # the session closed. Force it to resume.
18
+ # This is safe since a new session is started if the old session does not indeed exist.
19
+ if context["ussd.request"][:type] == :initial && can_resume_session?(session)
20
+ context["ussd.request"][:type] = :response
21
+ context["ussd.resumable_sessions"][:resumed] = true
22
+ end
23
+
24
+ res = @app.call(context)
25
+
26
+ if context["ussd.response"].present?
27
+ if context["ussd.response"][:type] == :terminal || context["ussd.resumable_sessions"][:disable]
28
+ session.delete "ussd.resumable_sessions"
29
+ else
30
+ session["ussd.resumable_sessions"] = Time.now.to_i
31
+ end
32
+ end
33
+
34
+ res
35
+ else
36
+ @app.call(context)
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def can_resume_session?(session)
43
+ return unless session["ussd.resumable_sessions"].present?
44
+ return true unless Config.resumable_sessions_timeout_seconds
45
+
46
+ last_active_at = Time.at(session["ussd.resumable_sessions"])
47
+ (Time.now - Config.resumable_sessions_timeout_seconds) < last_active_at
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,54 @@
1
+ require "middleware"
2
+
3
+ module FlowChat
4
+ module Ussd
5
+ class Processor
6
+ attr_reader :middleware, :gateway
7
+
8
+ def initialize(controller)
9
+ @context = FlowChat::Context.new
10
+ @context["controller"] = controller
11
+ @middleware = ::Middleware::Builder.new(name: "ussd.middleware")
12
+
13
+ yield self if block_given?
14
+ end
15
+
16
+ def use_gateway(gateway)
17
+ @gateway = gateway
18
+ self
19
+ end
20
+
21
+ def use_session_store(session_store)
22
+ @context["session.store"] = session_store
23
+ self
24
+ end
25
+
26
+ def use_middleware(middleware)
27
+ @middleware.use middleware
28
+ self
29
+ end
30
+
31
+ def use_resumable_sessions
32
+ middleware.insert_before 0, FlowChat::Ussd::Middleware::ResumableSession
33
+ self
34
+ end
35
+
36
+ def use_pagination
37
+ middleware.use FlowChat::Ussd::Middleware::Pagination
38
+ end
39
+
40
+ def run(flow, action)
41
+ @context["flow.class"] = flow
42
+ @context["flow.action"] = action
43
+
44
+ ::Middleware::Builder.new name: "ussd" do |b|
45
+ b.use gateway
46
+ b.use FlowChat::Session::Middleware
47
+ # b.use FlowChat::Middleware::Pagination
48
+ b.use middleware
49
+ b.use FlowChat::Ussd::Middleware::Executor
50
+ end.inject_logger(Rails.logger).call(@context)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,68 @@
1
+ module FlowChat
2
+ module Ussd
3
+ class Prompt
4
+ attr_reader :user_input
5
+
6
+ def initialize(input)
7
+ @user_input = input
8
+ end
9
+
10
+ def ask(msg, choices: nil, convert: nil, validate: nil, transform: nil)
11
+ if user_input.present?
12
+ input = user_input
13
+ input = convert.call(input) if convert.present?
14
+ validation_error = validate.call(input) if validate.present?
15
+
16
+ prompt!([validation_error, msg].join("\n\n"), choices:) if validation_error.present?
17
+
18
+ input = transform.call(input) if transform.present?
19
+ return input
20
+ end
21
+
22
+ prompt! msg, choices:
23
+ end
24
+
25
+ def say(message)
26
+ terminate! message
27
+ end
28
+
29
+ def select(msg, choices)
30
+ choices, choices_prompt = build_select_choices choices
31
+ ask(
32
+ msg,
33
+ choices: choices_prompt,
34
+ convert: lambda { |choice| choice.to_i },
35
+ validate: lambda { |choice| "Invalid selection:" unless (1..choices.size).include?(choice) },
36
+ transform: lambda { |choice| choices[choice - 1] }
37
+ )
38
+ end
39
+
40
+ def yes?(msg)
41
+ select(msg, ["Yes", "No"]) == "Yes"
42
+ end
43
+
44
+ private
45
+
46
+ def build_select_choices(choices)
47
+ case choices
48
+ when Array
49
+ choices_prompt = choices.map.with_index { |c, i| [i + 1, c] }.to_h
50
+ when Hash
51
+ choices_prompt = choices.values.map.with_index { |c, i| [i + 1, c] }.to_h
52
+ choices = choices.keys
53
+ else
54
+ raise ArgumentError, "choices must be an array or hash"
55
+ end
56
+ [choices, choices_prompt]
57
+ end
58
+
59
+ def prompt!(msg, choices:)
60
+ raise FlowChat::Interrupt::Prompt.new(msg, choices:)
61
+ end
62
+
63
+ def terminate!(msg)
64
+ raise FlowChat::Interrupt::Terminate.new(msg)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,51 @@
1
+ module FlowChat
2
+ module Ussd
3
+ module Simulator
4
+ module Controller
5
+ def ussd_simulator
6
+ respond_to do |format|
7
+ format.html do
8
+ render inline: simulator_view_template, layout: false, locals: simulator_locals
9
+ end
10
+ end
11
+ end
12
+
13
+ protected
14
+
15
+ def show_options
16
+ true
17
+ end
18
+
19
+ def default_msisdn
20
+ "233200123456"
21
+ end
22
+
23
+ def default_endpoint
24
+ "/ussd"
25
+ end
26
+
27
+ def default_provider
28
+ :nalo
29
+ end
30
+
31
+ def simulator_view_template
32
+ File.read simulator_view_path
33
+ end
34
+
35
+ def simulator_view_path
36
+ File.join FlowChat.root.join("flow_chat", "ussd", "simulator", "views", "simulator.html.erb")
37
+ end
38
+
39
+ def simulator_locals
40
+ {
41
+ pagesize: Config.pagination_page_size,
42
+ show_options: show_options,
43
+ default_msisdn: default_msisdn,
44
+ default_endpoint: default_endpoint,
45
+ default_provider: default_provider
46
+ }
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,239 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>FlowChat Ussd Simulator</title>
5
+ <style>
6
+ .content {
7
+ width: 320px;
8
+ margin: 100px auto;
9
+ }
10
+ .label {
11
+ display: inline-block;
12
+ width: 80px;
13
+ font-weight: bold;
14
+ }
15
+ .value {
16
+ display: inline;
17
+ }
18
+ .value select, input {
19
+ width: 200px;
20
+ }
21
+ .field {
22
+ margin: 5px;
23
+ }
24
+ #screen {
25
+ border: 1px black solid;
26
+ height:400px;
27
+ width:300px;
28
+ margin-top: 10px;
29
+ margin-bottom: 10px;
30
+ padding: 10px 5px;
31
+ }
32
+ #char-count {
33
+ text-align: center;
34
+ font-size: 10px;
35
+ }
36
+ .hidden {
37
+ display: none;
38
+ }
39
+ </style>
40
+ </head>
41
+ <body>
42
+ <div class="content">
43
+ <div class="field hidden<%= show_options ? '' : 'hidden' %>">
44
+ <div class="label">Provider </div>
45
+ <div class="value">
46
+ <select id="provider">
47
+ <option <%= default_provider == :nalo ? 'selected' : '' %> value="nalo">Nalo</option>
48
+ <option <%= default_provider == :nsano ? 'selected' : '' %> value="nsano">Nsano</option>
49
+ </select>
50
+ </div>
51
+ </div>
52
+ <div class="field <%= show_options ? '' : 'hidden' %>">
53
+ <div class="label">Endpoint </div>
54
+ <div class="value">
55
+ <input id="endpoint" value="<%= default_endpoint %>" />
56
+ </div>
57
+ </div>
58
+ <div class="field <%= show_options ? '' : 'hidden' %>">
59
+ <div class="label">MSISDN </div>
60
+ <div class="value">
61
+ <input id="msisdn" value="<%= default_msisdn %>" />
62
+ </div>
63
+ </div>
64
+ <div id="screen"></div>
65
+ <div id="char-count"></div>
66
+ <div class="field">
67
+ <input id="data" disabled> <button id="respond" disabled>Respond</button>
68
+ </div>
69
+ <div class="field">
70
+ <button id="initiate" disabled>Initiate</button>
71
+ <button id="reset" disabled>Reset</button>
72
+ </div>
73
+ </div>
74
+ <script>
75
+ // Config
76
+ const pagesize = <%= pagesize %>
77
+
78
+ // View
79
+ const $screen = document.getElementById('screen')
80
+ const $charCount = document.getElementById('char-count')
81
+
82
+ const $provider = document.getElementById('provider')
83
+ const $endpoint = document.getElementById('endpoint')
84
+ const $msisdn = document.getElementById('msisdn')
85
+
86
+ const $data = document.getElementById('data')
87
+ const $respondBtn = document.getElementById('respond')
88
+ const $initiateBtn = document.getElementById('initiate')
89
+ const $resetBtn = document.getElementById('reset')
90
+
91
+ $provider.addEventListener('change', function (e) {
92
+ state.provider = $provider.value
93
+ render()
94
+ }, false)
95
+
96
+ $endpoint.addEventListener('keyup', function (e) {
97
+ state.endpoint = $endpoint.value
98
+ render()
99
+ }, false)
100
+
101
+ $msisdn.addEventListener('keyup', function (e) {
102
+ state.msisdn = $msisdn.value
103
+ render()
104
+ }, false)
105
+
106
+ $initiateBtn.addEventListener('click', function (e) {
107
+ makeRequest()
108
+ }, false)
109
+
110
+ $resetBtn.addEventListener('click',function(e){
111
+ reset()
112
+ }, false)
113
+
114
+ $respondBtn.addEventListener('click', function (e) {
115
+ makeRequest()
116
+ }, false)
117
+
118
+ function disableInputs() {
119
+ $data.disabled = 'disabled'
120
+ $respondBtn.disabled = 'disabled'
121
+ $initiateBtn.disabled = 'disabled'
122
+ $resetBtn.disabled = 'disabled'
123
+ $data.disabled = 'disabled'
124
+ }
125
+
126
+ function enableResponse() {
127
+ $data.disabled = false
128
+ $respondBtn.disabled = false
129
+ $resetBtn.disabled = false
130
+ }
131
+
132
+ function display(text) {
133
+ $screen.innerText = text.substr(0, pagesize)
134
+ if(text.length > 0)
135
+ $charCount.innerText = `${text.length} chars`
136
+ else
137
+ $charCount.innerText = ''
138
+ }
139
+
140
+ function render() {
141
+ disableInputs()
142
+
143
+ if(!state.isRunning){
144
+ if(state.provider && state.endpoint && state.msisdn)
145
+ $initiateBtn.disabled = false
146
+ else
147
+ $initiateBtn.disabled = 'disabled'
148
+ }
149
+ else {
150
+ enableResponse()
151
+ }
152
+ }
153
+
154
+ // State
155
+ const state = {}
156
+
157
+ function reset(shouldRender) {
158
+ state.isRunning = false
159
+ state.request_id = btoa(Math.random().toString()).substr(10, 10)
160
+ state.provider = $provider.value
161
+ state.endpoint = $endpoint.value
162
+ state.msisdn = $msisdn.value
163
+
164
+ $data.value = null
165
+
166
+ display("")
167
+ if(shouldRender !== false) render()
168
+ }
169
+
170
+
171
+ // API
172
+
173
+ function makeRequest() {
174
+ var data = {}
175
+
176
+ switch (state.provider) {
177
+ case "nalo":
178
+ data = {
179
+ USERID: state.request_id,
180
+ MSISDN: state.msisdn,
181
+ USERDATA: $data.value,
182
+ MSGTYPE: !state.isRunning,
183
+ }
184
+ break;
185
+ case "nsano":
186
+ data = {
187
+ network: 'MTN',
188
+ msisdn: state.msisdn,
189
+ msg: $data.value,
190
+ UserSessionID: state.request_id,
191
+ }
192
+ break;
193
+
194
+ default:
195
+ alert(`Unhandled provider request: ${state.provider}`)
196
+ return
197
+ }
198
+
199
+ disableInputs()
200
+ fetch(state.endpoint, {
201
+ method: 'POST',
202
+ headers: {
203
+ 'Content-Type': 'application/json'
204
+ },
205
+ redirect: 'error',
206
+ body: JSON.stringify(data)
207
+ })
208
+ .then(response => {
209
+ if (!response.ok) {
210
+ throw Error(`${response.status}: ${response.statusText}`);
211
+ }
212
+ return response.json()
213
+ })
214
+ .then(data => {
215
+ switch (state.provider) {
216
+ case "nalo":
217
+ display(data.MSG)
218
+ state.isRunning = data.MSGTYPE
219
+ break;
220
+ case "nsano":
221
+ display(data.USSDResp.title)
222
+ state.isRunning = data.USSDResp.action == "input"
223
+ break;
224
+
225
+ default:
226
+ alert(`Unhandled provider response: ${state.provider}`)
227
+ return
228
+ }
229
+ $data.value = null
230
+ })
231
+ .catch(error => alert(error.message))
232
+ .finally(render);
233
+ }
234
+
235
+ // run the app
236
+ reset()
237
+ </script>
238
+ </body>
239
+ </html>
@@ -0,0 +1,3 @@
1
+ module FlowChat
2
+ VERSION = "0.1.0"
3
+ end
data/lib/flow_chat.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "zeitwerk"
2
+ require "active_support"
3
+
4
+ loader = Zeitwerk::Loader.for_gem
5
+ loader.enable_reloading if defined?(Rails.env) && Rails.env.development?
6
+ loader.setup
7
+
8
+ module FlowChat
9
+ def self.root
10
+ Pathname.new __dir__
11
+ end
12
+ end
13
+
14
+ loader.eager_load
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flow_chat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stefan Froelich
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: zeitwerk
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: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: actionpack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '6'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: phonelib
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: ibsciss-middleware
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.4.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.4.2
83
+ description: Enable USSD processing support in Rails.
84
+ email:
85
+ - sfroelich01@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - flow_chat.gemspec
100
+ - lib/flow_chat.rb
101
+ - lib/flow_chat/config.rb
102
+ - lib/flow_chat/context.rb
103
+ - lib/flow_chat/interrupt.rb
104
+ - lib/flow_chat/session/middleware.rb
105
+ - lib/flow_chat/session/rails_session_store.rb
106
+ - lib/flow_chat/ussd/app.rb
107
+ - lib/flow_chat/ussd/gateway/nalo.rb
108
+ - lib/flow_chat/ussd/gateway/nsano.rb
109
+ - lib/flow_chat/ussd/middleware/executor.rb
110
+ - lib/flow_chat/ussd/middleware/pagination.rb
111
+ - lib/flow_chat/ussd/middleware/resumable_session.rb
112
+ - lib/flow_chat/ussd/processor.rb
113
+ - lib/flow_chat/ussd/prompt.rb
114
+ - lib/flow_chat/ussd/simulator/controller.rb
115
+ - lib/flow_chat/ussd/simulator/views/simulator.html.erb
116
+ - lib/flow_chat/version.rb
117
+ homepage: https://github.com/ussd-engine/api-base
118
+ licenses:
119
+ - MIT
120
+ metadata:
121
+ allowed_push_host: https://rubygems.org
122
+ homepage_uri: https://github.com/ussd-engine/api-base
123
+ source_code_uri: https://github.com/ussd-engine/api-base
124
+ changelog_uri: https://github.com/ussd-engine/api-base
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: 2.3.0
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubygems_version: 3.5.6
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Enable USSD processing support in Rails.
144
+ test_files: []