You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The [VonageMessagesAPI](https://developer.vonage.com/messages/overview) allows you to send messages over a number of different channels, and various message types within each channel. See the VonageDeveloperDocumentationfor a [complete API reference](https://developer.vonage.com/en/api/messages) listing all the channel and message type combinations.
343
343
344
-
TheRubySDK allows you to construct message data for specific messaging channels. Other than SMS (which has only one type -- text), you need to pass the message `:type` as well as the `:message` itself as arguments to the appropriate messages method, along with any optional properties if needed.
344
+
### Sending a Message
345
+
346
+
TheRubySDK implements a `Messaging` object which can be accessed via a `messaging` method on the `Client` object. The`Messaging` object has a `send` method which lets you send any message type via any channel.
347
+
348
+
```ruby
349
+
response = client.messaging.send(
350
+
# message data
351
+
)
352
+
```
353
+
354
+
There are a number of ways in which you can pass the necessary message data to the method.
355
+
356
+
**UsingKeywordArguments**
357
+
358
+
You can pass the message properties and their values as keyword arguments to the method. Forexample:
359
+
360
+
```ruby
361
+
response = client.messaging.send(
362
+
to: '447700900000',
363
+
from: '447700900001',
364
+
channel: 'sms',
365
+
message_type: 'text',
366
+
text: 'Hello world!'
367
+
)
368
+
```
369
+
370
+
**Spread a Hash**
371
+
372
+
For more complex message structures, you can define the message as a Hash literal andthen spread that Hash as keyword arguments by passing it to the `send` method using the double-splat opertator (`**`). Forexample:
**Using a Combination of KeywordArgumentsandSpread**
390
+
391
+
You can use a combination of the above two approaches. This might be useful in situations where you want to iteratively send the same message to multiple recipients, forexample:
392
+
393
+
```ruby
394
+
message = {
395
+
from: '447700900000',
396
+
channel: 'sms',
397
+
message_type: 'text',
398
+
text: 'Hello world!'
399
+
}
400
+
401
+
['447700900001', '447700900002', '447700900003'].each do |to_number|
402
+
client.messaging.send(to: to_number, **message)
403
+
end
404
+
```
405
+
406
+
**UsingChannelConvenienceMethods**
407
+
408
+
TheRubySDK provides convenience methods for each channel which return a Hash object which you can then pass to the `send` method in the same way that you would with a Hash literal. As well as a simpler interface, the convenience methods also provide some basic validation.
409
+
410
+
Other than SMS (which has only one type --`text`), these methods require a `:type` argument, which defines the `message_type` of the message within that channel. They also require a `:message` argument, which defvines the message itself; this is a Stringin the case of `text` messages, and a Hash containing the appopriate properties for other message types (e.g. `image`). You can also optionally pass an `opts` arguments, the value of which should be a Hash which defines any other property that you want to includein the message.
You can choose to omit the `to`and/or`from` arguments from the convenience method calls and instead pass them in as keyword arguments during the `send` method invocation.
['447700900001', '447700900002', '447700900003'].each do |to_number|
465
+
client.messaging.send(to: to_number, **message)
466
+
end
358
467
```
359
468
360
-
Once the message data is created, you can then send the message.
469
+
### Sending a Message with Failover
470
+
471
+
TheMessagesAPI lets you define one or more failover messages which will be sent if the initial message is rejected. In the RubySDK, this feature is implemented by passing a `failover` keyword argument during the invocation of the `send` method. The value of this argument must be an Array containing one or more Hash objects representing the failover message(s). Forexample:
0 commit comments