C# Sample to connect to Event Grid to read incoming SMS Messages

John Pike 1 Reputation point
2023-01-20T02:44:06.94+00:00

I am able to send a SMS via c# program no problem. There are no good examples on how to connect to event grid to process the received SMS messages. I am interested in connecting to the event grid and processing messages directly. Can I do that?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,911 questions
Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,542 questions
Azure Event Grid
Azure Event Grid
An Azure event routing service designed for high availability, consistent performance, and dynamic scale.
454 questions
{count} votes

3 answers

Sort by: Most helpful
  1. AbdelRahman Elsabaa 5 Reputation points Microsoft Employee
    2023-01-20T21:04:03.5766667+00:00

    Hello John,
    As Deepthi mentioned, yes you can connect to EventGrid event and process.

    If you are looking into connecting it with an Azure Function start by this:

    1. Create Azure Function ready to receive EventGrid events - link
    2. From the Communication resource page, click on Events then "+ Event Subscription"
    3. You will need to fill in the required information as shared by Deepthi in the above comment, also add Azure Function as the endpoint type and choose the newly created trigger. Make sure to select the necessary Event Types that you want to get notified about.
    4. Now use your resource to send sms or chat message for example.

    5.Finally you should see events in your Azure Function Monitor [https://learn.microsoft.com/en-us/azure/event-grid/custom-event-to-function#verify-that-function-received-the-event

    Checkout here showing some code to process received events over webhook event handler from EventGrid docs.
    [https://learn.microsoft.com/en-us/azure/event-grid/receive-events
    Also link showing how to use webhook as an event handler to view events on EventGridViewer.
    [https://learn.microsoft.com/en-us/azure/communication-services/quickstarts/sms/handle-sms-events

    Hope this helps, please let us know if you have any other questions.

    Thanks,
    Abdel

    1 person found this answer helpful.
    0 comments No comments

  2. Santhi Swaroop Naik Bukke 595 Reputation points
    2023-01-20T21:12:08.9+00:00
    Here is an example of how you could use C# to connect to Event Grid and read incoming SMS messages:
    
    Copy code
    using System;
    using Microsoft.Azure.EventGrid;
    using Microsoft.Azure.EventGrid.Models;
    
    public class EventGridExample
    {
        private static readonly string eventGridTopicEndpoint = "https://<your-event-grid-topic-endpoint>";
        private static readonly string eventGridTopicKey = "<your-event-grid-topic-key>";
    
        public static void Main(string[] args)
        {
            // Create an EventGrid client
            EventGridClient client = new EventGridClient(new TopicCredentials(eventGridTopicKey));
    
            // Subscribe to the event grid topic
            client.AddEventGridEventAsync(eventGridTopicEndpoint, new EventGridEvent[] {
                new EventGridEvent()
                {
                    Id = Guid.NewGuid().ToString(),
                    EventType = "SMSReceived",
                    Data = new SMSData()
                    {
                        From = "+1234567890",
                        Body = "Hello, this is a test SMS message."
                    },
                    EventTime = DateTime.UtcNow,
                    Subject = "SMS",
                    DataVersion = "1.0"
                }
            }).Wait();
    
            Console.WriteLine("Event sent.");
        }
    }
    
    // Example data class for SMS messages
    public class SMSData
    {
        public string From { get; set; }
        public string Body { get; set; }
    }
    In this example, an instance of the EventGridClient class is created using the topic credentials (endpoint and key) for your Event Grid topic.
    
    The code then sends an event to the topic using the AddEventGridEventAsync method, passing in the event data as an EventGridEvent object. In this example, the event data is a simple class called SMSData that contains the sender's phone number and the message body.
    
    You can modify this example to suit your needs, for example, you could change the event type or the data fields to match your specific SMS data structure.
    
    You also can use the AddEventAsync method to send an event to the topic, however, AddEventGridEventAsync method is recommended, this method is specifically designed for Event Grid events, and it will automatically set the correct event type and format for Event Grid.
    
    You can find more information about using Event Grid with C# in the official Microsoft documentation: https://docs.microsoft.com/en-us/azure/event-grid/send-events-csharp
    
    1 person found this answer helpful.

  3. Deepthi Murali 21 Reputation points
    2023-01-20T09:36:07.6766667+00:00

    Yes, this is possible.

    You may create subscriptions to the topic and specify the endpoint in the subscription as below:

    User's image

    There are a number of endpoint types available.

    You can specify the endpoint in Webhook or use hybrid connection, if your endpoint is on-premise.

    Thanks,

    Deepthi

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.