SQL Server Requires Chilkat v11.0.0+
SQL Server
Schedule or cancel an SMS with Twilio
See more Twilio Examples
Live messaging operation. Choose one action per run. Scheduling requires an SMS-capable sender in a Twilio Messaging Service. Supply its MG SID, a recipient in E.164 format, and a send time from 15 minutes to 35 days in the future. The example uses UTC one hour from now. Twilio will send the scheduled message unless it is canceled in time.
Save the returned Message SID. To cancel later, select the cancel action and supply that SID under the same account. Check the returned status. If the message has already left the scheduled state, fetch its current status.
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @iTmp1 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
-- Load an API key for the Twilio account from private application configuration.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @http, 'BasicAuth', 1
EXEC sp_OASetProperty @http, 'Login', 'YOUR_API_KEY_SID'
EXEC sp_OASetProperty @http, 'Password', 'YOUR_API_KEY_SECRET'
DECLARE @url int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @url OUT
EXEC sp_OAMethod @url, 'Append', @success OUT, 'https://api.twilio.com/2010-04-01/Accounts/'
EXEC sp_OAMethod @url, 'Append', @success OUT, 'YOUR_ACCOUNT_SID'
EXEC sp_OAMethod @url, 'Append', @success OUT, '/Messages'
-- Set this to true on a later run to cancel the saved message.
DECLARE @cancelMessage int
SELECT @cancelMessage = 0
DECLARE @req int
EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT
DECLARE @sendTime int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @sendTime OUT
EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
EXEC sp_OASetProperty @req, 'ContentType', 'application/x-www-form-urlencoded'
DECLARE @expectedHttp int
SELECT @expectedHttp = 201
DECLARE @expectedStatus nvarchar(4000)
SELECT @expectedStatus = 'scheduled'
IF @cancelMessage <> 1
BEGIN
DECLARE @timeOk int
EXEC sp_OAMethod @sendTime, 'SetFromCurrentSystemTime', @timeOk OUT
IF @timeOk <> 1
BEGIN
EXEC sp_OAGetProperty @sendTime, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @sendTime
RETURN
END
EXEC sp_OAMethod @sendTime, 'AddSeconds', @timeOk OUT, 3600
IF @timeOk <> 1
BEGIN
EXEC sp_OAGetProperty @sendTime, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @sendTime
RETURN
END
EXEC sp_OAMethod @req, 'AddParam', NULL, 'MessagingServiceSid', 'YOUR_MESSAGING_SERVICE_SID'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'To', 'RECIPIENT_E164_NUMBER'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'Body', 'Scheduled message.'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'ScheduleType', 'fixed'
EXEC sp_OAMethod @sendTime, 'GetAsTimestamp', @sTmp0 OUT, 0
EXEC sp_OAMethod @req, 'AddParam', NULL, 'SendAt', @sTmp0
END
ELSE
BEGIN
-- Update the individual message. This branch never creates another message.
EXEC sp_OAMethod @url, 'Append', @success OUT, '/'
EXEC sp_OAMethod @url, 'Append', @success OUT, 'SAVED_SCHEDULED_MESSAGE_SID'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'Status', 'canceled'
SELECT @expectedHttp = 200
SELECT @expectedStatus = 'canceled'
END
EXEC sp_OAMethod @url, 'Append', @success OUT, '.json'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
DECLARE @reply int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @reply OUT
EXEC sp_OAMethod @url, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpReq', @success OUT, @sTmp0, @req, @resp
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
PRINT 'No HTTP response from Twilio.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @sendTime
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
RETURN
END
DECLARE @parsed int
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @reply, 'Load', @parsed OUT, @sTmp0
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF (@success <> 1) or (@iTmp0 <> @expectedHttp)
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'Twilio returned HTTP ' + @iTmp0
EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp0 OUT, 'code'
IF (@parsed = 1) and (@iTmp0 = 2)
BEGIN
EXEC sp_OAMethod @reply, 'IntOf', @iTmp0 OUT, 'code'
EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'message'
PRINT 'Twilio error: ' + @iTmp0 + ' ' + @sTmp0
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @sendTime
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
RETURN
END
IF @parsed <> 1
BEGIN
PRINT 'Expected a JSON response from Twilio.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @sendTime
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
RETURN
END
EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp0 OUT, 'sid'
EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp1 OUT, 'status'
IF (@iTmp0 <> 1) or (@iTmp1 <> 1)
BEGIN
PRINT 'The response is missing the message SID or status.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @sendTime
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
RETURN
END
EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'sid'
PRINT 'Message SID: ' + @sTmp0
EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'status'
PRINT 'Current status: ' + @sTmp0
EXEC sp_OAMethod @reply, 'StringOfEquals', @iTmp0 OUT, 'status', @expectedStatus, 1
IF @iTmp0 <> 1
BEGIN
PRINT 'The requested state was not confirmed. Check this SID before trying again.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @sendTime
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
RETURN
END
IF @cancelMessage <> 1
BEGIN
PRINT 'Save the Message SID to check or cancel the scheduled message.'
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @sendTime
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
END
GO