Set Up a .NET Dev Environment for Messaging

Using this guide, you can set up a development environment in five minutes to trigger API requests related to our Messaging API. It also shows how to send and receive messages using tunneling software to expose the local dev server to the public internet.

Install .NET Framework and the Plivo .NET SDK

To get started, install and set up .NET Framework 4.6 or higher and Plivo’s .NET SDK.

Install .NET Framework

You can check whether you already have .NET Framework installed under macOS or Linux and what version is installed by running the command dotnet --version in a terminal window. Under Windows there are several ways to check. If you don’t have it or need a more current version, download and install it.

Install the Plivo .NET SDK using Visual Studio

To install the Plivo .NET SDK:

  • Create a new project in Visual Studio.

Create New Project

  • Choose a template for the new project.

Choose Template

  • Install the Plivo NuGet package.

Manage NuGet packages Select Plivo NuGet package

Trigger an API request

Now you can create a file in the project directory and execute code to trigger any Plivo API. Here’s some example code that sends an SMS message. Open the Program.cs file in the CS project and paste into it this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using Plivo;

namespace testplivo
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            var api = new PlivoApi("<auth_id>","<auth_token>");
            var response = api.Message.Create(
                src: "<caller_id>",
                dst: new List<String> { "<destination_number" },
                text: "Hello, from .NET Framework!"
                );
            Console.WriteLine(response);
        }
    }
}
Note:
  • Replace the placeholders <auth_id> and <auth_token> with your authentication credentials, which you can find on the overview page of the Plivo console.
  • We recommend that you store your credentials in the auth_id and auth_token environment variables to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and it will automatically fetch the values from the environment variables.
  • You can use the Environment.SetEnvironmentVariable method to store environment variables, and fetch them using the Environment.GetEnvironmentVariable method while initializing the client.
  • Replace the placeholder <Caller_ID> with a phone number you’ve purchased, and <Destination_Number> with the phone number you’ll be calling. Both phone numbers should be in E.164 format.
</ul>

Before starting the application, edit Properties/launchSettings.json and set applicationUrl to

    "applicationUrl": "http://localhost:5000/"

Save the file and run it.

Send outbound SMS

You should see your basic server application in action on http://localhost:5000/receivesms/. You can follow the same approach to trigger other API requests. Refer to our detailed API reference to see all the API requests available on our Voice API platform.

Set up a .NET Framework application for incoming messages and callbacks

Now that we’ve sent a message, let’s set up a .Net Framework application to handle incoming messages.

Plivo supports receiving SMS text messages in several countries (see complete SMS API coverage). When someone sends a text message to a Plivo phone number, you can receive it on your server by setting a Message URL in your Plivo application. Plivo sends the message, along with other parameters, to your Message URL.

Set up a .NET Framework application

  • Create an MVC web application:

Create a MVC app

  • Configure the MVC application and provide a project name. We called ours “ReceiveSms”:

Configure the MVC app

  • Install the Plivo NuGet package:

Install Plivo Nuget Package

Create a controller to receive SMS

Navigate to the Controllers directory in the ReceiveSms application, create a Controller named ReceiveSmsController.cs, and paste into it this code.

Install Plivo Nuget Package

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using Microsoft.AspNetCore.Mvc;

namespace ReceiveSms.Controllers
{
    public class ReceiveSmsController : Controller
    {
        // GET: /<controller>/
        public String Index()
        {
            String from_number = Request.Form["From"];
            String to_number = Request.Form["To"];
            String text = Request.Form["Text"];
            Console.WriteLine("Message received - From: {0}, To: {1}, Text: {2}", from_number, to_number, text);

            return "Message received";
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using Microsoft.AspNetCore.Mvc;

namespace ReceiveSms.Controllers
{
    public class ReceiveSmsController : Controller
    {
        // GET: /<controller>/
        public String Index()
        {
            String from_number = Request.Form["From"];
            String to_number = Request.Form["To"];
            String text = Request.Form["Text"];
            String media_url = Request.Form["Media0"];
            Console.WriteLine("Message received - From: {0}, To: {1}, Text: {2}, Media: {3}", from_number, to_number, text, media_url);

            return "Message received";
        }
    }
}

Finally, before starting the application, edit Properties/launchSettings.json and set applicationUrl to

    "applicationUrl": "http://localhost:5000/"

Run the project, and you should see your basic server application in action on http://localhost:5000/receivesms/

Ngrok setup

To receive incoming messages, your local server must connect with Plivo API services. For that, we recommend using ngrok, which exposes local servers running behind NATs and firewalls to the public internet over secure tunnels. Using ngrok, you can set webhooks that can talk to the Plivo server.

ngrok block diagram

Install ngrok and run it on the command line, specifying the port that hosts the application on which you want to receive messages (5000 in this case):

./ngrok http 5000

This starts the ngrok server on your local server. Ngrok will display a forwarding link that you can use as a webhook to access your local server over the public network.

Sample ngrok CLI

Now people can send messages to your Plivo number.

You can follow the same approach to serve other XML documents to manage other call flows. Refer to our detailed XML reference to see all the XML elements available on our Messaging API platform.