Twilio Integration in Dot Net Core/Dot Net 6

Twilio is a cloud communications platform that allows developers to integrate various communication methods into their applications. It offers APIs for services such as:

  • SMS Messaging: Send and receive text messages globally.
  • Voice Calls: Make and receive phone calls using VoIP.
  • Video Calls: Enable video conferencing capabilities.
  • Email: Send and manage emails through its SendGrid service.
  • Chat: Create real-time chat applications.

Key Features

  1. APIs and SDKs: Twilio provides a range of APIs and software development kits (SDKs) that make it easy to integrate communication features into web or mobile applications.
  2. Scalability: Twilio is designed to scale with your application, accommodating both small projects and large enterprises.
  3. Global Reach: Twilio has a global infrastructure, allowing services to be deployed in multiple countries.
  4. Security: It offers tools for secure communications, including encryption and authentication features.
  5. Flexibility: Developers can customize their communication workflows and integrate with various platforms and technologies.

Use Cases

  • Customer Support: Businesses can set up automated messaging or voice response systems.
  • Marketing: Companies can send promotional messages via SMS or email.
  • Two-Factor Authentication: Secure applications with SMS or voice-based verification.


Install Dot Net Package :

Install-Package Twilio

Or

Add Package Refrence:

<ItemGroup>

<PackageReference Include="Twilio" Version="7.5.0" />

</ItemGroup>


GitHub SDK Link: https://github.com/twilio/twilio-csharp

.NET Core Command Line Tools:

dotnet add package Twilio

Code:

//Package Imports

using Twilio.Types;

using Twilio;

using Twilio.Rest.Api.V2010.Account;


//Declaring Const Variables

const string AUTH_TOKEN = "Your AUTH_TOKEN";

const string ACCOUNT_SID = "Your ACCOUNT_SID";


//Phone Numbers Used to Send and Receive Messages and Calls

//You can get the SENDER_PHONE_NUMBER from Twilio Dashboard

const string SENDER_PHONE_NUMBER= "Your SENDER_PHONE_NUMBER";

const string RECIEVER_PHONE_NUMBER = "Your RECIEVER_PHONE_NUMBER";


//Initializing TwilioClient

TwilioClient.Init(ACCOUNT_SID, AUTH_TOKEN);


//Call the RECIEVER_PHONE_NUMBER

var call = CallResource.Create(

new PhoneNumber(RECIEVER_PHONE_NUMBER),

from: new PhoneNumber(SENDER_PHONE_NUMBER),

url: new Uri("https://my.twiml.here")

);

Console.WriteLine(call.Sid);

//Sending Message To RECIEVER_PHONE_NUMBER

var message = MessageResource.Create(

new PhoneNumber(RECIEVER_PHONE_NUMBER),

from: new PhoneNumber(SENDER_PHONE_NUMBER),

body: $"Hello World! From Twilio {SENDER_PHONE_NUMBER} to {SENDER_PHONE_NUMBER}"

);

Console.WriteLine(message.Sid);


Explanation:

There are many merhods available to initialize the TwilioClient in dot net core but we are using the below method with UserName and Password

public static void Init(string username, string password)

{

SetUsername(username);

SetPassword(password);

}

ACCOUNT_SID will act as a UserName and

AUTH_TOKEN will act as a Password

TwilioClient.Init(ACCOUNT_SID, AUTH_TOKEN);


Exception:

When you are calling on Phone Number you will face the below exception

Twilio.Exceptions.ApiException: 'Account not authorized to RECIEVER_PHONE_NUMBER call . Perhaps you need to enable some international permissions: https://www.twilio.com/console/voice/calls/geo-permissions/low-risk'

then go to the above mentioned/given link of the Twilio Account Dshboard and enable the low-risk permission and then you are able to Call using the Twilio Calling Api



要查看或添加评论,请登录

Osama Nasir的更多文章

社区洞察

其他会员也浏览了