MitakeSms

A Ruby client for the Mitake SMS API, providing a simple and efficient way to send SMS messages through the Mitake SMS service.
Features
- Send single SMS messages
- Send batch SMS messages with automatic handling of the 500 message API limit
- UTF-8 encoding support by default
- Configurable API settings
- Simple and intuitive API
- Comprehensive error handling
Installation
Add this line to your application's Gemfile:
gem 'mitake_sms', github: '7a6163/mitake_sms'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install mitake_sms
Usage
Configuration
Before using the gem, you need to configure it with your Mitake SMS API credentials:
require 'mitake_sms'
MitakeSms.configure do |config|
config.username = 'your_username'
config.password = 'your_password'
config.api_url = 'https://smsapi.mitake.com.tw/api/mtk/'
end
Sending a Single SMS
response = MitakeSms.send_sms(to: '0912345678', text: 'Hello, this is a test message!')
if response.success?
puts "Message sent successfully! Message ID: #{response.message_id}"
puts "Remaining points: #{response.account_point}"
else
puts "Failed to send message: #{response.error}"
end
response = MitakeSms.send_sms(
to: '0912345678',
text: 'Hello with options!',
destname: 'John Doe',
response_url: 'https://your-callback-url.com/delivery-reports',
client_id: 'your-client-reference-id',
charset: 'BIG5'
)
Sending Multiple SMS in Batch
messages = [
{ to: '0912345678', text: 'First message' },
{ to: '0922333444', text: 'Second message', from: 'YourBrand' },
{ to: '0933555777', text: 'Third message', response_url: 'https://your-callback-url.com/reports' }
]
response = MitakeSms.batch_send(messages)
response = MitakeSms.batch_send(messages, charset: 'BIG5')
if response.is_a?(MitakeSms::Response) && response.success?
puts "Batch sent successfully!"
puts "Message ID: #{response.message_id}"
puts "Remaining points: #{response.account_point}"
elsif response.is_a?(Array)
response.each_with_index do |batch_response, index|
if batch_response.success?
puts "Batch #{index + 1} sent successfully!"
puts "Message ID: #{batch_response.message_id}"
puts "Remaining points: #{batch_response.account_point}"
else
puts "Batch #{index + 1} failed: #{batch_response.error}"
end
end
else
puts "Failed to send batch: #{response.error}"
end
Sending Large Batches with Custom Limit
messages = (1..1000).map do |i|
{ to: '0912345678', text: "Message #{i}" }
end
responses = MitakeSms.batch_send_with_limit(messages, 300)
responses = MitakeSms.batch_send_with_limit(messages, 300, charset: 'BIG5')
responses.each_with_index do |batch_response, index|
if batch_response.success?
puts "Batch #{index + 1} sent successfully!"
else
puts "Batch #{index + 1} failed: #{batch_response.error}"
end
end
Sending Batch SMS with Advanced Format
The batch_send method now uses the advanced format by default, which provides more control over each message in the batch, including scheduled delivery, validity period, recipient name, and more:
messages = [
{
client_id: 'unique-id-20250525-001',
to: '0912345678',
dlvtime: '20250526120000',
vldtime: '20250527120000',
destname: '大寶',
response_url: 'https://callback.url',
text: '這是一則測試簡訊'
},
{
to: '0922333444',
text: '這是另一則測試簡訊'
}
]
response = MitakeSms.batch_send(messages)
if response.is_a?(MitakeSms::Response) && response.success?
puts "Advanced batch sent successfully!"
puts "Message ID: #{response.message_id}"
puts "Remaining points: #{response.account_point}"
elsif response.is_a?(Array)
response.each_with_index do |batch_response, index|
if batch_response.success?
puts "Batch #{index + 1} sent successfully!"
else
puts "Batch #{index + 1} failed: #{batch_response.error}"
end
end
end
Error Handling
The gem provides specific error classes for different types of errors:
begin
response = MitakeSms.send_sms('invalid', 'test')
rescue MitakeSms::Client::AuthenticationError => e
puts "Authentication failed: #{e.message}"
rescue MitakeSms::Client::InvalidRequestError => e
puts "Invalid request: #{e.message}"
rescue MitakeSms::Client::ServerError => e
puts "Server error: #{e.message}"
rescue MitakeSms::Client::Error => e
puts "An error occurred: #{e.message}"
end
Development
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.
To install this gem onto your local machine, run bundle exec rake install.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/7a6163/mitake_sms.
License
The gem is available as open source under the terms of the MIT License.