Build the future of communications.
Start building for free
  • By Sam Agnew
    Improving your pun game with Python, Rhymebrain and the Twilio API for WhatsApp Guitarmony

    Puns are awesome and one easy way to come up with puns is to combine two different words. This is called a portmanteau. Let’s build a WhatsApp bot that gives you portmanteaus for certain words that you message it, so you can come up with better puns on the fly!

    We’ll be using the Rhymebrain API for the portmanteaus and the Twilio API for WhatsApp to program our WhatsApp bot.

    Setting up

    To get started make sure you have:

    Like with Twilio Programmable SMS we’ll need a webhook that will respond to incoming HTTP requests with every message ...

    Read More
  • By Sam Agnew
    Sending text messages from your iOS app in Swift using MFMessageComposeViewController Screen Shot 2018-07-17 at 6.45.06 PM

    Do you ever want to send text messages in your iOS app from your user’s phone number? While you can’t do this directly without your user’s consent, you can display a pre-composed message for your user to send using MFMessageComposeViewController.

    If you want to programmatically send an SMS from a different phone number, you can do this with Swift using Twilio.

    That’s cool. Let’s send a message!

    If you already have an app and want to get straight to the point, here’s the code you need to display the message composition interface:

    let composeVC = MFMessageComposeViewController()
    composeVC.messageComposeDelegate = self
    
    // Configure the fields of the interface.
    composeVC.recipients = ["13142026521"]
    composeVC.body = "I love Swift!"
    
    // Present the view controller modally.
    if MFMessageComposeViewController.canSendText() {
        self.present(composeVC, animated: true, completion: nil)
    }
    

    This code will send a text message to a Twilio number that is set up to give ...

    Read More
  • By Sam Agnew
    Handling Incoming Phone Calls with Server Side Swift, Vapor and Twilio Screen Shot 2018-06-01 at 11.37.46 AM

    You’re building a Vapor app and want to be able to handle phone calls? Let’s walk through how to add Twilio Programmable Voice to the barebones Vapor “Hello World” app.

    Swift Package Manager

    For this project we’re going to use Swift Package Manager to set everything up and install dependencies. Make sure you have Swift 4.0 or greater installed before moving on. You can check your Swift version by running the following in your terminal:

    swift --version
    

    Vapor has a convenient command line tool which can be used to generate templates for projects. Installing this will also take care of other necessary dependencies.

    You can install it using Homebrew with the following command:

    brew tap vapor/homebrew-tap
    brew install vapor
    

    Now initiate a new Swift project with the following terminal command in the directory where you want your code to live:

    swift package init --type ...
    Read More
  • By Sam Agnew
    How to receive/download picture messages in Node.js with Twilio MMS Screen Shot 2018-05-30 at 2.57.40 PM

    Have you ever needed to programmatically download an image from an MMS message sent to your Twilio number? Well here’s all the code you need to do this using Node.js and Express:

    const fs = require('fs');
    const express = require('express');
    const bodyParser = require('body-parser');
    const request = require('request');
    const MessagingResponse = require('twilio').twiml.MessagingResponse;
    
    const app = express();
    
    app.use(bodyParser.urlencoded({ extended: false } ));
    
    app.post('/sms', (req, res) => {
      const twiml = new MessagingResponse();
    
      if(req.body.NumMedia !== '0') {
        const filename = `${req.body.MessageSid}.png`;
        const url = req.body.MediaUrl0;
    
        // Download the image.
        request(url).pipe(fs.createWriteStream(filename))
          .on('close', () => console.log('Image downloaded.'));
    
        twiml.message('Thanks for the image!');
      } else {
        twiml.message("Try sending a picture message.");
      }
    
      res.send(twiml.toString());
    });
    
    app.listen(3000, () => console.log('Example app listening on port 3000!'));
    

    To run the above code, you’ll need to have the Twilio Node ...

    Read More
  • By Sam Agnew
    Receiving and Responding to Text Messages with Server Side Swift, Vapor and Twilio Screen Shot 2018-05-29 at 7.05.00 PM

    You’re building a Vapor app and want to be able to respond to SMS messages? Let’s walk through how to add Twilio SMS to the barebones Vapor “Hello World” app.

    Swift Package Manager

    For this project we’re going to use Swift Package Manager to set everything up and install dependencies. Make sure you have Swift 4.0 or greater installed before moving on. You can check your Swift version by running the following in your terminal:

    swift --version
    

    Vapor has a convenient command line tool which can be used to generate templates for projects. Installing this will also take care of other necessary dependencies.

    You can install it using Homebrew with the following command:

    brew tap vapor/homebrew-tap
    brew install vapor
    

    Now initiate a new Swift project with the following terminal command in the directory where you want your code to live:

    swift package init --type ...
    Read More
  • By Sam Agnew
    How to receive a POST request with server side Swift using Vapor Screen Shot 2018-05-21 at 9.41.09 PM

    After displaying text on a web page, receiving a POST request is the “Hello, World” v2 of building a web app. Let’s walk through how to do that using the popular server side Swift web framework, Vapor.

    Swift Package Manager

    For this project we’re going to use Swift Package Manager to set everything up and install dependencies. Make sure you have Swift 4.0 or greater installed before moving on. You can check your Swift version by running the following in your terminal:

    swift --version
    

    Vapor has a convenient command line tool which can be used to generate templates for projects. We won’t be using that functionality in this post, as our needs are more simple, but installing this will also install other necessary dependencies.

    You can install it using Homebrew with the following command:

    brew tap vapor/homebrew-tap
    brew install vapor
    

    Now initiate a new ...

    Read More
  • By Sam Agnew
    How to receive and download picture messages in Python with Twilio MMS Screen Shot 2018-05-10 at 5.26.51 PM

    Have you ever needed to programmatically download an image from an MMS message sent to your Twilio number? Well here’s all the code you need to do this using Python and Flask:

    import requests
    from flask import Flask, request, redirect
    from twilio.twiml.messaging_response import MessagingResponse
    
    
    DOWNLOAD_DIRECTORY = '/Path/to/your/code/directory'
    app = Flask(__name__)
    
    
    @app.route("/sms", methods=['GET', 'POST'])
    def sms_reply():
        """Respond to incoming with a simple text message."""
    
        resp = MessagingResponse()
    
        if request.values['NumMedia'] != '0':
    
            # Use the message SID as a filename.
            filename = request.values['MessageSid']   '.png'
            with open('{}/{}'.format(DOWNLOAD_DIRECTORY, filename), 'wb') as f:
               image_url = request.values['MediaUrl0']
               f.write(requests.get(image_url).content)
    
            resp.message("Thanks for the image!")
        else:
            resp.message("Try sending a picture message.")
    
        return str(resp)
    
    if __name__ == "__main__":
        app.run(debug=True)
    

    To run the above code, you’ll need to have the Twilio Python helper library installed, as ...

    Read More
  • By Sam Agnew
    Playing Pokemon via SMS in Swift Using Vapor and Twilio Screen Shot 2018-04-30 at 6.03.39 PM

    Pokemon filled many childhoods with joy. For some of us as adults, server side Swift does the same. What better way to spend a fun afternoon than combining the two?

    Let’s walk through how to build your own version of Twitch Plays Pokemon powered by text messages using the Twilio API, Vapor and Lua scripting for the Visual Boy Advance emulator.

    Getting Equipped

    Before moving on you’ll need to do the following to set up your environment:

    Install wine to be able to run the emulator we need, as only the Windows version has Lua scripting functionality.

    If you’re on a Mac, you can install Wine using Homebrew with the following command:

    brew install wine
    

    You might have issues getting the emulator to run by default, so make sure you have a 32-bit prefix set by running the following commands in your terminal:

    rm -rf ~/.wine
    WINEARCH ...
    Read More
  • By Sam Agnew
    Making phone calls in Swift with Twilio Screen Shot 2018-03-29 at 5.10.30 PM

    Programmatically making phone calls is awesome, but doing so from an iOS app can be dangerous because it requires you to store your Twilio credentials in the app. With Swift’s ability to run on the server, you can avoid the risks of using Twilio client-side!

    Let’s learn how to make phone calls with Twilio from our server using Swift.

    If you just want to skip right to the point, here is all the code you need:

    import Foundation
    import Alamofire
    
    if let accountSID = ProcessInfo.processInfo.environment["TWILIO_ACCOUNT_SID"],
       let authToken = ProcessInfo.processInfo.environment["TWILIO_AUTH_TOKEN"] {
    
      let url = "https://api.twilio.com/2010-04-01/Accounts/\(accountSID)/Calls"
      let parameters = ["From": "YOUR_TWILIO_NUMBER", "To": "YOUR_PERSONAL_NUMBER", "Url": "YOUR_TWIML_URL"]
    
      Alamofire.request(url, method: .post, parameters: parameters)
        .authenticate(user: accountSID, password: authToken)
        .responseJSON { response in
          debugPrint(response)
      }
    
      RunLoop.main.run()
    }
    

    Read ahead to learn how to get this code working. You’ll need to create a Twilio ...

    Read More
  • By Sam Agnew
    Sending text messages in Swift with Twilio Screen Shot 2018-03-22 at 3.56.52 PM

    Programmatically sending text messages is awesome, but doing so from an iOS app can be dangerous because it requires you to store your Twilio credentials in the app. With Swift’s ability to run on the server, you can avoid the risks of using Twilio client-side!

    Let’s learn how to send an SMS with Twilio from our server using Swift.

    If you just want to skip right to the point, here is all the code you need:

    import Foundation
    import Alamofire
    
    if let accountSID = ProcessInfo.processInfo.environment["TWILIO_ACCOUNT_SID"], 
       let authToken = ProcessInfo.processInfo.environment["TWILIO_AUTH_TOKEN"] {
    
      let url = "https://api.twilio.com/2010-04-01/Accounts/\(accountSID)/Messages"
      let parameters = ["From": "YOUR_TWILIO_NUMBER", "To": "YOUR_PERSONAL_NUMBER", "Body": "Hello from Swift!"]
    
      Alamofire.request(url, method: .post, parameters: parameters)
        .authenticate(user: accountSID, password: authToken)
        .responseJSON { response in
          debugPrint(response)
      }
    
      RunLoop.main.run()
    }
    

    Read ahead to learn how to get this code working. Before moving on you’ll ...

    Read More
  • Newer
    Older
    Sign up and start building
    Not ready yet? Talk to an expert.