10000 Add completed TwilioSmsSink logic by g8keller · Pull Request #934 · azist/azos · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add completed TwilioSmsSink logic #934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion src/Azos.Sky/Messaging/Sinks/TwilioSmsSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,66 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text;
using Azos.Client;
using Azos.Glue.Protocol;
using Azos.Web;
using Azos.Serialization.JSON;

namespace Azos.Sky.Messaging.Sinks
{
class TwilioSmsSink
class TwilioSmsSink : TwilioSinkBase
{
const string FROM_PHONE_NUMBER = "+18555450552";

public TwilioSmsSink(MessageDaemon director) : base(director)
{
}

public override MsgChannels SupportedChannels => MsgChannels.SMS;

protected override bool DoSendMsg(Message message)
{
if (!checkMessage(message))
{
WriteLogFromHere(Azos.Log.MessageType.Warning,
"SMS message data not qualified for Twilio API",
related: message.Id,
pars: new { msg = message.ShortBody.TakeFirstChars(64, ".."), t = message.AddressTo }.ToJson(JsonWritingOptions.CompactASCII)
);
return false;
}

// Twilio requires that each message is its own call
foreach (var recipient in message.AddressToBuilder.GetMatchesForChannels(SupportedChannelNames))
{

// Twilio didn't seem to like it when you give them raw JSON as a body.
var smsContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("To", recipient.Address),
new KeyValuePair<string, string>("From", FROM_PHONE_NUMBER),
new KeyValuePair<string, string>("Body", message.ShortBody),
});

var task = m_Twilio.Call(TwilioServiceAddress,
nameof(TwilioSmsSink),
new ShardKey(message.Id), //sharding
(http, ct) => http.Client.PostAndGetJsonMapAsync("send", smsContent));

task.Await(); //complete synchronously by design
}

return true;
}

private bool checkMessage(Message message)
{
var toAddresses = message.AddressToBuilder.GetMatchesForChannels(SupportedChannelNames);
return message.ShortBody.IsNotNullOrWhiteSpace() && toAddresses.Any();
}
}
}
Loading
0