gupshup_whatsapp 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.DS_Store +0 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CHANGELOG +0 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/gupshup_ruby.gemspec +26 -0
- data/lib/gupshup_whatsapp.rb +40 -0
- data/lib/gupshup_whatsapp/framework/error.rb +52 -0
- data/lib/gupshup_whatsapp/framework/request.rb +40 -0
- data/lib/gupshup_whatsapp/framework/response.rb +17 -0
- data/lib/gupshup_whatsapp/http/http.rb +54 -0
- data/lib/gupshup_whatsapp/rest/whatsapp/message/incoming_message.rb +16 -0
- data/lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb +120 -0
- data/lib/gupshup_whatsapp/rest/whatsapp/whatsapp.rb +20 -0
- data/lib/gupshup_whatsapp/version.rb +4 -0
- metadata +68 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 79ae49e29751763184e3d263e89473c527a714e4423078e4c541fa8fc7599c98
|
4
|
+
data.tar.gz: a4755fb47759f6841d6ef00e98e9ffcf1a0f5f5e8b1ba8082620900691ef0c71
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2505087bbacde3e5f058666680d0fc4269923a7d92d88ee252015307a129a42671b783c92ef2b44ff6c87501707abe955ae29ef95aafcd750c6f9e4962bd9ea2
|
7
|
+
data.tar.gz: a65d6488ba5d91541e697c0a764e99852da50d052e8dc0bdca7b73181a83fe61b45c2b0ade45f1cba9a2a902f25dbb3fbeb16b4d7fe36ffe1af4fa5c7c52d761
|
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG
ADDED
File without changes
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 devagastya0@gmail.com. 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 [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 thundersparkf
|
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,72 @@
|
|
1
|
+
# GupshupRuby
|
2
|
+
|
3
|
+
Gupshup Whatsapp API as a Gem in Ruby for easier integration
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'gupshup_whatsapp'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install gupshup_ruby
|
20
|
+
|
21
|
+
To test, refer to [Testing](#testing)
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Use the Gupshup API [docs](https://www.gupshup.io/developer/docs/bot-platform/guide/whatsapp-api-documentation) to construct the payload for a message type.
|
25
|
+
|
26
|
+
For example:
|
27
|
+
#### Image payload:
|
28
|
+
```
|
29
|
+
payload = {
|
30
|
+
:type => "image",
|
31
|
+
:originalUrl => 'https://www.chatwoot.com/docs/img/logo.png',
|
32
|
+
:previewUrl => 'https://www.chatwoot.com/docs/img/logo.png'
|
33
|
+
}
|
34
|
+
```
|
35
|
+
#### Text Payload
|
36
|
+
|
37
|
+
```
|
38
|
+
payload = {
|
39
|
+
:isHSM => false,
|
40
|
+
:type => "text",
|
41
|
+
:text => 'Hi Hello'
|
42
|
+
}
|
43
|
+
```
|
44
|
+
|
45
|
+
After this, run the following commands in your code after replacing the credentials and phone number:
|
46
|
+
```
|
47
|
+
g = Gupshup::WhatsApp.new(app, apikey, phone_number, version='2')
|
48
|
+
g.send('916383467769', payload)
|
49
|
+
```
|
50
|
+
## <a name="testing"></a>Testing
|
51
|
+
To run tests, configure your gupshup credentials in the `spec/gupshup_ruby_spec.rb`, and run
|
52
|
+
```
|
53
|
+
rspec --format doc
|
54
|
+
```
|
55
|
+
## Development
|
56
|
+
|
57
|
+
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.
|
58
|
+
|
59
|
+
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).
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
Bug reports and pull requests are welcome on GitHub at https://gitlab.com/thundersp/gupshup_ruby. 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]/gupshup_ruby/blob/master/CODE_OF_CONDUCT.md).
|
64
|
+
|
65
|
+
|
66
|
+
## License
|
67
|
+
|
68
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
69
|
+
|
70
|
+
## Code of Conduct
|
71
|
+
|
72
|
+
Everyone interacting in the GupshupRuby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/gupshup_ruby/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "gupshup_whatsapp"
|
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,26 @@
|
|
1
|
+
require_relative 'lib/gupshup_whatsapp/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "gupshup_whatsapp"
|
5
|
+
spec.version = GupshupRuby::VERSION
|
6
|
+
spec.authors = ["thundersparkf"]
|
7
|
+
spec.email = ["devagastya0@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = "Ruby Wrapper for Gupshup Whatsapp API"
|
10
|
+
spec.homepage = "https://rubygems.org/"
|
11
|
+
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
spec.metadata["source_code_uri"] = "https://gitlab.com/thundersparkf/gupshup_whatsapp.git"
|
16
|
+
spec.metadata["changelog_uri"] = "https://gitlab.com/thundersparkf/gupshup_whatsapp/-/blob/21dc7da6a580d24af1280e7bfb8e22a7e644ddd7/CHANGELOG"
|
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 have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/https'
|
3
|
+
require 'cgi'
|
4
|
+
require 'openssl'
|
5
|
+
require 'base64'
|
6
|
+
require 'forwardable'
|
7
|
+
require 'time'
|
8
|
+
require 'json'
|
9
|
+
require 'logger'
|
10
|
+
Dir["#{File.dirname(__FILE__)}/gupshup_whatsapp/http/**/*.rb"].sort.each do |file|
|
11
|
+
require file
|
12
|
+
end
|
13
|
+
Dir["#{File.dirname(__FILE__)}/gupshup_whatsapp/framework/**/*.rb"].sort.each do |file|
|
14
|
+
require file
|
15
|
+
end
|
16
|
+
Dir["#{File.dirname(__FILE__)}/gupshup_whatsapp/rest/*.rb"].sort.each do |file|
|
17
|
+
require file
|
18
|
+
end
|
19
|
+
Dir["#{File.dirname(__FILE__)}/gupshup_whatsapp/rest/**/*.rb"].sort.each do |file|
|
20
|
+
require file
|
21
|
+
end
|
22
|
+
Dir["#{File.dirname(__FILE__)}/gupshup_whatsapp/rest/**/**/*.rb"].sort.each do |file|
|
23
|
+
require file
|
24
|
+
end
|
25
|
+
Dir["#{File.dirname(__FILE__)}/gupshup_whatsapp/compatibility/**/*.rb"].sort.each do |file|
|
26
|
+
require file
|
27
|
+
end
|
28
|
+
|
29
|
+
module Gupshup
|
30
|
+
class Error < StandardError; end
|
31
|
+
class WhatsApp
|
32
|
+
attr_accessor :app, :apikey
|
33
|
+
def initialize(app, apikey, version = '2', phone)
|
34
|
+
@app = app
|
35
|
+
@apikey = apikey
|
36
|
+
@version = version
|
37
|
+
@phone = phone
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module Gupshup
|
5
|
+
module REST
|
6
|
+
class GupshupError < StandardError
|
7
|
+
# @deprecated all errors that have a body are now 'Twilio::RestError's
|
8
|
+
def body
|
9
|
+
warn 'Gupshup error handling'
|
10
|
+
nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class RestError < GupshupError
|
15
|
+
attr_reader :message, :response, :code, :status_code, :details, :more_info, :error_message
|
16
|
+
def initialize(message)
|
17
|
+
super
|
18
|
+
@status_code = response.status_code
|
19
|
+
@code = response.body.fetch('code', @status_code)
|
20
|
+
@details = response.body.fetch('details', nil)
|
21
|
+
@error_message = response.body.fetch('message', nil)
|
22
|
+
@more_info = response.body.fetch('more_info', nil)
|
23
|
+
@response = response
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
# @deprecated use #response instead
|
28
|
+
def body
|
29
|
+
warn 'This error used to be a "Twilio::REST::TwilioError" but is now a "Twilio::REST::RestError". ' \
|
30
|
+
'Please use #response instead of #body.'
|
31
|
+
@response
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_s
|
35
|
+
message
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def format_message(initial_message)
|
41
|
+
message = "[HTTP #{status_code}] #{code} : #{initial_message}"
|
42
|
+
message += "\n#{error_message}" if error_message
|
43
|
+
message += "\n#{details}" if details
|
44
|
+
message += "\n#{more_info}" if more_info
|
45
|
+
message + "\n\n"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class ObsoleteError < StandardError
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Gupshup
|
4
|
+
class Request
|
5
|
+
attr_reader :host, :port, :method, :url, :params, :data, :headers, :auth, :timeout
|
6
|
+
|
7
|
+
def initialize(host, port, method, url, params = {}, data = {}, headers = {}, auth = nil, timeout = nil)
|
8
|
+
@host = host
|
9
|
+
@port = port
|
10
|
+
@url = url
|
11
|
+
@method = method
|
12
|
+
@params = params
|
13
|
+
@data = data
|
14
|
+
@headers = headers
|
15
|
+
@auth = auth
|
16
|
+
@timeout = timeout
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_s
|
20
|
+
auth = @auth.nil? ? '' : '(' + @auth.join(',') + ')'
|
21
|
+
|
22
|
+
params = ''
|
23
|
+
unless @params.nil? || @params.empty?
|
24
|
+
params = '?' + @params.each.map { |key, value| "#{CGI.escape(key)}=#{CGI.escape(value)}" }.join('&')
|
25
|
+
end
|
26
|
+
|
27
|
+
headers = ''
|
28
|
+
unless @headers.nil? || @headers.empty?
|
29
|
+
headers = "\n" + @headers.each.map { |key, value| "-H \"#{key}\": \"#{value}\"" }.join("\n")
|
30
|
+
end
|
31
|
+
|
32
|
+
data = ''
|
33
|
+
unless @data.nil? || @data.empty?
|
34
|
+
data = @method.equal?('GET') ? "\n -G" : "\n"
|
35
|
+
data += @data.each.map { |key, value| "-d \"#{key}\"=\"#{value}\"" }.join("\n")
|
36
|
+
end
|
37
|
+
"#{auth} #{@method} #{@url}#{params}#{data}#{headers}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
|
2
|
+
module Gupshup
|
3
|
+
class Response
|
4
|
+
attr_accessor :status_code, :body, :headers
|
5
|
+
|
6
|
+
def initialize(status_code, body, headers: nil)
|
7
|
+
@status_code = status_code
|
8
|
+
body = "{}" if !body || body.empty?
|
9
|
+
@body = JSON.parse(body.to_s)
|
10
|
+
@headers = !headers ? {} : headers.to_hash
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
"[#{status_code}] #{body}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
|
2
|
+
require 'faraday'
|
3
|
+
|
4
|
+
module Gupshup
|
5
|
+
module HTTP
|
6
|
+
class Client
|
7
|
+
attr_accessor :adapter
|
8
|
+
attr_reader :last_response, :last_request
|
9
|
+
def initialize
|
10
|
+
@adapter = Faraday.default_adapter
|
11
|
+
end
|
12
|
+
|
13
|
+
def _request(request)
|
14
|
+
@connection = Faraday.new(url: "#{request.host}:#{request.port.to_s}", ssl: { verify: true }) do |f|
|
15
|
+
f.request :url_encoded
|
16
|
+
f.adapter @adapter
|
17
|
+
f.headers = request.headers
|
18
|
+
end
|
19
|
+
@last_request = request
|
20
|
+
@last_response = nil
|
21
|
+
|
22
|
+
response = send(request)
|
23
|
+
if response.status == 504
|
24
|
+
object = { message: 'Request timeout', code: 504 }.to_json
|
25
|
+
elsif response.body && !response.body.empty?
|
26
|
+
object = response.body
|
27
|
+
elsif response.status == 400
|
28
|
+
object = { message: 'Bad request', code: 400 }.to_json
|
29
|
+
end
|
30
|
+
|
31
|
+
gupshup_response = Gupshup::Response.new(response.status, object, headers: response.headers)
|
32
|
+
@last_response = gupshup_response
|
33
|
+
|
34
|
+
gupshup_response
|
35
|
+
end
|
36
|
+
|
37
|
+
def send(request)
|
38
|
+
@connection.send(request.method.downcase.to_sym,
|
39
|
+
request.url,
|
40
|
+
request.method == 'GET' ? request.params : request.data)
|
41
|
+
rescue Faraday::Error => e
|
42
|
+
raise Gupshup::REST::GupshupError, e
|
43
|
+
end
|
44
|
+
|
45
|
+
def request(host, port, method, url, data = {}, headers = {}, params = {}, auth = nil, timeout = nil)
|
46
|
+
request = Gupshup::Request.new(host, port, method, url, params, data, headers)
|
47
|
+
_request(request)
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
|
2
|
+
require 'uuidtools'
|
3
|
+
require 'securerandom'
|
4
|
+
require_relative '../whatsapp'
|
5
|
+
module WhatsApp
|
6
|
+
class IncomingMessage < Gupshup::REST::WhatsApp
|
7
|
+
def initialize(app, apikey, version = '2')
|
8
|
+
super
|
9
|
+
@type = 'message'
|
10
|
+
end
|
11
|
+
|
12
|
+
def sendMessage(message, type)
|
13
|
+
payload = {'id': SecureRandom.hex, }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
|
2
|
+
require_relative '../whatsapp'
|
3
|
+
module Gupshup
|
4
|
+
module REST
|
5
|
+
class OutboundMessage < Gupshup::REST::WhatsApp
|
6
|
+
attr_accessor :phone, :app, :base_uri, :apikey, :version, :content_type, :path, :headers
|
7
|
+
|
8
|
+
def initialize(app, apikey, phone, version = '2')
|
9
|
+
super
|
10
|
+
@channel = 'whatsapp'
|
11
|
+
@source = phone
|
12
|
+
@headers = { 'Content-type' => @content_type, 'apikey' => @apikey }
|
13
|
+
end
|
14
|
+
|
15
|
+
def send(destination, message_params)
|
16
|
+
# Exception handling for parameters to be added
|
17
|
+
case message_params[:type]
|
18
|
+
when 'text'
|
19
|
+
if message_params[:isHSM]
|
20
|
+
send_text_message(destination, message_params[:text], isHSM = true)
|
21
|
+
else
|
22
|
+
send_text_message(destination, message_params[:text], isHSM = false)
|
23
|
+
end
|
24
|
+
when 'image'
|
25
|
+
send_image(destination, message_params[:originalUrl], message_params[:previewUrl], message_params[:caption])
|
26
|
+
when 'video'
|
27
|
+
send_video(destination, message_params[:url], message_params[:caption])
|
28
|
+
when 'audio'
|
29
|
+
send_audio(destination, message_params[:url])
|
30
|
+
when 'file'
|
31
|
+
send_file(destination, message_params[:url], message_params[:filename])
|
32
|
+
when 'location'
|
33
|
+
send_location(destination, message_params[:longitude], message_params[:latitude], message_params[:name], message_params[:address])
|
34
|
+
else
|
35
|
+
puts 'Done'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# @param [Object] destination
|
40
|
+
def send_text_message(destination, message, isHSM=false)
|
41
|
+
payload = {'isHSM' => isHSM,
|
42
|
+
'type' => 'text',
|
43
|
+
'text' => message }.to_json
|
44
|
+
|
45
|
+
data = { 'channel' => @channel,
|
46
|
+
'destination' => destination.to_s,
|
47
|
+
'source' => @source,
|
48
|
+
'src.name' => @app,
|
49
|
+
'message' => payload }
|
50
|
+
r = Gupshup::HTTP::Client.new
|
51
|
+
r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers)
|
52
|
+
end
|
53
|
+
|
54
|
+
def send_image(destination, imageUrl, previewUrl, caption)
|
55
|
+
payload = {'type' => 'image', 'originalUrl' => imageUrl,
|
56
|
+
'previewUrl' => previewUrl, 'caption' => caption }.to_json
|
57
|
+
data = { 'channel' => @channel, 'destination' => destination.to_s,
|
58
|
+
'source' => @source, 'src.name' => @app,
|
59
|
+
'message' => payload }
|
60
|
+
r = Gupshup::HTTP::Client.new
|
61
|
+
r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers)
|
62
|
+
end
|
63
|
+
|
64
|
+
def send_video(destination, url, caption)
|
65
|
+
payload = {'url' => url,
|
66
|
+
'type' => 'video',
|
67
|
+
'caption' => caption }.to_json
|
68
|
+
|
69
|
+
data = { 'channel' => @channel,
|
70
|
+
'destination' => destination.to_s,
|
71
|
+
'source' => @source,
|
72
|
+
'src.name' => @app,
|
73
|
+
'message' => payload }
|
74
|
+
r = Gupshup::HTTP::Client.new
|
75
|
+
r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers)
|
76
|
+
end
|
77
|
+
|
78
|
+
def send_file(destination, url, filename)
|
79
|
+
payload = {'url' => url,
|
80
|
+
'type' => 'file',
|
81
|
+
'filename' => filename }.to_json
|
82
|
+
|
83
|
+
data = { 'channel' => @channel,
|
84
|
+
'destination' => destination.to_s,
|
85
|
+
'source' => @source,
|
86
|
+
'src.name' => @app,
|
87
|
+
'message' => payload }
|
88
|
+
r = Gupshup::HTTP::Client.new
|
89
|
+
r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers)
|
90
|
+
end
|
91
|
+
|
92
|
+
def send_audio(destination, url)
|
93
|
+
payload = {'url' => url,
|
94
|
+
'type' => 'audio' }.to_json
|
95
|
+
|
96
|
+
data = { 'channel' => @channel,
|
97
|
+
'destination' => destination.to_s,
|
98
|
+
'source' => @source,
|
99
|
+
'src.name' => @app,
|
100
|
+
'message' => payload }
|
101
|
+
r = Gupshup::HTTP::Client.new
|
102
|
+
r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers)
|
103
|
+
end
|
104
|
+
|
105
|
+
def send_location(destination, longitude, latitude, name, address)
|
106
|
+
payload = { "type": 'location',
|
107
|
+
"longitude": longitude, "latitude": latitude,
|
108
|
+
"name": name, "address": address }.to_json
|
109
|
+
|
110
|
+
data = { 'channel' => @channel,
|
111
|
+
'destination' => destination.to_s,
|
112
|
+
'source' => @source,
|
113
|
+
'src.name' => @app,
|
114
|
+
'message' => payload }
|
115
|
+
r = Gupshup::HTTP::Client.new
|
116
|
+
r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
|
2
|
+
module Gupshup
|
3
|
+
module REST
|
4
|
+
class WhatsApp
|
5
|
+
attr_reader :content_type, :apikey, :base_uri, :app, :phone, :path
|
6
|
+
|
7
|
+
def initialize(app, apikey, version = '1', phone="917384811114")
|
8
|
+
@app = app
|
9
|
+
@apikey = apikey
|
10
|
+
@version = version
|
11
|
+
@content_type = 'application/x-www-form-urlencoded'
|
12
|
+
@base_uri = 'https://api.gupshup.io'
|
13
|
+
@path = '/sm/api/v1/msg'
|
14
|
+
@phone = phone
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gupshup_whatsapp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- thundersparkf
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-07-02 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- devagastya0@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".DS_Store"
|
21
|
+
- ".gitignore"
|
22
|
+
- ".rspec"
|
23
|
+
- ".travis.yml"
|
24
|
+
- CHANGELOG
|
25
|
+
- CODE_OF_CONDUCT.md
|
26
|
+
- Gemfile
|
27
|
+
- LICENSE.txt
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- bin/console
|
31
|
+
- bin/setup
|
32
|
+
- gupshup_ruby.gemspec
|
33
|
+
- lib/gupshup_whatsapp.rb
|
34
|
+
- lib/gupshup_whatsapp/framework/error.rb
|
35
|
+
- lib/gupshup_whatsapp/framework/request.rb
|
36
|
+
- lib/gupshup_whatsapp/framework/response.rb
|
37
|
+
- lib/gupshup_whatsapp/http/http.rb
|
38
|
+
- lib/gupshup_whatsapp/rest/whatsapp/message/incoming_message.rb
|
39
|
+
- lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb
|
40
|
+
- lib/gupshup_whatsapp/rest/whatsapp/whatsapp.rb
|
41
|
+
- lib/gupshup_whatsapp/version.rb
|
42
|
+
homepage: https://rubygems.org/
|
43
|
+
licenses:
|
44
|
+
- MIT
|
45
|
+
metadata:
|
46
|
+
homepage_uri: https://rubygems.org/
|
47
|
+
source_code_uri: https://gitlab.com/thundersparkf/gupshup_whatsapp.git
|
48
|
+
changelog_uri: https://gitlab.com/thundersparkf/gupshup_whatsapp/-/blob/21dc7da6a580d24af1280e7bfb8e22a7e644ddd7/CHANGELOG
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 2.3.0
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubygems_version: 3.1.4
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: Ruby Wrapper for Gupshup Whatsapp API
|
68
|
+
test_files: []
|