IntelliTect / text-trolley-hackathon2022

MIT License
1 stars 0 forks source link

Receive message from Communication Services #29

Closed twofingerrightclick closed 1 year ago

twofingerrightclick commented 1 year ago

something like this

Note that you will need to configure your Azure Communication Services resource to forward incoming SMS messages to the endpoint of your ASP.NET application. You can do this by creating an SMS-enabled phone number and configuring it to forward incoming messages to your ASP.NET endpoint. You can also configure your Azure Communication Services resource to forward incoming SMS messages to a webhook endpoint that your ASP.NET application can consume.

using Azure;
using Azure.Communication.Sms;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace MyApplication.Controllers
{
    public class SmsController : ControllerBase
    {
        private readonly SmsClient _smsClient;

        public SmsController(string connectionString)
        {
            var smsClientOptions = new SmsClientOptions();
            _smsClient = new SmsClient(connectionString, smsClientOptions);
        }

        [HttpPost]
        public async Task<IActionResult> ReceiveSmsAsync()
        {
            // Retrieve the request body
            string requestBody = await new StreamReader(Request.Body).ReadToEndAsync();

            // Parse the SMS message
            var smsMessage = SmsMessage.FromJson(requestBody);

            // Process the SMS message
            // ...

            return Ok();
        }
    }
}

and here is the structure:

{
  "from": "+1234567890",
  "to": "+0987654321",
  "messageId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "messageType": "text",
  "text": "Hello, World!",
  "timestamp": "2022-03-03T00:00:00Z"
}
twofingerrightclick commented 1 year ago

Using twilio