使用 C# lib 的完整 VB.NET 代码 - 短信和彩信的 Twilio 示例

本文关键字:示例 Twilio NET lib 使用 VB 代码 | 更新日期: 2023-09-27 18:35:13

我到处寻找Twilio短信和彩信的 VB.NET 示例,在空了之后,我自己把一个放在一起。这是完整的代码。 它使用配置文件来存储 SID、令牌和调用方。它设置为在运行时采用 4 个参数。 它是在Visual Studio 2012中创建的控制台应用程序。

使用 C# lib 的完整 VB.NET 代码 - 短信和彩信的 Twilio 示例

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

看起来你因为没有在使用前实例化你的对象而被打屁股?帖子 = 新帖子(问题)Post.Answer = MyAnswer.text因此,访问 NUL 对象的错误消息。 :-)