I am trying to create an Azure function to receive the body of an sms received by our Twilio number and show it on a MS Teams channel. However, I am facing a lot of issus and my code is not working. is there someone here who has done this before so they can help me?
1-I have created an Azure function app and selected powershell as the language for code. 2- I have added the TwilioAccountSid and TwilioAuthToken to the function on configuration>aplication setting 3- I have created a http triger funcion and put function URL on Twilio's URl wher it sdays messaging configuration
4- I have added a project.json and requirement.psd1 files to home\sites\wwroot fileder the content of project.jason is:
{
"frameworks": {
"net46":{
"dependencies": {
"Twilio": "5.0.2"
}
}
}
}
the content of requirement.psd1 is :
# This file enables modules to be automatically managed by the Functions service.
# See https://aka.ms/functionsmanageddependency for additional information.
#
@{
'dependencies' = @{
'Twilio' = '5.71.1'
}
}
@{
'dependencies' = @{
'Microsoft.Azure.WebJobs.Extensions.Twilio' = '5.0.0'
}
}
@{
'dependencies' = @{
'Microsoft.Azure.WebJobs.Extensions.Http' = '4.0.4'
}
}
the fnction code is:
`# Function to handle the HTTP request from Twilio
param($req, $triggerMetadata)
function Run($req, $triggerMetadata)
{
# Import the Twilio module from the Modules folder
Import-Module "$PSScriptRoot\Modules\Twilio"
# Retrieve the Twilio credentials from the Azure Function app settings
$accountSid = $env:TwilioAccountSid
$authToken = $env:TwilioAuthToken
# Parse the request body to get the SMS content
$body = $req.Body | ConvertFrom-Json
$messageSid = $body.Body
$from = $body.From
$to = $body.To
$smsBody = $body.Body
# Display the SMS content
Write-Host "SMS received:"
Write-Host "From: $from"
Write-Host "To: $to"
Write-Host "Body: $smsBody"
# Perform any additional processing with the SMS content here
# Return a response to Twilio
return [HttpResponseContext]@{
StatusCode = 200
Body = "SMS received successfully"
}
}