stealth-webhook 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: 2ee61ffe618083ffd5c17136fb05b65b0bd4ce7ff6b6c8a50c999791b78547d1
4
+ data.tar.gz: 1a95943f90f7a9c372dab8e3844555009c34c24056e97d01b706d45c7ec8b7c1
5
+ SHA512:
6
+ metadata.gz: 6370d5dfecb8a2bc448667bb11f7bd0d91ee5be30d1e0665a4e4b4f1c88a5fc592b81e321b289cc7db41367b7c979bc7ef53a4d42633a98d6139284b79f87292
7
+ data.tar.gz: 4adca9140cf08980b73235d143ca179a81f9eab4e4df571a65bdfdf200fb4c2a7eb921f3313e7ab97964ec700f7819bc61919485243dcb360dd2155ae1319582
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ # please add general patterns to your global ignore list
2
+ # see https://github.com/github/gitignore#readme
3
+ .DS_STORE
4
+ *.swp
5
+ *.rbc
6
+ *.sass-cache
7
+ /pkg
8
+ /doc/api
9
+ /coverage
10
+ .yardoc
11
+ doc/
12
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,38 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ stealth-webhook (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ diff-lcs (1.3)
11
+ method_source (0.9.0)
12
+ pry (0.11.3)
13
+ coderay (~> 1.1.0)
14
+ method_source (~> 0.9.0)
15
+ rspec (3.7.0)
16
+ rspec-core (~> 3.7.0)
17
+ rspec-expectations (~> 3.7.0)
18
+ rspec-mocks (~> 3.7.0)
19
+ rspec-core (3.7.1)
20
+ rspec-support (~> 3.7.0)
21
+ rspec-expectations (3.7.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.7.0)
24
+ rspec-mocks (3.7.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.7.0)
27
+ rspec-support (3.7.1)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ pry (~> 0.10)
34
+ rspec (~> 3.6)
35
+ stealth-webhook!
36
+
37
+ BUNDLED WITH
38
+ 1.16.2
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ Stealth Webhook give to [Stealth](https://hellostealth.com) a webhook URL to send messages.
2
+
3
+ PS: Is not generic (YET).
4
+ - Uses ActiveRecord
5
+ - For Facebook Messenger only
6
+ - Static defined fields (yet)
7
+
8
+
9
+ ## Getting Started
10
+
11
+ Getting started with Stealth Wehook is simple:
12
+
13
+
14
+ Add to Gemfile
15
+
16
+
17
+ ```
18
+ gem 'stealth-webhook'
19
+ ```
20
+
21
+ And send a simple notification
22
+
23
+ curl -H "Content-Type: application/json" -d '{"recipient": {"fb_id": "1540684789327149"}, "message": "This is a simple notification"}' -X POST http://localhost:3000/api/v1/webhook
24
+
25
+
26
+
27
+ ## Thanks
28
+
29
+ Stealth wouldn't exist without the great work of many other open source projects and people including:
30
+
31
+ * [Stealth](https://hellostealth.com) for creating this awesome bot framework to Ruby.
32
+ * [Defensoria Pública do Estado do Tocantins](http://www.defensoria.to.def.br) for belive in this project.
33
+
34
+ ## License
35
+
36
+ Stealth Webhook source code is released under the MIT License.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1 @@
1
+ require 'stealth-webhook/base'
@@ -0,0 +1,12 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ # core
5
+ require 'stealth-webhook/overriding/server'
6
+ require 'stealth-webhook/overriding/scheduled_reply'
7
+
8
+ require 'stealth-webhook/webhook'
9
+ require 'stealth-webhook/version'
10
+
11
+ module StealthWebhook
12
+ end
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Stealth
5
+
6
+ class ScheduledReplyJob < Stealth::Jobs
7
+ sidekiq_options queue: :webhooks, retry: false
8
+
9
+ def perform(service, user_id, flow, state, message)
10
+ service_message = ServiceMessage.new(service: service)
11
+ service_message.message = message
12
+ service_message.sender_id = user_id
13
+ controller = BotController.new(service_message: service_message)
14
+ controller.update_session_to(flow: flow, state: state)
15
+ controller.route
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+ module Stealth
4
+ class Server < Sinatra::Base
5
+
6
+ post '/api/v1/webhook' do
7
+
8
+ params = JSON.parse(request.body.read, :symbolize_names => true)
9
+ Stealth::Logger.l(topic: 'webhook', message: "Webhook received data: #{params}")
10
+
11
+ response_status, response_body = send_message(params, request.env['HTTP_AUTHORIZATION'])
12
+
13
+ status response_status
14
+ body response_body
15
+ end
16
+
17
+
18
+ def send_message(params, auth_token = nil)
19
+ return [401, 'Unauthorized: Invalid Token'] if ENV['WEHBOOK_AUTH_TOKEN'] && auth_token != "Token #{ENV['WEHBOOK_AUTH_TOKEN']}"
20
+
21
+ begin
22
+ webhook = StealthWebhook::Webhook.new
23
+ webhook.send_message(params)
24
+ rescue => e
25
+ [503, "Service Unavailable: #{e.message}"]
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module StealthWebhook
5
+ module Version
6
+ def self.version
7
+ File.read(File.join(File.dirname(__FILE__), '../..', 'VERSION')).strip
8
+ end
9
+ end
10
+
11
+ VERSION = Version.version
12
+ end
@@ -0,0 +1,70 @@
1
+ module StealthWebhook
2
+ class Webhook
3
+ attr_reader :errors
4
+ StructUser = Struct.new(:fb_id)
5
+
6
+ def initialize
7
+ @errors = {}
8
+ end
9
+
10
+ def send_message(params)
11
+ if validate_and_send_message(params)
12
+ [200, 'Message sent with success']
13
+ else
14
+ [@errors[:status], @errors.to_json]
15
+ end
16
+ rescue => e
17
+ [500, e.message]
18
+ end
19
+
20
+
21
+ def validate_and_send_message(params)
22
+ return unless validate(params)
23
+ user = recipient(params)
24
+ return unless user
25
+
26
+ deliver(user.fb_id, params[:message])
27
+ end
28
+
29
+ def deliver(recipient, message)
30
+ Stealth::ScheduledReplyJob.perform_in(0, 'facebook', recipient, 'webhook', 'say_notification', message)
31
+ true
32
+ end
33
+
34
+
35
+ private
36
+
37
+
38
+ def validate(params)
39
+
40
+ unless params[:recipient].respond_to?(:dig)
41
+ @errors = {message: ":recipient must be a hash : #{params[:recipient].inspect}", code: 400, status: :bad_request}
42
+ return
43
+ end
44
+
45
+ unless params.dig(:recipient, :assistido_id) || params.dig(:recipient, :assistido_cpf) || params.dig(:recipient, :fb_id)
46
+ @errors= {message: ':recipient must have the follow keys: assistido_id ou assistido_cpf', code: 400, status: :bad_request}
47
+ return
48
+ end
49
+ true
50
+ end
51
+
52
+ def recipient(params)
53
+
54
+ if params.dig(:recipient, :fb_id)
55
+ user = StructUser.new(params.dig(:recipient, :fb_id))
56
+ else
57
+ query_params = {
58
+ solar_id: params.dig(:recipient, :assistido_id),
59
+ cpf: params.dig(:recipient, :assistido_cpf)
60
+ }.compact
61
+
62
+ user = User.where(query_params).first
63
+ end
64
+
65
+ @errors= {message: "recipient not found: #{query_params.to_json})", code: 404, status: :not_found} unless user
66
+ user
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,13 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ require 'rspec'
7
+ require 'pry'
8
+
9
+ require 'stealth-webhook'
10
+
11
+ RSpec.configure do |config|
12
+ ENV['STEALTH_ENV'] = 'test'
13
+ end
@@ -0,0 +1,17 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
5
+
6
+ describe "StealthWebhook::Version" do
7
+
8
+ let(:version_in_file) { File.read(File.join(File.dirname(__FILE__), '..', 'VERSION')).strip }
9
+
10
+ it "should return the current gem version" do
11
+ expect(StealthWebhook::Version.version).to eq version_in_file
12
+ end
13
+
14
+ it "should return the current gem version via a constant" do
15
+ expect(StealthWebhook::VERSION).to eq version_in_file
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
2
+
3
+ version = File.read(File.join(File.dirname(__FILE__), 'VERSION')).strip
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'stealth-webhook'
7
+ s.summary = 'Add Webhook to Stealth'
8
+ s.description = 'Simplify handle of payload and messages'
9
+ s.homepage = 'https://github.com/luizcarvalho/stealth-webhook'
10
+ s.licenses = ['MIT']
11
+ s.version = version
12
+ s.authors = ['Luiz Carvalho']
13
+ s.email = 'contato@luizcarvalho.com'
14
+
15
+ s.add_development_dependency 'rspec', '~> 3.6'
16
+ s.add_development_dependency 'pry', '~> 0.10'
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.require_paths = ['lib']
21
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stealth-webhook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Luiz Carvalho
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.10'
41
+ description: Simplify handle of payload and messages
42
+ email: contato@luizcarvalho.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".gitignore"
48
+ - Gemfile
49
+ - Gemfile.lock
50
+ - README.md
51
+ - VERSION
52
+ - lib/stealth-webhook.rb
53
+ - lib/stealth-webhook/base.rb
54
+ - lib/stealth-webhook/overriding/scheduled_reply.rb
55
+ - lib/stealth-webhook/overriding/server.rb
56
+ - lib/stealth-webhook/version.rb
57
+ - lib/stealth-webhook/webhook.rb
58
+ - spec/spec_helper.rb
59
+ - spec/version_spec.rb
60
+ - stealth-webhook.gemspec
61
+ homepage: https://github.com/luizcarvalho/stealth-webhook
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.7.6
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Add Webhook to Stealth
85
+ test_files: []