whatsapp_sdk 0.1.0 → 0.3.1
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 +4 -4
- data/.github/ISSUE_TEMPLATE/bug_report.md +21 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +14 -0
- data/.github/workflows/codeql-analysis.yml +72 -0
- data/.rubocop.yml +11 -1
- data/CHANGELOG.md +12 -0
- data/Gemfile +6 -2
- data/Gemfile.lock +22 -7
- data/README.md +68 -15
- data/example.rb +102 -47
- data/lib/version.rb +1 -1
- data/lib/whatsapp_sdk/api/client.rb +28 -8
- data/lib/whatsapp_sdk/api/medias.rb +101 -0
- data/lib/whatsapp_sdk/api/messages.rb +40 -16
- data/lib/whatsapp_sdk/api/phone_numbers.rb +8 -4
- data/lib/whatsapp_sdk/api/request.rb +10 -4
- data/lib/whatsapp_sdk/api/response.rb +6 -13
- data/lib/whatsapp_sdk/api/responses/error_response.rb +10 -19
- data/lib/whatsapp_sdk/api/responses/media_data_response.rb +29 -0
- data/lib/whatsapp_sdk/api/responses/message_error_response.rb +30 -0
- data/lib/whatsapp_sdk/api/responses/read_message_data_response.rb +2 -2
- data/lib/whatsapp_sdk/api/responses/success_response.rb +26 -0
- data/lib/whatsapp_sdk/configuration.rb +23 -0
- data/lib/whatsapp_sdk.rb +13 -27
- data/tmp/whatsapp.png +0 -0
- data/whatsapp_sdk.gemspec +13 -8
- metadata +51 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 309171f5666c8ab046ca3e386ea48486076ba6aa1412eb088b9a884e30d06bf3
|
4
|
+
data.tar.gz: 3bf929a941471762f0f35333b7179fc03c6c48c227a6fbd83246169f0739c505
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb2485c108c4c8ec5fd431bb906fd6f82046fb12344fa2fdda23eea9dc2eac97753f47cf600871d7c81a043d1c14fdcfa518d20e9c5fa3363cb4c88224c0f179
|
7
|
+
data.tar.gz: 157afd0a17d5e310c803c7d7e36b88fcb7c8ea4d33d0bff2800aec57459442f08a2bb820b137e0184f4d5edf87177d2f3643f445bc07e0b1a3bd8f0fee954999
|
@@ -0,0 +1,21 @@
|
|
1
|
+
---
|
2
|
+
name: Bug report
|
3
|
+
about: Create a report to help us improve
|
4
|
+
title: ''
|
5
|
+
labels: ''
|
6
|
+
assignees: ''
|
7
|
+
|
8
|
+
---
|
9
|
+
|
10
|
+
**Describe the bug**
|
11
|
+
A clear and concise description of what the bug is.
|
12
|
+
|
13
|
+
**To Reproduce**
|
14
|
+
Steps to reproduce the behavior:
|
15
|
+
1. Go to '...'
|
16
|
+
|
17
|
+
**Expected behavior**
|
18
|
+
A clear and concise description of what you expected to happen.
|
19
|
+
|
20
|
+
**Additional context**
|
21
|
+
Add any other context about the problem here.
|
@@ -0,0 +1,14 @@
|
|
1
|
+
---
|
2
|
+
name: Feature request
|
3
|
+
about: Suggest an idea for this project
|
4
|
+
title: ''
|
5
|
+
labels: ''
|
6
|
+
assignees: ''
|
7
|
+
|
8
|
+
---
|
9
|
+
|
10
|
+
**Is your feature request related to a problem? Please describe.**
|
11
|
+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
12
|
+
|
13
|
+
**Additional context**
|
14
|
+
Add any other context or screenshots about the feature request here.
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# For most projects, this workflow file will not need changing; you simply need
|
2
|
+
# to commit it to your repository.
|
3
|
+
#
|
4
|
+
# You may wish to alter this file to override the set of languages analyzed,
|
5
|
+
# or to provide custom queries or build logic.
|
6
|
+
#
|
7
|
+
# ******** NOTE ********
|
8
|
+
# We have attempted to detect the languages in your repository. Please check
|
9
|
+
# the `language` matrix defined below to confirm you have the correct set of
|
10
|
+
# supported CodeQL languages.
|
11
|
+
#
|
12
|
+
name: "CodeQL"
|
13
|
+
|
14
|
+
on:
|
15
|
+
push:
|
16
|
+
branches: [ "main" ]
|
17
|
+
pull_request:
|
18
|
+
# The branches below must be a subset of the branches above
|
19
|
+
branches: [ "main" ]
|
20
|
+
schedule:
|
21
|
+
- cron: '20 22 * * 6'
|
22
|
+
|
23
|
+
jobs:
|
24
|
+
analyze:
|
25
|
+
name: Analyze
|
26
|
+
runs-on: ubuntu-latest
|
27
|
+
permissions:
|
28
|
+
actions: read
|
29
|
+
contents: read
|
30
|
+
security-events: write
|
31
|
+
|
32
|
+
strategy:
|
33
|
+
fail-fast: false
|
34
|
+
matrix:
|
35
|
+
language: [ 'ruby' ]
|
36
|
+
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
|
37
|
+
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
|
38
|
+
|
39
|
+
steps:
|
40
|
+
- name: Checkout repository
|
41
|
+
uses: actions/checkout@v3
|
42
|
+
|
43
|
+
# Initializes the CodeQL tools for scanning.
|
44
|
+
- name: Initialize CodeQL
|
45
|
+
uses: github/codeql-action/init@v2
|
46
|
+
with:
|
47
|
+
languages: ${{ matrix.language }}
|
48
|
+
# If you wish to specify custom queries, you can do so here or in a config file.
|
49
|
+
# By default, queries listed here will override any specified in a config file.
|
50
|
+
# Prefix the list here with "+" to use these queries and those in the config file.
|
51
|
+
|
52
|
+
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
|
53
|
+
# queries: security-extended,security-and-quality
|
54
|
+
|
55
|
+
|
56
|
+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
57
|
+
# If this step fails, then you should remove it and run the build manually (see below)
|
58
|
+
- name: Autobuild
|
59
|
+
uses: github/codeql-action/autobuild@v2
|
60
|
+
|
61
|
+
# ℹ️ Command-line programs to run using the OS shell.
|
62
|
+
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
|
63
|
+
|
64
|
+
# If the Autobuild fails above, remove it and uncomment the following three lines.
|
65
|
+
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
|
66
|
+
|
67
|
+
# - run: |
|
68
|
+
# echo "Run, Build Application using script"
|
69
|
+
# ./location_of_script_within_repo/buildscript.sh
|
70
|
+
|
71
|
+
- name: Perform CodeQL Analysis
|
72
|
+
uses: github/codeql-action/analyze@v2
|
data/.rubocop.yml
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-minitest
|
3
|
+
- rubocop-performance
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
NewCops: enable
|
7
|
+
|
1
8
|
Style/StringLiterals:
|
2
9
|
Enabled: false
|
3
10
|
|
@@ -24,4 +31,7 @@ Metrics/AbcSize:
|
|
24
31
|
|
25
32
|
# Wait until https://github.com/rubocop/rubocop/issues/8761 is fixed
|
26
33
|
Gemspec/RequiredRubyVersion:
|
27
|
-
Enabled: false
|
34
|
+
Enabled: false
|
35
|
+
|
36
|
+
Minitest/MultipleAssertions:
|
37
|
+
Enabled: false
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,16 @@
|
|
1
1
|
# Unreleased
|
2
|
+
|
3
|
+
# v 0.3.1
|
4
|
+
- Update Meta API to v14
|
5
|
+
|
6
|
+
# v 0.3.0
|
7
|
+
- Allow Apps to have a singleton global authentication client.
|
8
|
+
|
9
|
+
# v 0.2.0
|
10
|
+
- Added Media API
|
11
|
+
- Added error and sucess responses
|
12
|
+
- Added faraday-multiplart as part of the library
|
13
|
+
|
2
14
|
# v 0.1.0
|
3
15
|
- Added Message Template API.
|
4
16
|
- Added Currency and Datetime resources.
|
data/Gemfile
CHANGED
@@ -4,18 +4,22 @@ source "https://rubygems.org"
|
|
4
4
|
|
5
5
|
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
6
6
|
|
7
|
-
# Specify your gem's dependencies in whatsapp_sdk.gemspec
|
8
7
|
gem("faraday")
|
8
|
+
gem("faraday-multipart")
|
9
9
|
gem("oj")
|
10
|
+
gem("rake", ">= 12.3.3")
|
11
|
+
gem("zeitwerk", ">= 2.6.0")
|
10
12
|
|
11
13
|
group(:test) do
|
12
14
|
gem('mocha')
|
15
|
+
gem('rubocop', require: false)
|
16
|
+
gem('rubocop-minitest', require: false)
|
17
|
+
gem('rubocop-performance', require: false)
|
13
18
|
end
|
14
19
|
|
15
20
|
group(:development) do
|
16
21
|
gem('pry')
|
17
22
|
gem('pry-nav')
|
18
|
-
gem('rubocop', require: false)
|
19
23
|
end
|
20
24
|
|
21
25
|
gemspec
|
data/Gemfile.lock
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
whatsapp_sdk (0.0
|
4
|
+
whatsapp_sdk (0.3.0)
|
5
5
|
faraday (~> 2.3.0)
|
6
|
+
faraday-multipart (~> 1.0.4)
|
6
7
|
oj (~> 3.13.13)
|
8
|
+
zeitwerk (~> 2.6.0)
|
7
9
|
|
8
10
|
GEM
|
9
11
|
remote: https://rubygems.org/
|
@@ -13,11 +15,14 @@ GEM
|
|
13
15
|
faraday (2.3.0)
|
14
16
|
faraday-net_http (~> 2.0)
|
15
17
|
ruby2_keywords (>= 0.0.4)
|
18
|
+
faraday-multipart (1.0.4)
|
19
|
+
multipart-post (~> 2)
|
16
20
|
faraday-net_http (2.0.3)
|
17
21
|
method_source (1.0.0)
|
18
|
-
minitest (5.
|
22
|
+
minitest (5.16.1)
|
19
23
|
mocha (1.14.0)
|
20
|
-
|
24
|
+
multipart-post (2.2.3)
|
25
|
+
oj (3.13.14)
|
21
26
|
parallel (1.22.1)
|
22
27
|
parser (3.1.2.0)
|
23
28
|
ast (~> 2.4.1)
|
@@ -27,10 +32,10 @@ GEM
|
|
27
32
|
pry-nav (1.0.0)
|
28
33
|
pry (>= 0.9.10, < 0.15)
|
29
34
|
rainbow (3.1.1)
|
30
|
-
rake (
|
35
|
+
rake (13.0.6)
|
31
36
|
regexp_parser (2.5.0)
|
32
37
|
rexml (3.2.5)
|
33
|
-
rubocop (1.30.
|
38
|
+
rubocop (1.30.1)
|
34
39
|
parallel (~> 1.10)
|
35
40
|
parser (>= 3.1.0.0)
|
36
41
|
rainbow (>= 2.2.2, < 4.0)
|
@@ -41,9 +46,15 @@ GEM
|
|
41
46
|
unicode-display_width (>= 1.4.0, < 3.0)
|
42
47
|
rubocop-ast (1.18.0)
|
43
48
|
parser (>= 3.1.1.0)
|
49
|
+
rubocop-minitest (0.20.1)
|
50
|
+
rubocop (>= 0.90, < 2.0)
|
51
|
+
rubocop-performance (1.14.2)
|
52
|
+
rubocop (>= 1.7.0, < 2.0)
|
53
|
+
rubocop-ast (>= 0.4.0)
|
44
54
|
ruby-progressbar (1.11.0)
|
45
55
|
ruby2_keywords (0.0.5)
|
46
|
-
unicode-display_width (2.
|
56
|
+
unicode-display_width (2.2.0)
|
57
|
+
zeitwerk (2.6.0)
|
47
58
|
|
48
59
|
PLATFORMS
|
49
60
|
x86_64-darwin-21
|
@@ -51,14 +62,18 @@ PLATFORMS
|
|
51
62
|
DEPENDENCIES
|
52
63
|
bundler (~> 2.3)
|
53
64
|
faraday
|
65
|
+
faraday-multipart
|
54
66
|
minitest (~> 5.0)
|
55
67
|
mocha
|
56
68
|
oj
|
57
69
|
pry
|
58
70
|
pry-nav
|
59
|
-
rake (
|
71
|
+
rake (>= 12.3.3)
|
60
72
|
rubocop
|
73
|
+
rubocop-minitest
|
74
|
+
rubocop-performance
|
61
75
|
whatsapp_sdk!
|
76
|
+
zeitwerk (>= 2.6.0)
|
62
77
|
|
63
78
|
BUNDLED WITH
|
64
79
|
2.3.9
|
data/README.md
CHANGED
@@ -1,7 +1,14 @@
|
|
1
1
|
# Ruby Whatsapp SDK
|
2
|
+
[](https://badge.fury.io/rb/whatsapp_sdk)
|
3
|
+
[](https://circleci.com/gh/ignacio-chiazzo/ruby_whatsapp_sdk)
|
4
|
+
<a href="https://codeclimate.com/github/ignacio-chiazzo/ruby_whatsapp_sdk/maintainability"><img src="https://api.codeclimate.com/v1/badges/169cce95450272e4ad7d/maintainability" /></a>
|
2
5
|
|
3
6
|
The SDK provides a set of operations and classes to use the Whatsapp API.
|
4
|
-
Send stickers, messages, audio, videos, locations or just ask for the phone numbers through this library in a few steps!
|
7
|
+
Send stickers, messages, audio, videos, and locations or just ask for the phone numbers through this library in a few steps!
|
8
|
+
|
9
|
+
|
10
|
+
https://user-images.githubusercontent.com/11672878/173238826-6fc0a6f8-d0ee-4eae-8947-7dfd3b8b3446.mov
|
11
|
+
|
5
12
|
|
6
13
|
## Installation
|
7
14
|
|
@@ -21,12 +28,22 @@ Or install it yourself as:
|
|
21
28
|
|
22
29
|
## Quick Start
|
23
30
|
|
24
|
-
There are
|
31
|
+
There are three primary resources, `Messages`, `Media` and `PhoneNumbers`. `Messages` allows users to send any kind of message (text, audio, location, video, image, etc.). `Media` allows users to manage media, and `Phone Numbers` enable clients to query the associated phone numbers.
|
32
|
+
|
33
|
+
To use `Messages`, `Media` or `PhoneNumbers`, you need to initialize the `Client` that contains auth information. There are two ways to do it
|
25
34
|
|
26
|
-
|
35
|
+
1) Using an initializer
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
WhatsappSdk.configure do |config|
|
39
|
+
config.access_token = ACCESS_TOKEN
|
40
|
+
end
|
41
|
+
```
|
42
|
+
OR 2) creating a `Client` instance and pass it to the `Messages`, `Medias` or `PhoneNumbers` instance like this:
|
27
43
|
|
28
44
|
```ruby
|
29
45
|
client = WhatsappSdk::Api::Client.new("<ACCESS TOKEN>") # replace this with a valid access token
|
46
|
+
messages_api = WhatsappSdk::Api::Messages.new(client)
|
30
47
|
```
|
31
48
|
|
32
49
|
Each API operation returns a `WhatsappSdk::Api::Response` that contains `data` and `error` and a couple of helpful functions such as `ok?` and `error?`. There are three types of response `WhatsappSdk::Api::MessageDataResponse`, `WhatsappSdk::Api::PhoneNumberDataResponse` and `WhatsappSdk::Api::PhoneNumbersDataResponse`. Each of them contains different attributes.
|
@@ -35,11 +52,13 @@ Each API operation returns a `WhatsappSdk::Api::Response` that contains `data` a
|
|
35
52
|
First, create the client and then create an instance `WhatsappSdk::Api::Messages` that requires a client as a param like this:
|
36
53
|
|
37
54
|
```ruby
|
38
|
-
|
39
|
-
|
40
|
-
|
55
|
+
messages_api = WhatsappSdk::Api::Messages.new
|
56
|
+
phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new
|
57
|
+
medias_api = WhatsappSdk::Api::Medias.new
|
41
58
|
```
|
42
59
|
|
60
|
+
Note: Remember to initialize the client first!
|
61
|
+
|
43
62
|
### Phone numbers API
|
44
63
|
Get the list of phone numbers registered
|
45
64
|
```ruby
|
@@ -51,6 +70,28 @@ Get the a phone number by id
|
|
51
70
|
phone_numbers_api.registered_numbers("123456") # accepts a phone_number_id
|
52
71
|
```
|
53
72
|
|
73
|
+
### Media API
|
74
|
+
|
75
|
+
Upload a media
|
76
|
+
```ruby
|
77
|
+
medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
|
78
|
+
```
|
79
|
+
|
80
|
+
Get a media
|
81
|
+
```ruby
|
82
|
+
media = medias_api.media(media_id: MEDIA_ID)
|
83
|
+
```
|
84
|
+
|
85
|
+
Download media
|
86
|
+
```ruby
|
87
|
+
medias_api.download(url: MEDIA_URL, file_path: 'tmp/downloaded_whatsapp.png')
|
88
|
+
```
|
89
|
+
|
90
|
+
Delete a media
|
91
|
+
```ruby
|
92
|
+
medias_api.delete(media_id: MEDIA_ID)
|
93
|
+
```
|
94
|
+
|
54
95
|
### Messages API
|
55
96
|
|
56
97
|
**Send a text message**
|
@@ -59,11 +100,19 @@ phone_numbers_api.registered_numbers("123456") # accepts a phone_number_id
|
|
59
100
|
messages_api.send_text(sender_id: 1234, recipient_number: "112345678", message: "hola")
|
60
101
|
```
|
61
102
|
|
103
|
+
**Read a message**
|
104
|
+
```ruby
|
105
|
+
messages_api.read_message(sender_id: 1234, message_id: "wamid.HBgLMTM0M12345678910=")
|
106
|
+
```
|
107
|
+
|
108
|
+
Note: To get the `message_id` you can set up [Webhooks](https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/components) that will listen and fire an event when a message is received.
|
109
|
+
|
110
|
+
|
62
111
|
**Send a location message**
|
63
112
|
|
64
113
|
```ruby
|
65
114
|
messages_api.send_location(
|
66
|
-
sender_id: 123123, recipient_number: "56789",
|
115
|
+
sender_id: 123123, recipient_number: "56789",
|
67
116
|
longitude: 45.4215, latitude: 75.6972, name: "nacho", address: "141 cooper street"
|
68
117
|
)
|
69
118
|
```
|
@@ -71,12 +120,12 @@ messages_api.send_location(
|
|
71
120
|
**Send an image message**
|
72
121
|
It could use a link or an image_id.
|
73
122
|
```ruby
|
74
|
-
# with a link
|
123
|
+
# with a link
|
75
124
|
messages_api.send_image(
|
76
125
|
sender_id: 123123, recipient_number: "56789", link: "image_link", caption: "Ignacio Chiazzo Profile"
|
77
126
|
)
|
78
127
|
|
79
|
-
# with an image id
|
128
|
+
# with an image id
|
80
129
|
messages_api.send_image(
|
81
130
|
sender_id: 123123, recipient_number: "56789", image_id: "1234", caption: "Ignacio Chiazzo Profile"
|
82
131
|
)
|
@@ -85,22 +134,22 @@ messages_api.send_image(
|
|
85
134
|
**Send an audio message**
|
86
135
|
It could use a link or an audio_id.
|
87
136
|
```ruby
|
88
|
-
# with a link
|
137
|
+
# with a link
|
89
138
|
messages_api.send_audio(sender_id: 123123, recipient_number: "56789", link: "audio_link")
|
90
139
|
|
91
|
-
# with an audio id
|
140
|
+
# with an audio id
|
92
141
|
messages_api.send_audio(sender_id: 123123, recipient_number: "56789", audio_id: "1234")
|
93
142
|
```
|
94
143
|
|
95
144
|
**Send a document message**
|
96
145
|
It could use a link or a document_id.
|
97
146
|
```ruby
|
98
|
-
# with a link
|
147
|
+
# with a link
|
99
148
|
messages_api.send_document(
|
100
149
|
sender_id: 123123, recipient_number: "56789", link: "document_link", caption: "Ignacio Chiazzo"
|
101
150
|
)
|
102
151
|
|
103
|
-
# with a document id
|
152
|
+
# with a document id
|
104
153
|
messages_api.send_document(
|
105
154
|
sender_id: 123123, recipient_number: "56789", document_id: "1234", caption: "Ignacio Chiazzo"
|
106
155
|
)
|
@@ -109,7 +158,7 @@ messages_api.send_document(
|
|
109
158
|
**Send a sticker message**
|
110
159
|
It could use a link or a sticker_id.
|
111
160
|
```ruby
|
112
|
-
# with a link
|
161
|
+
# with a link
|
113
162
|
messages_api.send_sticker(sender_id: 123123, recipient_number: "56789", link: "link")
|
114
163
|
|
115
164
|
# with a sticker_id
|
@@ -193,6 +242,10 @@ Visit [the example file](/example.rb) with examples to call the API in a single
|
|
193
242
|
- See the [official documentation](https://developers.facebook.com/docs/whatsapp/cloud-api) for the Whatsapp Cloud API.
|
194
243
|
- For pricing, refer to the [official documentation](https://developers.facebook.com/docs/whatsapp/pricing/). As of today, Whatsapp offers have 1000 conversations free per month.
|
195
244
|
|
245
|
+
## Troubleshooting
|
246
|
+
|
247
|
+
- If the API response is `success` but the message is not delivered, make sure the device you're sending the message to is using a supported Whatsapp version. [Check documentation](https://developers.facebook.com/docs/whatsapp/cloud-api/support/troubleshooting#message-not-delivered)
|
248
|
+
|
196
249
|
## Development
|
197
250
|
|
198
251
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
|
@@ -201,7 +254,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
201
254
|
|
202
255
|
## Contributing
|
203
256
|
|
204
|
-
Bug reports and pull requests are welcome on GitHub at [https://github.com/ignacio-chiazzo/
|
257
|
+
Bug reports and pull requests are welcome on GitHub at [https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk) 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.
|
205
258
|
|
206
259
|
## License
|
207
260
|
|
data/example.rb
CHANGED
@@ -11,7 +11,7 @@ gemfile(true) do
|
|
11
11
|
|
12
12
|
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
|
13
13
|
|
14
|
-
gem "whatsapp_sdk"
|
14
|
+
gem "whatsapp_sdk", path: "/Users/ignaciochiazzo/src/whatsapp_sdk"
|
15
15
|
gem "pry"
|
16
16
|
gem "pry-nav"
|
17
17
|
end
|
@@ -20,43 +20,93 @@ require 'whatsapp_sdk'
|
|
20
20
|
require "pry"
|
21
21
|
require "pry-nav"
|
22
22
|
|
23
|
-
|
24
|
-
SENDER_ID = 123
|
25
|
-
RECIPIENT_NUMBER = "456"
|
23
|
+
################# UPDATE CONSTANTS #################
|
26
24
|
|
27
|
-
|
28
|
-
|
29
|
-
|
25
|
+
ACCESS_TOKEN = "EAAHwuHDNkqoBAOtjFZAw1MUOXsFOv4EMP8Hi2mtzvcHSPbYWD4HLLLRapjPIDQWagke3cF2IIZCbtlhMZCluO1sH7C594q0q9UqZAXwufTqBtebRlssZCLwKeC69l6Emdmt2gRYGDnhTJH0mrM7L5ivZAWezhjOvtzdnKJ7xBZALiQRKB6JrEAgAZBcnBMfBZC75xR0ZACXQUPoplhjN1a5FTA" # replace this with a valid access_token # TODO replace
|
26
|
+
SENDER_ID = 107_878_721_936_019
|
27
|
+
RECIPIENT_NUMBER = 13_437_772_910
|
28
|
+
BUSINESS_ID = 114_503_234_599_312
|
29
|
+
IMAGE_LINK = "https://ignaciochiazzo.com/static/4c403819b9750c8ad8b20a75308f2a8a/876d5/profile-pic.avif"
|
30
|
+
binding.pry
|
31
|
+
# message_sent = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "holis")
|
30
32
|
|
31
|
-
|
32
|
-
|
33
|
+
################# Initialize Client #################
|
34
|
+
WhatsappSdk.configure do |config|
|
35
|
+
config.access_token = ACCESS_TOKEN
|
36
|
+
end
|
37
|
+
|
38
|
+
################# HELPERS ########################
|
39
|
+
def print_message_sent(message_response)
|
40
|
+
if message_response.ok?
|
41
|
+
puts "Message sent to: #{message_response.data.contacts.first.input}"
|
42
|
+
else
|
43
|
+
puts "Error: #{message_response.error&.to_s}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
##################################################
|
47
|
+
|
48
|
+
medias_api = WhatsappSdk::Api::Medias.new
|
49
|
+
messages_api = WhatsappSdk::Api::Messages.new
|
50
|
+
phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new
|
51
|
+
|
52
|
+
############################## Phone Numbers API ##############################
|
53
|
+
phone_numbers_api.registered_number(SENDER_ID)
|
54
|
+
phone_numbers_api.registered_numbers(BUSINESS_ID)
|
55
|
+
############################## Media API ##############################
|
56
|
+
|
57
|
+
# upload a media
|
58
|
+
uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
|
59
|
+
puts "Uploaded media id: #{uploaded_media.data&.id}"
|
33
60
|
|
34
|
-
|
35
|
-
|
61
|
+
# get a media
|
62
|
+
uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
|
63
|
+
media = medias_api.media(media_id: uploaded_media.data&.id).data
|
64
|
+
puts "Media info: #{media.raw_data_response}"
|
65
|
+
|
66
|
+
# download media
|
67
|
+
download_image = medias_api.download(url: media&.url, file_path: 'tmp/downloaded_whatsapp.png')
|
68
|
+
puts "Downloaded: #{download_image.data.success?}"
|
69
|
+
|
70
|
+
# # delete a media
|
71
|
+
# deleted_media = medias_api.delete(media_id: media&.id)
|
72
|
+
# puts "Deleted: #{deleted_media.data.success?}"
|
73
|
+
|
74
|
+
############################## Messages API ##############################
|
75
|
+
|
76
|
+
######### SEND A TEXT MESSAGE
|
77
|
+
message_sent = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
|
78
|
+
message: "Hey there! it's Whatsapp Ruby SDK")
|
79
|
+
print_message_sent(message_sent)
|
80
|
+
|
81
|
+
location_sent = messages_api.send_location(
|
36
82
|
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
|
37
|
-
longitude:
|
83
|
+
longitude: -75.6898604, latitude: 45.4192206, name: "Ignacio", address: "My house"
|
38
84
|
)
|
85
|
+
print_message_sent(location_sent)
|
39
86
|
|
40
|
-
|
87
|
+
######### READ A MESSAGE
|
88
|
+
# messages_api.read_message(sender_id: SENDER_ID, message_id: msg_id)
|
41
89
|
|
42
|
-
|
43
|
-
|
44
|
-
|
90
|
+
######### SEND AN IMAGE
|
91
|
+
# Send an image with a link
|
92
|
+
image_sent = messages_api.send_image(
|
93
|
+
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: media.url, caption: "Ignacio Chiazzo Profile"
|
45
94
|
)
|
95
|
+
print_message_sent(image_sent)
|
46
96
|
|
47
|
-
|
97
|
+
# Send an image with an id
|
48
98
|
messages_api.send_image(
|
49
|
-
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id:
|
99
|
+
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id: media.id, caption: "Ignacio Chiazzo Profile"
|
50
100
|
)
|
51
101
|
|
52
|
-
|
102
|
+
######### SEND AUDIOS
|
53
103
|
## with a link
|
54
104
|
messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "audio_link")
|
55
105
|
|
56
106
|
## with an audio id
|
57
107
|
messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, audio_id: "1234")
|
58
108
|
|
59
|
-
|
109
|
+
######### SEND DOCUMENTS
|
60
110
|
## with a link
|
61
111
|
messages_api.send_document(
|
62
112
|
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "document_link", caption: "Ignacio Chiazzo"
|
@@ -67,16 +117,22 @@ messages_api.send_document(
|
|
67
117
|
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, document_id: "1234", caption: "Ignacio Chiazzo"
|
68
118
|
)
|
69
119
|
|
70
|
-
|
120
|
+
######### SEND STICKERS
|
71
121
|
## with a link
|
72
122
|
messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "link")
|
73
123
|
|
74
124
|
## with a sticker_id
|
75
125
|
messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, sticker_id: "1234")
|
76
126
|
|
77
|
-
|
127
|
+
######### SEND A TEMPLATE
|
78
128
|
# Note: The template must have been created previously.
|
79
129
|
|
130
|
+
# Send a template with no component
|
131
|
+
response_with_object = messages_api.send_template(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
|
132
|
+
name: "hello_world", language: "en_US", components: [])
|
133
|
+
puts response_with_object
|
134
|
+
|
135
|
+
# Send a template with components.Remember to create the template first.
|
80
136
|
header_component = WhatsappSdk::Resource::Component.new(
|
81
137
|
type: WhatsappSdk::Resource::Component::Type::HEADER
|
82
138
|
)
|
@@ -120,31 +176,31 @@ body_component.add_parameter(parameter_video)
|
|
120
176
|
body_component.add_parameter(parameter_document)
|
121
177
|
body_component.to_json
|
122
178
|
|
123
|
-
button_component_1 = WhatsappSdk::Resource::Component.new(
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
)
|
129
|
-
|
130
|
-
button_component_2 = WhatsappSdk::Resource::Component.new(
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
)
|
136
|
-
|
137
|
-
#
|
179
|
+
# button_component_1 = WhatsappSdk::Resource::Component.new(
|
180
|
+
# type: WhatsappSdk::Resource::Component::Type::BUTTON,
|
181
|
+
# index: 0,
|
182
|
+
# sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
|
183
|
+
# parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "payload")]
|
184
|
+
# )
|
185
|
+
|
186
|
+
# button_component_2 = WhatsappSdk::Resource::Component.new(
|
187
|
+
# type: WhatsappSdk::Resource::Component::Type::BUTTON,
|
188
|
+
# index: 1,
|
189
|
+
# sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
|
190
|
+
# parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "payload")]
|
191
|
+
# )
|
192
|
+
|
193
|
+
# Send a template with component_json
|
138
194
|
response_with_json = messages_api.send_template(
|
139
195
|
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, name: "hello_world", language: "en_US",
|
140
196
|
components_json: [
|
141
197
|
{
|
142
|
-
"type"
|
143
|
-
"parameters"
|
198
|
+
"type" => "header",
|
199
|
+
"parameters" => [
|
144
200
|
{
|
145
|
-
"type"
|
146
|
-
"image"
|
147
|
-
"link"
|
201
|
+
"type" => "image",
|
202
|
+
"image" => {
|
203
|
+
"link" => "https://www.google.com/imgres?imgurl=https%3A%2F%2Fqph.cf2.quoracdn.net%2Fmain-qimg-6d977408fdd90a09a1fee7ba9e2f777c-lq&imgrefurl=https%3A%2F%2Fwww.quora.com%2FHow-can-I-find-my-WhatsApp-ID&tbnid=lDAx1vzXwqCakM&vet=12ahUKEwjKupLviJX4AhVrrHIEHQpGD9MQMygAegUIARC9AQ..i&docid=s-DNQVCrZmhJYM&w=602&h=339&q=example%20whatsapp%20image%20id&ved=2ahUKEwjKupLviJX4AhVrrHIEHQpGD9MQMygAegUIARC9AQ"
|
148
204
|
}
|
149
205
|
}
|
150
206
|
]
|
@@ -153,8 +209,7 @@ response_with_json = messages_api.send_template(
|
|
153
209
|
)
|
154
210
|
puts response_with_json
|
155
211
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
puts response_with_object
|
212
|
+
|
213
|
+
# # delete a media
|
214
|
+
deleted_media = medias_api.delete(media_id: media&.id)
|
215
|
+
puts "Deleted: #{deleted_media.data.success?}"
|
data/lib/version.rb
CHANGED