简体   繁体   中英

Full VB.NET code using C# lib - Twilio example for SMS and MMS message

I looked everywhere for a VB.NET example for Twilio SMS and MMS messages, after coming up empty I put one together myself. Here is the complete code. It uses a config file to store SID, Token and Caller. It is setup to take 4 parameters at run time. It is a Console app created in Visual Studio 2012.

Imports System.Configuration
Imports System.Collections.Specialized
Imports System
Imports RestSharp
Imports Twilio

Module Module1
    ' Twilio REST API version

    Const API_VERSION As String = "2010-04-01"

    Sub Main(ByVal CmdArgs() As String)
        Dim account As Twilio.TwilioRestClient
        Dim TwiGetInfo As Twilio.TwilioRestClient
        'Dim recList As Twilio.MessageListRequest
        Dim message As Twilio.Message
        Dim to1 As String
        Dim strBody As String
        Dim SID As String
        Dim Token As String
        Dim Caller As String
        Dim PostBackURL As String
        Dim strFriendlyName As String
        Dim strEventID As String
        Dim SendID As String


        If CmdArgs.Length < 1 Then
            Console.WriteLine("Both a phone number and message variable are needed")
            Console.WriteLine("Press any key to exit")
            Console.ReadKey()
            Exit Sub
        End If

        strFriendlyName = CmdArgs(0)
        to1 = CmdArgs(1)
        strEventID = CmdArgs(2)
        strBody = CmdArgs(3)


        SID = ConfigurationManager.AppSettings("Key0")
        Token = ConfigurationManager.AppSettings("Key1")
        Caller = ConfigurationManager.AppSettings("Key2")

        PostBackURL = "http://173.111.111.110:8001/XMLResponse.aspx"

        ' Create Twilio REST account object using Twilio account ID and token
        account = New Twilio.TwilioRestClient(SID, Token)
        message = New Twilio.Message


        Dim ArrMedia(0) As String
        ArrMedia(0) = Nothing

        Try
            'Overload 1
            Console.WriteLine(account.SendSmsMessage(Caller, to1, strBody, PostBackURL))
            'Overload 1 - Sends with an Image
            'Console.WriteLine(account.SendMessage(Caller, to1, strBody, ArrMedia, PostBackURL))

        Catch e As Exception
            Console.WriteLine("An error occurred: {0}", e.Message)
        End Try
        Console.WriteLine("Press any key to continue")
        Console.ReadKey()
    End Sub

End Module

looks like you were spanked for not instantiating your object prior to use? Post = New Post(Question) Post.Answer = MyAnswer.text Thus the error message for accessing a nul object. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM