how to send SMS with the sparrow SMS from aspnet core?
Sparrow SMS is a Bulk SMS and SMS Marketing service to help businesses acquire and engage customers. A leading VAS provider in Nepal, Sparrow SMS was Initiated by Janaki Technology Pvt.
Let’s start
- First, you need to create an account
- They send the information about login details
- Login with your account. They provide information your login details. Just like below.
- We need to talk with them to verify our account. They provide us demo with 10 message. They will provide information on your register email.
- Generate the token
After generating the token. So let dive into the code. Create a project on Aspnet Core from visual studio or vs code.
- Create from
@model SendInfo
<div>
<form action="/home/send" method="post">
<div class="form-group">
<label asp-for="number"></label>
<input asp-for="number" class="form-control" />
</div>
<div class="form-group">
<label asp-for="message"></label>
<input asp-for="message" class="form-control" />
</div>
<button type="submit" class="btn btn-primary" >Send</button>
</form>
</div>
In Home controller create Method or function
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ETransportation.Models;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using System.Text;
namespace ETransportation.Controllers
{
public class HomeController : Controller
{
string api = "http://api.sparrowsms.com/";
string token = "Random string";
string from = "Demo";
[HttpGet]
public IActionResult Send()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Send(SendInfo sendInfo)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response;
var parameters = $"token={token}&from={from}" +
$"&to={sendInfo.number}&text={sendInfo.message}";
response = await client.GetAsync(api+ "v2/sms/?"+ parameters);
response.Content.ReadAsStringAsync().Result;
if (response.IsSuccessStatusCode)
{
sendInfo.message = “send succefully”;
return View(sendInfo);
}
else
{
sendInfo.message = “send succefully”;
return View();
}
}
}
In Success Response you will get this result.
"credit_consumedstring":1,
"response_codestring":200,
"countstring":1,
"credit_availablestring":7.0,
"responsestring":"1 mesages has been queued for deliverystring",
"message_idstring":194298012
For more details go here.