Loading...

Build a conversational SMS bot with Azure Communication Services and Azure OpenAI

Build a conversational SMS bot with Azure Communication Services and Azure OpenAI

It is no secret that large language models (LLM) like ChatGPT have been all the rage in the last couple of months. These conversational models offer seamless and intuitive interfaces for users to interact with, enabling them to easily ask questions or carry out tasks.  The Azure Communication Services team strives to provide developers with the tools to integrate these conversational entities with communication channels, and delight end users with exceptional support. In this blog we will show you how to use Azure Communication Services SMS capability and Azure OpenAI Service’s GPT-3 model to light up a personalized end user interaction scenario.

In today's blog, we are building an Obi Wan Kanobi conversational bot powered by Azure OpenAI and Azure Communication Services SMS. See the preview:

obi-wan-demo.gif

 

To follow along, you will need: 

 

You can find the finished code for this blog on GitHub.

 

This application will leverage Azure Event Grid to listen for incoming text messages to Azure Communication Services number and an Azure Function to process the event and respond with an Azure OpenAI generated response. 

 

We will start by configuring an Azure Function to receive Azure Event Grid events. To create the Azure Function, you can follow instructions to set it up directly on Visual Studio Code. (Ensure for the Azure Function to be of type EventGridTrigger). In this blog, we will jump ahead and show the configured Azure Function. 

 

 

