Hey there, fellow Ruby enthusiast! Ready to add some SMS magic to your project? Let's dive into integrating TextMagic's SMS API using their nifty textmagic-ruby package. It's easier than you might think, and I'll walk you through it step by step.
Before we jump in, make sure you've got:
First things first, let's get that gem installed:
gem install textmagic-ruby
Now, create a new Ruby file for your project. Let's call it sms_magic.rb
. Fancy, huh?
Alright, time to get our hands dirty. Open up sms_magic.rb
and let's start coding:
require 'textmagic-ruby' client = Textmagic::REST::Client.new 'your_username', 'your_api_key'
Replace 'your_username'
and 'your_api_key'
with your actual credentials. Don't worry, I won't peek!
Let's send your first SMS. It's as easy as pie:
message = client.messages.create( text: 'Hello from TextMagic!', phones: '+1234567890' ) puts "Message ID: #{message.id}"
Boom! You've just sent an SMS. How cool is that?
Got a whole bunch of people to text? No sweat:
message = client.messages.create( text: 'Bulk message from TextMagic!', phones: '+1234567890, +9876543210' )
Want to send a message in the future? We've got you covered:
message = client.messages.create( text: 'This is a scheduled message', phones: '+1234567890', sendingTime: 1733664000 # Unix timestamp )
Curious about your message? Let's check its status:
message = client.messages.get(message_id) puts "Message status: #{message.status}"
Need to see your messaging history? Easy peasy:
messages = client.messages.list(limit: 10) messages.each do |message| puts "ID: #{message.id}, Text: #{message.text}" end
To handle incoming messages, you'll need to set up a webhook. Once that's done, you can process incoming messages like this:
post '/sms_webhook' do sender = params['from'] text = params['text'] # Process the incoming message puts "Received message from #{sender}: #{text}" end
And there you have it! You're now a TextMagic Ruby wizard. 🧙♂️ Remember, this is just scratching the surface. The TextMagic API has tons more features to explore.
For more in-depth info, check out the TextMagic API documentation. Now go forth and send those SMSes like a boss!
Happy coding! 🚀