hahamut 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: 1e245ce79903796e15236e1b5b3e3f0086df00ee0cb5113853dd65edecc7c97a
4
+ data.tar.gz: 277a9346c3f4417a4bfbd9dc9c6722acd315d854a2daf55ecf82361ee7470375
5
+ SHA512:
6
+ metadata.gz: 60823d6acaa8f3113557c4e1edabb902d36597378f480d5afc478c27291bc78f0de9bb1769d002e5767c56ea9b788b56f265a0e746b986bf0aa775597a2ed4c1
7
+ data.tar.gz: '083d00fb3ac48504c8d6942e6edd3d782cd18bfe8c91aadfd9b80caf162cf3ffa20d57fd574091e0f46d33b2175fcd135094199630d1f0128bf1acc868327e86'
data/.gitignore ADDED
@@ -0,0 +1,11 @@
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
data/.overcommit.yml ADDED
@@ -0,0 +1,22 @@
1
+ PreCommit:
2
+ AuthorName:
3
+ enabled: false
4
+ RuboCop:
5
+ enabled: true
6
+ on_warn: fail # Treat all warnings as failures
7
+ TrailingWhitespace:
8
+ enabled: true
9
+ exclude:
10
+ - '.licenses/**/*'
11
+
12
+ PrePush:
13
+ BundleAudit:
14
+ enabled: true
15
+ BundleOutdated:
16
+ enabled: true
17
+
18
+ PostCheckout:
19
+ ALL: # Special hook name that customizes all hooks of this type
20
+ quiet: true # Change all post-checkout hooks to only display output on failure
21
+ IndexTags:
22
+ enabled: true # Generate a tags file with `ctags` each time HEAD changes
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.1
7
+ before_install: gem install bundler -v 1.17.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at elct9620@frost.tw. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in hahamut.gemspec
8
+ gemspec
9
+
10
+ gem 'pry'
data/Gemfile.lock ADDED
@@ -0,0 +1,79 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hahamut (0.1.0)
5
+ rack
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.0)
11
+ bundler-audit (0.6.1)
12
+ bundler (>= 1.2.0, < 3)
13
+ thor (~> 0.18)
14
+ childprocess (1.0.1)
15
+ rake (< 13.0)
16
+ coderay (1.1.2)
17
+ diff-lcs (1.3)
18
+ docile (1.3.2)
19
+ iniparse (1.4.4)
20
+ jaro_winkler (1.5.3)
21
+ json (2.2.0)
22
+ method_source (0.9.2)
23
+ overcommit (0.49.0)
24
+ childprocess (>= 0.6.3, < 2.0)
25
+ iniparse (~> 1.4)
26
+ parallel (1.17.0)
27
+ parser (2.6.3.0)
28
+ ast (~> 2.4.0)
29
+ pry (0.12.2)
30
+ coderay (~> 1.1.0)
31
+ method_source (~> 0.9.0)
32
+ rack (2.0.7)
33
+ rainbow (3.0.0)
34
+ rake (10.5.0)
35
+ rspec (3.8.0)
36
+ rspec-core (~> 3.8.0)
37
+ rspec-expectations (~> 3.8.0)
38
+ rspec-mocks (~> 3.8.0)
39
+ rspec-core (3.8.2)
40
+ rspec-support (~> 3.8.0)
41
+ rspec-expectations (3.8.4)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.8.0)
44
+ rspec-mocks (3.8.1)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.8.0)
47
+ rspec-support (3.8.2)
48
+ rubocop (0.73.0)
49
+ jaro_winkler (~> 1.5.1)
50
+ parallel (~> 1.10)
51
+ parser (>= 2.6)
52
+ rainbow (>= 2.2.2, < 4.0)
53
+ ruby-progressbar (~> 1.7)
54
+ unicode-display_width (>= 1.4.0, < 1.7)
55
+ ruby-progressbar (1.10.1)
56
+ simplecov (0.17.0)
57
+ docile (~> 1.1)
58
+ json (>= 1.8, < 3)
59
+ simplecov-html (~> 0.10.0)
60
+ simplecov-html (0.10.2)
61
+ thor (0.20.3)
62
+ unicode-display_width (1.6.0)
63
+
64
+ PLATFORMS
65
+ ruby
66
+
67
+ DEPENDENCIES
68
+ bundler (~> 1.17)
69
+ bundler-audit
70
+ hahamut!
71
+ overcommit
72
+ pry
73
+ rake (~> 10.0)
74
+ rspec (~> 3.0)
75
+ rubocop (~> 0.73.0)
76
+ simplecov
77
+
78
+ BUNDLED WITH
79
+ 1.17.3
data/README.md ADDED
@@ -0,0 +1,117 @@
1
+ # Hahamut
2
+
3
+ The [Bahamut Forum](https://gamer.com.tw)'s Chatbot ruby client implements
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'hahamut'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install hahamut
20
+
21
+ ## Usage
22
+
23
+ If you are using Rack, just run it
24
+
25
+ ```ruby
26
+ run Hahamut::Engine
27
+ ```
28
+
29
+ If you are using Rails, mount it in your `config/routes.rb`
30
+
31
+ ```ruby
32
+ mount Hahamut::Engine => '/hahamut'
33
+ ```
34
+
35
+ ### Configure
36
+
37
+ This gem is support multiple chatbot, we can use `#setup` to configure it.
38
+
39
+ ```ruby
40
+ # config/initializers/hahamut.rb
41
+
42
+ Hahamut::Engine.setup do |config|
43
+
44
+ # Strong recommendation to put in credentials or environment variables
45
+ config.register bot_id: 'YOUR_BOT_ID',
46
+ token: 'YOUR_BOT_TOKEN',
47
+ secret: 'YOUR_BOT_SECRET'
48
+ end
49
+ ```
50
+
51
+ ### Handling Event
52
+
53
+ When you setup Hahamut, you can set handler
54
+
55
+ ```ruby
56
+ # config/initializers/hahamut.rb
57
+
58
+ Hahamut::Engine.setup do |config|
59
+
60
+ # Your bot register
61
+
62
+ # Option 1
63
+ config.on_message do |bot, event|
64
+ # Implement your bot behavior
65
+ end
66
+
67
+ # Option 2
68
+ config.on_message ChatbotHandler
69
+ end
70
+ ```
71
+
72
+ > If you want to use the handler object, you must implement a class method `#call`
73
+
74
+ ### Sending Message
75
+
76
+ Current support message types
77
+
78
+ * Text
79
+ * Image
80
+ * Sticker
81
+ * BotStart
82
+ * BotEvent
83
+
84
+ When you receive a message, you will get the bot instance and event.
85
+ To send a message, you have to use bot instance to send a message object.
86
+
87
+ ```ruby
88
+ message = Hahamut::Message::Text.new(text: 'Hello World')
89
+ bot.send_to 'RECEIPIENT_ID', message
90
+ ```
91
+
92
+ > BotStart can accept `init` attribute with BotEvent, it can help you write less JSON for it.
93
+ > This feature will be improved in the future to make it easier to use.
94
+
95
+ ### Upload Image
96
+
97
+ Same as send a message, we need a bot instance.
98
+ The result is a `Hahamut::Message::Image` object and you can send to the user directly.
99
+
100
+ ```ruby
101
+ image = bot.upload('IMAGE_PATH')
102
+ bot.send_to 'RECEIPIENT_ID', image
103
+ ```
104
+
105
+ ## Development
106
+
107
+ 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.
108
+
109
+ 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).
110
+
111
+ ## Contributing
112
+
113
+ Bug reports and pull requests are welcome on GitHub at https://github.com/elct9620/hahamut. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
114
+
115
+ ## Code of Conduct
116
+
117
+ Everyone interacting in the Hahamut project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/elct9620/hahamut/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'hahamut'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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/hahamut.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'hahamut/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'hahamut'
9
+ spec.version = Hahamut::VERSION
10
+ spec.authors = ['蒼時弦也']
11
+ spec.email = ['concat@frost.tw']
12
+
13
+ spec.summary = 'The Hahamut client'
14
+ spec.description = 'This is the client for messaging app ' \
15
+ 'Hahamut build by gamer.com.tw'
16
+ spec.homepage = 'https://github.com/elct9620/hahamut'
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that
20
+ # have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`
23
+ .split("\x0")
24
+ .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 'rack'
31
+
32
+ spec.add_development_dependency 'bundler', '~> 1.17'
33
+ spec.add_development_dependency 'bundler-audit'
34
+ spec.add_development_dependency 'overcommit'
35
+ spec.add_development_dependency 'rake', '~> 10.0'
36
+ spec.add_development_dependency 'rspec', '~> 3.0'
37
+ spec.add_development_dependency 'rubocop', '~> 0.73.0'
38
+ spec.add_development_dependency 'simplecov'
39
+ end
data/lib/hahamut.rb ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'hahamut/version'
4
+ require 'hahamut/client'
5
+ require 'hahamut/engine'
6
+ require 'hahamut/handler'
7
+ require 'hahamut/manager'
8
+ require 'hahamut/signature'
9
+ require 'hahamut/attribute'
10
+ require 'hahamut/support'
11
+ require 'hahamut/event'
12
+ require 'hahamut/message'
13
+ require 'hahamut/sender'
14
+ require 'hahamut/uploader'
15
+
16
+ module Hahamut
17
+ class Error < StandardError; end
18
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'hahamut/attribute/base'
4
+ require 'hahamut/attribute/string'
5
+ require 'hahamut/attribute/integer'
6
+ require 'hahamut/attribute/timestamp'
7
+ require 'hahamut/attribute/json'
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ module Attribute
5
+ # Base value
6
+ class Base
7
+ attr_reader :before_cast
8
+
9
+ def initialize(value)
10
+ @before_cast = value
11
+ end
12
+
13
+ def value
14
+ @value ||= before_cast
15
+ end
16
+
17
+ def as_json
18
+ value
19
+ end
20
+
21
+ def to_json(_obj = nil)
22
+ as_json.to_json
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ module Attribute
5
+ # Integer Type
6
+ class Integer < Base
7
+ def value
8
+ @value ||= before_cast.to_i
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ module Attribute
5
+ # JSON Attribute
6
+ class Json < Base
7
+ def value
8
+ @value ||= before_cast.as_json if before_cast.respond_to?(:as_json)
9
+ @value ||= before_cast if before_cast.is_a?(Hash)
10
+ @value ||= {}
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ module Attribute
5
+ # String Type
6
+ class String < Base
7
+ def value
8
+ @value ||= before_cast.to_s
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ module Attribute
5
+ # Timestamp Attribute
6
+ class Timestamp < Base
7
+ def value
8
+ @value ||= Time.at(before_cast.to_i / 1000)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ # Hahamut Client
5
+ class Client
6
+ attr_reader :id, :token, :secret, :sender, :uploader
7
+
8
+ def initialize(bot_id, token, secret)
9
+ @id = bot_id
10
+ @token = token
11
+ @secret = secret
12
+ @sender = Sender.new(@token)
13
+ @uploader = Uploader.new(@id, @token)
14
+
15
+ Manager.register(self)
16
+ end
17
+
18
+ def send_to(recipient, message)
19
+ @sender.send(recipient, message)
20
+ end
21
+
22
+ def upload(path)
23
+ @uploader.upload(path)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+
5
+ module Hahamut
6
+ # Hahamut Engine
7
+ class Engine
8
+ include Singleton
9
+
10
+ class << self
11
+ def call(env)
12
+ Handler.new(env).process
13
+ end
14
+
15
+ def setup(&_block)
16
+ yield Manager.instance
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ # Hahamut Webhook Event
5
+ class Event
6
+ include Support::Attributes
7
+
8
+ attribute :botid, :string
9
+ attribute :time, :timestamp
10
+
11
+ def initialize(attributes)
12
+ assign_attributes(attributes)
13
+
14
+ @attributes[:messaging] ||= []
15
+ end
16
+
17
+ def messaging
18
+ @attributes[:messaging]
19
+ end
20
+
21
+ def messaging=(messages)
22
+ @attributes[:messaging] =
23
+ messages
24
+ &.map { |message| Message.parse(message) }
25
+ &.compact
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rack'
4
+ require 'json'
5
+
6
+ module Hahamut
7
+ # Handling HTTP Request
8
+ class Handler
9
+ SIGNATURE_HEADER = 'HTTP_X_BAHA_DATA_SIGNATURE'
10
+
11
+ attr_reader :request, :response
12
+
13
+ def initialize(env)
14
+ @request = Rack::Request.new(env)
15
+ end
16
+
17
+ def process
18
+ return invalid_client if client.nil?
19
+ return invalid_signature unless valid_signature?
20
+
21
+ Manager.handle(client, Event.new(body))
22
+
23
+ Rack::Response.new([], 204)
24
+ end
25
+
26
+ def body
27
+ @body ||= JSON.parse(raw)
28
+ end
29
+
30
+ def client
31
+ Manager.find(body['botid'])
32
+ end
33
+
34
+ def raw
35
+ return @raw unless @raw.nil?
36
+
37
+ @raw = @request.body.read
38
+ @request.body.rewind
39
+ @raw
40
+ end
41
+
42
+ private
43
+
44
+ def invalid_signature
45
+ Rack::Response.new([], 401)
46
+ end
47
+
48
+ def invalid_client
49
+ Rack::Response.new([], 404)
50
+ end
51
+
52
+ def valid_signature?
53
+ return false unless @request.has_header?(SIGNATURE_HEADER)
54
+
55
+ signature = @request.get_header(SIGNATURE_HEADER)
56
+ Signature.new(client.secret, raw).match?(signature)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+
5
+ module Hahamut
6
+ # Client Manager
7
+ class Manager
8
+ include Singleton
9
+
10
+ class << self
11
+ def register(client)
12
+ instance.register(client)
13
+ end
14
+
15
+ def find(id)
16
+ instance.get(id)
17
+ end
18
+
19
+ def handle(client, message)
20
+ instance.handler&.call(client, message)
21
+ end
22
+ end
23
+
24
+ attr_accessor :handler
25
+
26
+ def initialize
27
+ @clients = {}
28
+ end
29
+
30
+ def register(args)
31
+ case args
32
+ when Hash then client = create_from(args)
33
+ when Client then client = args
34
+ end
35
+
36
+ @clients[client.id] = client
37
+ end
38
+
39
+ def get(id)
40
+ @clients[id]
41
+ end
42
+
43
+ def on_message(&block)
44
+ @handler = block
45
+ end
46
+
47
+ private
48
+
49
+ def create_from(args)
50
+ Client.new(
51
+ args[:bot_id],
52
+ args[:token],
53
+ args[:secret]
54
+ )
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'hahamut/message/base'
4
+ require 'hahamut/message/text'
5
+ require 'hahamut/message/image'
6
+ require 'hahamut/message/sticker'
7
+ require 'hahamut/message/command'
8
+ require 'Hahamut/message/bot_start'
9
+ require 'Hahamut/message/bot_event'
10
+
11
+ module Hahamut
12
+ # Hahamut Message
13
+ module Message
14
+ class << self
15
+ def types
16
+ @types ||= (constants - [:Base]).map { |name| const_get(name) }
17
+ end
18
+
19
+ def parse(message)
20
+ # TODO: Improve type match
21
+ klass = types.find { |type| type.match?(message) }
22
+ klass&.new(message['message'], message['sender_id'])
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ module Message
5
+ # Abstract Message
6
+ class Base
7
+ class << self
8
+ def match?(_message)
9
+ false
10
+ end
11
+ end
12
+
13
+ include Support::Attributes
14
+
15
+ attr_reader :sender_id
16
+
17
+ # TODO: Improve sender / recipient setup
18
+ def initialize(attributes, sender_id = nil)
19
+ assign_attributes(attributes)
20
+ @sender_id = sender_id
21
+ end
22
+
23
+ def type
24
+ raise NotImplementedError
25
+ end
26
+
27
+ def as_json(_obj = nil)
28
+ @attributes.dup.merge(type: type)
29
+ end
30
+
31
+ def to_json(_obj = nil)
32
+ as_json.to_json
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ module Message
5
+ # Bot Start Message
6
+ class BotEvent < Base
7
+ # TODO: Implement image attribute
8
+ attribute :image, :string
9
+ # TODO: Implement specify attribute
10
+ attribute :hp, :json
11
+ attribute :text, :json
12
+ attribute :button, :json
13
+
14
+ def type
15
+ :botEvent
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ module Message
5
+ # Bot Start Message
6
+ class BotStart < Base
7
+ # TODO: Implement image attribute
8
+ attribute :start_img, :string
9
+ attribute :init, :json
10
+
11
+ def type
12
+ :botStart
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ module Message
5
+ # Command Message
6
+ class Command < Base
7
+ class << self
8
+ def match?(message)
9
+ message['message']&.key?('bot_command')
10
+ end
11
+ end
12
+
13
+ attribute :event_id, :string
14
+ attribute :bot_command, :string
15
+
16
+ # Unsupport
17
+ def type
18
+ nil
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ module Message
5
+ # Image Message
6
+ class Image < Base
7
+ attribute :id, :string
8
+ attribute :ext, :string
9
+ attribute :width, :integer
10
+ attribute :height, :integer
11
+
12
+ def type
13
+ :img
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ module Message
5
+ # Sticker Message
6
+ class Sticker < Base
7
+ class << self
8
+ def match?(message)
9
+ message['message']&.key?('sticker_group')
10
+ end
11
+ end
12
+
13
+ attribute :sticker_group, :string
14
+ attribute :sticker_id, :string
15
+
16
+ def type
17
+ :sticker
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ module Message
5
+ # Text Message
6
+ class Text < Base
7
+ class << self
8
+ def match?(message)
9
+ message['message']&.key?('text')
10
+ end
11
+ end
12
+
13
+ attribute :text, :string
14
+
15
+ def type
16
+ :text
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+
5
+ module Hahamut
6
+ # Chat Message Sender
7
+ class Sender
8
+ ENDPOINT = 'https://us-central1-hahamut-8888.cloudfunctions.net/' \
9
+ 'messagePush?access_token=%<token>s'
10
+
11
+ def initialize(token)
12
+ @token = token
13
+ end
14
+
15
+ def uri
16
+ @uri ||= URI(format(ENDPOINT, token: @token))
17
+ end
18
+
19
+ def ssl?
20
+ uri.scheme == 'https'
21
+ end
22
+
23
+ def send(recipient, message)
24
+ http = Net::HTTP.new(uri.host, uri.port)
25
+ http.use_ssl = ssl?
26
+ http.request build_request(recipient, message)
27
+ end
28
+
29
+ private
30
+
31
+ def build_request(recipient, message)
32
+ @request = Net::HTTP::Post.new(uri)
33
+ @request['Content-Type'] = 'application/json'
34
+ @request.body = {
35
+ recipient: { id: recipient },
36
+ message: message
37
+ }.to_json
38
+ @request
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ # Signature Verifier
5
+ class Signature
6
+ def initialize(secret, body)
7
+ @secret = secret
8
+ @body = body
9
+ end
10
+
11
+ def match?(verify_signature)
12
+ signature == verify_signature
13
+ end
14
+
15
+ def signature
16
+ "sha1=#{OpenSSL::HMAC.hexdigest('SHA1', @secret, @body)}"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'Hahamut/support/attributes'
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ module Support
5
+ # Define attributes API
6
+ module Attributes
7
+ class << self
8
+ def included(other)
9
+ other.extend ClassMethods
10
+ end
11
+ end
12
+
13
+ # Define attribute class
14
+ module ClassMethods
15
+ def attribute(name, type)
16
+ name = name.to_sym
17
+ klass = Attribute.const_get(type.to_s.capitalize)
18
+
19
+ define_method name do
20
+ @attributes[name]&.value
21
+ end
22
+
23
+ define_method "#{name}=" do |value|
24
+ @attributes ||= {}
25
+ @attributes[name] = klass.new(value)
26
+ end
27
+ end
28
+ end
29
+
30
+ attr_reader :attributes
31
+
32
+ def assign_attributes(attributes)
33
+ attributes.each do |attr, value|
34
+ send("#{attr}=", value)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Hahamut
6
+ # Image Uploader
7
+ class Uploader
8
+ ENDPOINT = 'https://us-central1-hahamut-8888.cloudfunctions.net/' \
9
+ 'imagePush?bot_id=%<botid>s' \
10
+ '&access_token=%<token>s'
11
+
12
+ def initialize(botid, token)
13
+ @botid = botid
14
+ @token = token
15
+ end
16
+
17
+ def upload(path)
18
+ http = Net::HTTP.new(uri.host, uri.port)
19
+ http.use_ssl = true
20
+ create_image http.request(build_request(path))
21
+ end
22
+
23
+ def uri
24
+ @uri ||= URI(format(ENDPOINT, botid: @botid, token: @token))
25
+ end
26
+
27
+ private
28
+
29
+ def build_request(path)
30
+ @request = Net::HTTP::Post.new(uri.request_uri)
31
+ @request['Content-Type'] =
32
+ "multipart/form-data; boundary=#{Rack::Multipart::MULTIPART_BOUNDARY}"
33
+ @request.body = read_file(path)
34
+ @request
35
+ end
36
+
37
+ def read_file(path)
38
+ Rack::Multipart::Generator.new(
39
+ filedata: Rack::Multipart::UploadedFile.new(
40
+ path,
41
+ 'application/octet-stream'
42
+ )
43
+ ).dump
44
+ end
45
+
46
+ def create_image(res)
47
+ return false unless res.is_a?(Net::HTTPSuccess)
48
+
49
+ Message::Image.new(JSON.parse(res.body))
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hahamut
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hahamut
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - 蒼時弦也
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler-audit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: overcommit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
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: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.73.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.73.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: This is the client for messaging app Hahamut build by gamer.com.tw
126
+ email:
127
+ - concat@frost.tw
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".overcommit.yml"
134
+ - ".rspec"
135
+ - ".travis.yml"
136
+ - CODE_OF_CONDUCT.md
137
+ - Gemfile
138
+ - Gemfile.lock
139
+ - README.md
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/setup
143
+ - hahamut.gemspec
144
+ - lib/hahamut.rb
145
+ - lib/hahamut/attribute.rb
146
+ - lib/hahamut/attribute/base.rb
147
+ - lib/hahamut/attribute/integer.rb
148
+ - lib/hahamut/attribute/json.rb
149
+ - lib/hahamut/attribute/string.rb
150
+ - lib/hahamut/attribute/timestamp.rb
151
+ - lib/hahamut/client.rb
152
+ - lib/hahamut/engine.rb
153
+ - lib/hahamut/event.rb
154
+ - lib/hahamut/handler.rb
155
+ - lib/hahamut/manager.rb
156
+ - lib/hahamut/message.rb
157
+ - lib/hahamut/message/base.rb
158
+ - lib/hahamut/message/bot_event.rb
159
+ - lib/hahamut/message/bot_start.rb
160
+ - lib/hahamut/message/command.rb
161
+ - lib/hahamut/message/image.rb
162
+ - lib/hahamut/message/sticker.rb
163
+ - lib/hahamut/message/text.rb
164
+ - lib/hahamut/sender.rb
165
+ - lib/hahamut/signature.rb
166
+ - lib/hahamut/support.rb
167
+ - lib/hahamut/support/attributes.rb
168
+ - lib/hahamut/uploader.rb
169
+ - lib/hahamut/version.rb
170
+ homepage: https://github.com/elct9620/hahamut
171
+ licenses: []
172
+ metadata: {}
173
+ post_install_message:
174
+ rdoc_options: []
175
+ require_paths:
176
+ - lib
177
+ required_ruby_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ required_rubygems_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ requirements: []
188
+ rubygems_version: 3.0.3
189
+ signing_key:
190
+ specification_version: 4
191
+ summary: The Hahamut client
192
+ test_files: []