kanal-interfaces-pachka 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: 16ad5df226c63ee228dda72d8ad6d3258b586f6ff4542e33898f6284bc43a6b7
4
+ data.tar.gz: 9619d6834efc1213db764df6cc1cb4aad152992c1a3ae7836e1980f48b17b802
5
+ SHA512:
6
+ metadata.gz: 3deeee89b8f7382b7633c77c39508cb65c9458d002fdd6e146f57c5d1ce1c968fd4b7284c094067c707950761f9dbfdf160bbd647fd48cf0d415a0db3978646d
7
+ data.tar.gz: f7d51ae3cedc28aba061a0fa6b3dde0fbbe596e15d13a6d1fd33928c38bc3e2d694c5a137a0273ffbae38ff46e7f9e835ce31c7b1391458222e9ba084df4eccf
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-04-14
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at idchlife@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in kanal-interfaces-pachka.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ group :development do
11
+ gem "solargraph"
12
+ gem "rubocop"
13
+ gem "yard"
14
+ gem "ruby-debug-ide"
15
+ end
16
+
17
+ group :test do
18
+ gem "rspec", "~> 3.0"
19
+ end
20
+
data/Gemfile.lock ADDED
@@ -0,0 +1,111 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ kanal-interfaces-pachka (0.1.0)
5
+ faraday (= 2.7.4)
6
+ faraday-multipart (= 1.0.4)
7
+ kanal (= 0.5.1)
8
+ mini_mime (= 1.1.2)
9
+ webrick (= 1.8.1)
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ ast (2.4.2)
15
+ backport (1.2.0)
16
+ benchmark (0.2.1)
17
+ diff-lcs (1.5.0)
18
+ e2mmap (0.1.0)
19
+ faraday (2.7.4)
20
+ faraday-net_http (>= 2.0, < 3.1)
21
+ ruby2_keywords (>= 0.0.4)
22
+ faraday-multipart (1.0.4)
23
+ multipart-post (~> 2)
24
+ faraday-net_http (3.0.2)
25
+ jaro_winkler (1.5.4)
26
+ json (2.6.3)
27
+ kanal (0.5.1)
28
+ kramdown (2.4.0)
29
+ rexml
30
+ kramdown-parser-gfm (1.1.0)
31
+ kramdown (~> 2.0)
32
+ mini_mime (1.1.2)
33
+ multipart-post (2.3.0)
34
+ nokogiri (1.14.3-x86_64-darwin)
35
+ racc (~> 1.4)
36
+ parallel (1.22.1)
37
+ parser (3.2.2.0)
38
+ ast (~> 2.4.1)
39
+ racc (1.6.2)
40
+ rainbow (3.1.1)
41
+ rake (13.0.6)
42
+ rbs (2.8.4)
43
+ regexp_parser (2.7.0)
44
+ reverse_markdown (2.1.1)
45
+ nokogiri
46
+ rexml (3.2.5)
47
+ rspec (3.12.0)
48
+ rspec-core (~> 3.12.0)
49
+ rspec-expectations (~> 3.12.0)
50
+ rspec-mocks (~> 3.12.0)
51
+ rspec-core (3.12.1)
52
+ rspec-support (~> 3.12.0)
53
+ rspec-expectations (3.12.2)
54
+ diff-lcs (>= 1.2.0, < 2.0)
55
+ rspec-support (~> 3.12.0)
56
+ rspec-mocks (3.12.5)
57
+ diff-lcs (>= 1.2.0, < 2.0)
58
+ rspec-support (~> 3.12.0)
59
+ rspec-support (3.12.0)
60
+ rubocop (1.50.1)
61
+ json (~> 2.3)
62
+ parallel (~> 1.10)
63
+ parser (>= 3.2.0.0)
64
+ rainbow (>= 2.2.2, < 4.0)
65
+ regexp_parser (>= 1.8, < 3.0)
66
+ rexml (>= 3.2.5, < 4.0)
67
+ rubocop-ast (>= 1.28.0, < 2.0)
68
+ ruby-progressbar (~> 1.7)
69
+ unicode-display_width (>= 2.4.0, < 3.0)
70
+ rubocop-ast (1.28.0)
71
+ parser (>= 3.2.1.0)
72
+ ruby-debug-ide (0.7.3)
73
+ rake (>= 0.8.1)
74
+ ruby-progressbar (1.13.0)
75
+ ruby2_keywords (0.0.5)
76
+ solargraph (0.49.0)
77
+ backport (~> 1.2)
78
+ benchmark
79
+ bundler (~> 2.0)
80
+ diff-lcs (~> 1.4)
81
+ e2mmap
82
+ jaro_winkler (~> 1.5)
83
+ kramdown (~> 2.3)
84
+ kramdown-parser-gfm (~> 1.1)
85
+ parser (~> 3.0)
86
+ rbs (~> 2.0)
87
+ reverse_markdown (~> 2.0)
88
+ rubocop (~> 1.38)
89
+ thor (~> 1.0)
90
+ tilt (~> 2.0)
91
+ yard (~> 0.9, >= 0.9.24)
92
+ thor (1.2.1)
93
+ tilt (2.1.0)
94
+ unicode-display_width (2.4.2)
95
+ webrick (1.8.1)
96
+ yard (0.9.34)
97
+
98
+ PLATFORMS
99
+ x86_64-darwin-22
100
+
101
+ DEPENDENCIES
102
+ kanal-interfaces-pachka!
103
+ rake (~> 13.0)
104
+ rspec (~> 3.0)
105
+ rubocop
106
+ ruby-debug-ide
107
+ solargraph
108
+ yard
109
+
110
+ BUNDLED WITH
111
+ 2.4.9
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 idchlife
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
+ # Kanal::Interfaces::Pachka
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ 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/kanal/interfaces/pachka`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ 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.
26
+
27
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/kanal-interfaces-pachka. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/kanal-interfaces-pachka/blob/main/CODE_OF_CONDUCT.md).
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the Kanal::Interfaces::Pachka project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/kanal-interfaces-pachka/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "logger"
4
+ require "kanal"
5
+ require "kanal/interfaces/pachka"
6
+
7
+ core = Kanal::Core::Core.new
8
+ logger = Logger.new $stdout
9
+ logger.level = Logger::WARN
10
+ core.add_logger logger
11
+
12
+ api_key = ENV.fetch("PACHKA_API_KEY", nil)
13
+
14
+ puts "Pachka api key: #{api_key}"
15
+
16
+ interface = Kanal::Interfaces::Pachka::PachkaInterface.new(core, api_key, api_debug_log: false)
17
+
18
+ interface.router.default_response do
19
+ pachka_text "Hey! I don't know how to answer to this yet... But I will, someday ;)"
20
+ end
21
+
22
+ interface.router.error_response do
23
+ pachka_text "Something went wrong. We're already fixing!"
24
+ end
25
+
26
+ interface.router.configure do
27
+ on :pachka, command: :ping do
28
+ respond do
29
+ pachka_text "pong!"
30
+ end
31
+ end
32
+
33
+ on :pachka, command: :meme do
34
+ respond do
35
+ pachka_text "Here is meme about: #{input.pachka_text}"
36
+ end
37
+ end
38
+
39
+ on :pachka, command: :sample_image do
40
+ respond do
41
+ pachka_file_path "./sample_image.png"
42
+ end
43
+ end
44
+
45
+ on :pachka, command: :sample_file do
46
+ respond do
47
+ pachka_text "Text provided with file"
48
+ pachka_file_path "./sample_file.zip"
49
+ end
50
+ end
51
+ end
52
+
53
+ puts "Starting interface!"
54
+ interface.start
Binary file
Binary file
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/kanal/interfaces/pachka/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "kanal-interfaces-pachka"
7
+ spec.version = Kanal::Interfaces::Pachka::VERSION
8
+ spec.authors = ["idchlife"]
9
+ spec.email = ["idchlife@gmail.com"]
10
+
11
+ spec.summary = "Kanal Interface to work with Pachka messenger"
12
+ spec.description = "Use this interface with your Kanal to create bot for Pachka messenger"
13
+ spec.homepage = "https://github.com/idchlife/kanal-interfaces-pachka"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.7.6"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/idchlife/kanal-interfaces-pachka"
21
+ spec.metadata["changelog_uri"] = "https://github.com/idchlife/kanal-interfaces-pachka/blob/main/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ spec.add_dependency "faraday", "2.7.4"
36
+ spec.add_dependency "faraday-multipart", "1.0.4"
37
+ spec.add_dependency "kanal", "0.5.1"
38
+ spec.add_dependency "mini_mime", "1.1.2"
39
+ spec.add_dependency "webrick", "1.8.1"
40
+
41
+ # For more information and examples about making a new gem, check out our
42
+ # guide at: https://bundler.io/guides/creating_gem.html
43
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "webrick"
4
+ require "json"
5
+
6
+ module Kanal
7
+ module Interfaces
8
+ module Pachka
9
+ module Helpers
10
+ #
11
+ # This class starts web server and accepts requests on api endpoint
12
+ #
13
+ class LocalServer
14
+ #
15
+ # @param host [String] host of web server (usually localhost)
16
+ # @param port [Integer] port of web server
17
+ #
18
+ def initialize(host, port)
19
+ @on_request_block = nil
20
+ @host = host
21
+ @port = port
22
+ end
23
+
24
+ #
25
+ # Provide block to this method which will be called with has parameters from json body
26
+ #
27
+ # @param &block [Proc] block which will be called with Hash parsed from json body as first argument
28
+ #
29
+ # @return [void] <description>
30
+ #
31
+ def on_request(&block)
32
+ @on_request_block = block
33
+ end
34
+
35
+ #
36
+ # Method to start web server and accept requests.
37
+ # Requests are accepted on @host:@port where @port is provided in constructor
38
+ # Endpoint: /api/bod
39
+ # Accepts only method POST and application/json Content-Type
40
+ # After request accepted it"s body is parsed and passed to @on_request_block
41
+ #
42
+ # @return [void] <description>
43
+ #
44
+ def start_accepting_requests
45
+ server = WEBrick::HTTPServer.new(Host: @host, Port: @port)
46
+
47
+ server.mount_proc "/api/bot" do |req, res|
48
+ if req.request_method == "POST" && req.content_type == "application/json"
49
+ begin
50
+ body = JSON.parse(req.body)
51
+
52
+ @on_request_block&.call(body)
53
+ rescue JSON::ParserError
54
+ res.status = 400
55
+ res.body = "Bad Request"
56
+ end
57
+ else
58
+ res.status = 405
59
+ res.body = "Method Not Allowed"
60
+ end
61
+
62
+ res["Content-Type"] = "text/plain"
63
+ res["Content-Length"] = res.body.length
64
+ end
65
+
66
+ trap("INT") { server.shutdown }
67
+
68
+ server.start
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "faraday"
5
+ require "faraday/multipart"
6
+ require "mini_mime"
7
+ require "kanal"
8
+
9
+ module Kanal
10
+ module Interfaces
11
+ module Pachka
12
+ module Helpers
13
+ #
14
+ # Class to work with Pachka public api
15
+ #
16
+ class PachkaApi
17
+ include Kanal::Logger
18
+
19
+ #
20
+ # @param access_token [String] access token for bot
21
+ # @param verbose [Boolean] pass true to enable STDOUT logging of requests and responses
22
+ #
23
+ def initialize(access_token, verbose: false)
24
+ @access_token = access_token
25
+
26
+ @conn = Faraday.new(
27
+ url: "https://api.pachca.com/api/shared/v1",
28
+ headers: { "Authorization" => "Bearer #{@access_token}" }
29
+ ) do |faraday|
30
+ if verbose
31
+ logger = ::Logger.new $stdout
32
+ logger.level = ::Logger::DEBUG
33
+ faraday.response :logger, logger, body: true, bodies: { request: true, response: true }
34
+ end
35
+
36
+ faraday.request :multipart
37
+ end
38
+ end
39
+
40
+ #
41
+ # Method sends text message via bot into the Pachka api
42
+ #
43
+ # @param entity_id [Integer] id of discussion obtained from input
44
+ # @param text [String] text to be sent
45
+ #
46
+ # @return [void] <description>
47
+ #
48
+ def send_text(entity_id, text)
49
+ @conn.post(
50
+ "messages",
51
+ {
52
+ message: {
53
+ entity_type: "discussion",
54
+ entity_id: entity_id,
55
+ content: text
56
+ }
57
+ }.to_json,
58
+ { "Content-Type" => "application/json" }
59
+ )
60
+ rescue Exception => e
61
+ logger.error "Cant send message to Pachka api! Error: #{e.full_message}"
62
+ end
63
+
64
+ #
65
+ # Method sends message with file
66
+ #
67
+ # @param entity_id [Integer] entity_id obtained via input.pachka_entity_id
68
+ # @param filepath [String] local filepath to file
69
+ # @param text [String] text of message
70
+ #
71
+ # @return [void] <description>
72
+ #
73
+ def send_file(entity_id, filepath, text)
74
+ text ||= " "
75
+
76
+ # Obtaining all the needed fields for uploading a file
77
+ res = @conn.post("uploads")
78
+
79
+ raise "Problem with requesting info about uploads! More in api logs." unless res.success?
80
+
81
+ first_response_body = JSON.parse(res.body)
82
+
83
+ direct_url = first_response_body["direct_url"]
84
+
85
+ file_key = first_response_body["key"]
86
+
87
+ filename = File.basename filepath
88
+
89
+ file_key = file_key.sub "${filename}", filename
90
+
91
+ mime_type = MiniMime.lookup_by_filename(filename).content_type
92
+
93
+ # filepath = File.absolute_path(filepath) if File.absolute_path(filepath) != filepath
94
+
95
+ file = Faraday::Multipart::FilePart.new filepath, mime_type
96
+
97
+ req_params = first_response_body
98
+
99
+ req_params.delete "direct_url"
100
+
101
+ req_params["key"] = file_key
102
+
103
+ payload = req_params
104
+ payload[:file] = file
105
+
106
+ res = @conn.post(direct_url, payload)
107
+
108
+ raise "Problem with uploading file to Pachka api! More in api logs." unless res.success?
109
+
110
+ res = @conn.post(
111
+ "messages",
112
+ {
113
+ message: {
114
+ entity_type: "discussion",
115
+ entity_id: entity_id,
116
+ content: text,
117
+ files: [
118
+ key: file_key,
119
+ name: filename,
120
+ file_type: mime_type.include?("image") ? "image" : "file",
121
+ size: File.size(filepath)
122
+ ]
123
+ }
124
+ }.to_json,
125
+ { "Content-Type" => "application/json" }
126
+ )
127
+
128
+ raise "Problem with sending message with file to Pachka api! More in api logs." unless res.success?
129
+ rescue Exception => e
130
+ logger.error "Error sending file to Pachka api! More info: #{e.full_message}"
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "json"
5
+ require "logger"
6
+ require "kanal/core/interfaces/interface"
7
+ require "kanal/plugins/batteries/batteries_plugin"
8
+ require_relative "./helpers/local_server"
9
+ require_relative "./helpers/pachka_api"
10
+ require_relative "./plugins/pachka_integration_plugin"
11
+
12
+ module Kanal
13
+ module Interfaces
14
+ module Pachka
15
+ #
16
+ # Interface for Pachka messenger, to work with bots provided in the integration section of Pachka
17
+ # messenger. It starts web server and accepts requests on api endpoint and sends requests to the
18
+ # Pachka api for bot to actually respond.
19
+ # Input parameters registered:
20
+ # input.pachka_command - command sent from users to bot. Full text of command passed, like "/hello"
21
+ #
22
+ # Output parameters registered:
23
+ # output.pachka_text - you can specify it in the respond block for bot to reply with text
24
+ # TODO: more parameters
25
+ #
26
+ class PachkaInterface < Kanal::Core::Interfaces::Interface
27
+ include Kanal::Logger
28
+ #
29
+ # Creates interface with core and optional parameters
30
+ # Be aware, it starts web server to accept Pachkas bot outgoing webhook requests
31
+ #
32
+ # @param core [Kanal::Core::Core] Kanal core
33
+ # @param api_token [String] access_token you obtained in Pachka integrations for bot
34
+ # @param host [String] host of web server accepting outgoing webhook requests from pachka bot
35
+ # @param port [Integer] port of web server accepting outgoing webhook requests from pachka bot
36
+ # @param local_server_debug_log [Boolean] pass true for local server to log requests to it to Kanal logger
37
+ # @param api_debug_log [Boolean] pass true to log pachka api requests to STDOUT
38
+ #
39
+ def initialize(core, access_token, host: "localhost", port: 8090, local_server_debug_log: false, api_debug_log: false)
40
+ super(core)
41
+
42
+ @port = port
43
+ @host = host
44
+
45
+ @local_server_debug_log = local_server_debug_log
46
+
47
+ @api = Kanal::Interfaces::Pachka::Helpers::PachkaApi.new access_token, verbose: api_debug_log
48
+
49
+ @access_token = access_token
50
+
51
+ core.register_plugin Kanal::Plugins::Batteries::BatteriesPlugin.new
52
+ core.register_plugin Kanal::Interfaces::Pachka::Plugins::PachkaIntegrationPlugin.new
53
+ end
54
+
55
+ def consume_output(output)
56
+ @api.send_text(output.pachka_entity_id, output.pachka_text) unless output.pachka_text.nil?
57
+
58
+ return if output.pachka_file_path.nil?
59
+
60
+ @api.send_file(output.pachka_entity_id, output.pachka_file_path, output.pachka_text)
61
+ end
62
+
63
+ def start
64
+ logger.info "Starting Pachka interface on http://#{@host}:#{@port}"
65
+
66
+ endpoint = Kanal::Interfaces::Pachka::Helpers::LocalServer.new(@host, @port)
67
+ endpoint.on_request do |body|
68
+ logger.debug "Local server received request with body: #{body}" if @local_server_debug_log
69
+
70
+ input = core.create_input
71
+ input.source = :pachka
72
+ input.pachka_entity_id = body["entity_id"]
73
+ input.pachka_query = body["content"]
74
+
75
+ consume_input input
76
+ end
77
+ endpoint.start_accepting_requests
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "kanal"
4
+ require "kanal/core/plugins/plugin"
5
+
6
+ module Kanal
7
+ module Interfaces
8
+ module Pachka
9
+ module Plugins
10
+ #
11
+ # Pachka integration plugin for parameters and conditions and hooks registration
12
+ #
13
+ class PachkaIntegrationPlugin < Kanal::Core::Plugins::Plugin
14
+ include Kanal::Logger
15
+
16
+ def name
17
+ :pachka_integration
18
+ end
19
+ #
20
+ # @param core [Kanal::Core::Core] <description>
21
+ #
22
+ # @return [void] <description>
23
+ #
24
+ def setup(core)
25
+ register_input_parameters core
26
+ register_output_parameters core
27
+ register_conditions core
28
+ register_hooks core
29
+ end
30
+
31
+ def register_input_parameters(core)
32
+ core.register_input_parameter :pachka_query, readonly: true
33
+ core.register_input_parameter :pachka_entity_id, readonly: true
34
+ core.register_input_parameter :pachka_command, readonly: true
35
+ core.register_input_parameter :pachka_text, readonly: true
36
+ end
37
+
38
+ def register_output_parameters(core)
39
+ core.register_output_parameter :pachka_entity_id
40
+ core.register_output_parameter :pachka_text
41
+ core.register_output_parameter :pachka_file_path
42
+ end
43
+
44
+ def register_hooks(core)
45
+ core.hooks.attach :input_before_router do |input|
46
+ input.pachka_command = input.pachka_query.split.first[1..] # "/hello" transforms to "hello"
47
+ input.pachka_text = input.pachka_query.split[1..].join " " # removes command and leaves just message
48
+ rescue e
49
+ logger.error "Error parsing input.pachka_query to populate input.pachka_command and input.pachka_text! Query: #{input.pachka_query} More info: #{e.full_message}"
50
+ input.pachka_command = "ERROR" if input.pachka_command.nil?
51
+ input.pachka_text = "ERROR" if input.pachka_text.nil?
52
+ end
53
+
54
+ core.hooks.attach :output_before_returned do |input, output|
55
+ output.pachka_entity_id = input.pachka_entity_id
56
+ end
57
+ end
58
+
59
+ def register_conditions(core)
60
+ core.add_condition_pack :pachka do
61
+ add_condition :query do
62
+ with_argument
63
+
64
+ met? do |input, _core, argument|
65
+ input.pachka_query == argument
66
+ end
67
+ end
68
+
69
+ add_condition :command do
70
+ with_argument
71
+
72
+ met? do |input, _core, argument|
73
+ input.pachka_command.to_sym == argument || input.pachka_command == argument
74
+ end
75
+ end
76
+
77
+ add_condition :text do
78
+ with_argument
79
+
80
+ met? do |input, _core, argument|
81
+ input.pachka_text == argument
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanal
4
+ module Interfaces
5
+ module Pachka
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "pachka/version"
4
+ require_relative "pachka/pachka_interface"
5
+
6
+ module Kanal
7
+ module Interfaces
8
+ module Pachka
9
+ class Error < StandardError; end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ module Kanal
2
+ module Interfaces
3
+ module Pachka
4
+ VERSION: String
5
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kanal-interfaces-pachka
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - idchlife
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.7.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday-multipart
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: kanal
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.5.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.5.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: mini_mime
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.1.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.1.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: webrick
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 1.8.1
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 1.8.1
83
+ description: Use this interface with your Kanal to create bot for Pachka messenger
84
+ email:
85
+ - idchlife@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".rspec"
91
+ - ".rubocop.yml"
92
+ - ".ruby-version"
93
+ - CHANGELOG.md
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - example/example.rb
101
+ - example/sample_file.zip
102
+ - example/sample_image.png
103
+ - kanal-interfaces-pachka.gemspec
104
+ - lib/kanal/interfaces/pachka.rb
105
+ - lib/kanal/interfaces/pachka/helpers/local_server.rb
106
+ - lib/kanal/interfaces/pachka/helpers/pachka_api.rb
107
+ - lib/kanal/interfaces/pachka/pachka_interface.rb
108
+ - lib/kanal/interfaces/pachka/plugins/pachka_integration_plugin.rb
109
+ - lib/kanal/interfaces/pachka/version.rb
110
+ - sig/kanal/interfaces/pachka.rbs
111
+ homepage: https://github.com/idchlife/kanal-interfaces-pachka
112
+ licenses:
113
+ - MIT
114
+ metadata:
115
+ allowed_push_host: https://rubygems.org
116
+ homepage_uri: https://github.com/idchlife/kanal-interfaces-pachka
117
+ source_code_uri: https://github.com/idchlife/kanal-interfaces-pachka
118
+ changelog_uri: https://github.com/idchlife/kanal-interfaces-pachka/blob/main/CHANGELOG.md
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: 2.7.6
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubygems_version: 3.1.6
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Kanal Interface to work with Pachka messenger
138
+ test_files: []