import { AzureFunction, Context } from "@azure/functions" const eventGridTrigger: AzureFunction = async function (context: Context, eventGridEvent: any): Promise<void> { context.log(eventGridEvent); const to = eventGridEvent['data']['to']; const from = eventGridEvent['data']['from']; const message = eventGridEvent['data']['message']; // Insert Azure Open AI and SMS code here }; export default eventGridTrigger;

 

 

Next, we will add a call to Azure OpenAI to ask our model to generate a response. We will use REST APIs to POST a request with our prompt. For the prompt, we will use a combination of the message sent by the user and a pre-designed text. In this example, we want the GPT-3 model to act like Obi Wan Kenobi. We added some sample quotes for the model to draw inspiration from. These quotes help guide the model’s response and provide a more intuitive and conversational flow for the users. 

 

 

You're Obi Wan Kenobi and you are having a deep meaningful conversation with a student. Examples of the types of things that Kenobi says: - "You must do what you feel is right of course. " - "Who's the more foolish, the fool or the fool who follows him?" - "Your eyes can deceive you, don't trust them." - "Don't give in to hate, that leads to the dark side." - "War tends to distort our point of view. If we sacrifice our code, even for victory, we may lose that which is important—our honor." - "If you define yourself by the power to take life, the desire to dominate, to possess—then you have nothing." Kenobi: Welcome to the Jedi Academy! Big things await for you over the next few years. Student:

 

 

We concatenate the prompt with the user’s message. 

 

 

// Azure Open AI context var url = '<Azure OpenAI Endpoint>/openai/deployments/<Deployment Name>/completions?api-version=2022-12-01' var instructions = 'You\'re Obi Wan Kenobi and you\'re have a deep meaningful conversation with a student. Example of the types of things that Kenobi says: \n- "You must do what you feel is right of course. " \n - "Who\'s the more foolish, the fool or the fool who follows him?" \n - "Your eyes can deceive you, don\'t trust them." \n - "Don\'t give in to hate, that leads to the dark side." \n - "War tends to distort our point of view. If we sacrifice our code, even for victory, we may lose that which is important—our honor." \n - "If you define yourself by the power to take life, the desire to dominate, to possess—then you have nothing." \n Kenobi: Welcome to the Jedi Academy! Big things await for you over the next few years. \n Student:' var prompt = instructions + message + '\n Kenobi:' fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', 'api-key':'<Azure OpenAI key>' }, body: JSON.stringify({ prompt}) }) .then(res => res.json()) .then(async data => { console.log(data['choices'][0]['text'])})

 

 

Finally, we will configure our SMS Client to respond with the new response generated by Azure OpenAI. You will need Azure Communication Services connection string to initialize the SMS Client.  You can either paste the connection string directly in the code or place it inside your local.settings.json file in your Azure Function directory under values.  

 

 

{ "IsEncrypted": false, "Values": { "FUNCTIONS_WORKER_RUNTIME": "node", "ACS_CONNECTION_STRING": "<<CONNECTION STRING>>" } }

 

 

Then we will modify the function itself to add our SMS client logic.

 

 

import { AzureFunction, Context } from "@azure/functions" import { SmsClient } from "@azure/communication-sms"; const connectionString = process.env.ACS_CONNECTION_STRING; //Replace with your connection string const eventGridTrigger: AzureFunction = async function (context: Context, eventGridEvent: any): Promise<void> { context.log(eventGridEvent); const to = eventGridEvent['data']['to']; const from = eventGridEvent['data']['from']; const message = eventGridEvent['data']['message']; // Open AI context var url = '<Azure OpenAI Endpoint>/openai/deployments/<Deployment Name>/completions?api-version=2022-12-01' var prompt = 'You\'re Obi Wan Kenobi and you\'re have a deep meaningful conversation with a student. Example of the types of things that Kenobi says: \n- "You must do what you feel is right of course. " \n - "Who\'s the more foolish, the fool or the fool who follows him?" \n - "Your eyes can deceive you, don\'t trust them." \n - "Don\'t give in to hate, that leads to the dark side." \n - "War tends to distort our point of view. If we sacrifice our code, even for victory, we may lose that which is important—our honor." \n - "If you define yourself by the power to take life, the desire to dominate, to possess—then you have nothing." \n Kenobi: Welcome to the Jedi Academy! Big things await for you over the next few years. \n Student:' var fullPrompt = prompt + message + '\n Kenobi:' fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', 'api-key':'<Azure OpenAI key>' }, body: JSON.stringify({ prompt}) }) .then(res => res.json()) .then(async data => { var generatedText = data['choices'][0]['text'] const smsClient = new SmsClient(connectionString); const sendResults = await smsClient.send({ from: to, to: [from], message: generatedText }); }) }; export default eventGridTrigger;

 

 

To run the function locally, simply press F5 in Visual Studio Code. We will use ngrok  to hook our locally running Azure Function with Azure Event Grid. You will need to download ngrok  for your environment. Once the function is running, we will configure ngrok. 

 

 

ngrok http 7071

 

 

Copy the ngrok link provided where your function is running. 

 

Finally, we configure SMS events through Event Grid in your Azure Communication Services resource. We will do this using the Azure CLI . You will need the Azure Communication Services resource ID found in the Azure Portal.  (The resource ID will look something like:  /subscriptions/<<AZURE SUBSCRIPTION ID>>/resourceGroups/<<RESOURCE GROUP NAME>>/providers/Microsoft.Communication/CommunicationServices/<<RESOURCE NAME>>)

 

 

az eventgrid event-subscription create --name "<<EVENT_SUBSCRIPTION_NAME>>" --endpoint-type webhook --endpoint "<<NGROK URL/runtime/webhooks/EventGrid?functionName=FUNCTION NAME>> " --source-resource-id "<<RESOURCE_ID>>" --included-event-types Microsoft.Communication.SMSReceived

 

 

Now that everything is hooked up, test the flow by sending an SMS to the phone number in the Azure Communication Services resource.

Published on:

Learn more
Azure Communication Services Blog
Azure Communication Services Blog

Azure Communication Services Blog articles

Share post:

Related posts

Copilot Studio – Environment-level agent telemetry export to Azure Application Insights (Preview)

We are announcing the ability for administrators to export Copilot Studio agent telemetry at the environment level to Azure Application Insigh...

1 day ago

See our new Azure Cosmos DB Design Patterns

Design patterns are where good data modeling lives or dies. In a NoSQL database like Azure Cosmos DB, the difference between a schema that sca...

1 day ago

Need a different partition key in Azure Cosmos DB? Pick the right approach

Once you create a container, its partition key is fixed at creation, and you can’t change it in place. However, if your original key starts ca...

1 day ago

Azure SDK Release (June 2026)

Azure SDK releases every month. In this post, you'll find this month's highlights and release notes. The post Azure SDK Release (June 2026) ap...

2 days ago

Fundamentals of Azure DevOps with SQL projects

Building automated pipelines with your SQL database projects enables you to build a rich CI/CD ecosystem to ensure that your application is be...

5 days ago

Upcoming Change: NTLM Removal in Git (libcurl) – Impact to Azure DevOps Server Customers

Overview In September 2026, NTLM support will be removed from libcurl, which is used by Git for HTTP(S) operations. As a result, Git operation...

6 days ago

What’s new across Microsoft SQL in 2026 so far (SQL Server, Azure SQL, and SQL database in Fabric)

We’re halfway through 2026, and Microsoft SQL has not slowed down. Since SQLCon/FabCon in March (where we released a ton of things, and those ...

6 days ago

Power Automate Flow — HTTP Trigger to Azure OpenAI

Build the secure Power Automate HTTP trigger flow that receives free text from the portal, calls Azure OpenAI using your smart-form-extract de...

8 days ago

Spring AI 2.0 is GA: Vector Search, Memory, and Agents on Azure Cosmos DB

The wait is over. Spring AI 2.0 is generally available, and Azure Cosmos DB is right there with it. With this release, Spring AI graduates int...

9 days ago
Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!
* Yes, I agree to the privacy policy