简体   繁体   English

如何使用C#和twilio API发送短信

[英]How to send sms using C# and twilio API

I'm trying to create a User interface where the user can enter her credential and phone number and send messages to some recipient using Twilio API 我正在尝试创建一个用户界面,用户可以在其中输入她的凭证和电话号码,并使用Twilio API向某些收件人发送消息

So as explained I created my account and initialized the Authentication key and token 因此,我解释说,我创建了我的帐户并初始化了身份验证密钥和令牌

    private void button1_Click(object sender, EventArgs e)
    {
         string ssid = twilioSSIDBox.Text;
         string token = twilioTokenBox.Text;
         string number = twilioNumberBox.Text;


        var client = new TwilioRestClient(Environment.GetEnvironmentVariable(ssid), Environment.GetEnvironmentVariable(token));
        client.SendMessage(number, "+158965220", "Teting API message!");

    }

After multiple test (hard coding the ssid and token and number ) and documentation consulting , the message is still not being sent on the visual studio platform and I'm not receiving any error message or anything 经过多次测试(硬编码ssid和令牌和编号)和文档咨询后,仍未在visual studio平台上发送消息,我没有收到任何错误消息或任何内容

So my question is what I am missing? 所以我的问题是我错过了什么? Do I need a certain library that allow visual studio to be able to send sms messages? 我是否需要某个允许visual studio能够发送短信的库?

I'm using Visual studio 2015 and windows 10 platform 我正在使用Visual Studio 2015和Windows 10平台

Thank you 谢谢

I end up resolving my problems 我最终解决了我的问题

so this was not working on VS2015 Platform : 所以这不适用于VS2015平台:

    var client = new TwilioRestClient(Environment.GetEnvironmentVariable(ssid), Environment.GetEnvironmentVariable(token));
    client.SendMessage(number, "+158965220", "Teting API message!");

Even thought I already installed the Twilio api version 4.7.2 using nuget console 甚至以为我已经使用nuget console安装了Twilio api版本4.7.2

I did again the following : 我又做了以下几点:

Install-Package Twilio

than Visual studio suggest me to add reference to the API inside the code 比Visual Studio建议我在代码中添加对API的引用

 using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

Thanks to the following Example 感谢以下示例

this has worked : 这有效:

 TwilioClient.Init(accountSid, authToken);

        var message = MessageResource.Create(
            to: new PhoneNumber(toNumber),
            from: new PhoneNumber(FromNumber),
            body: "Hello from C#");

So my conclusion is probably an outdated library because when I used NUGET to install: 所以我的结论可能是一个过时的库,因为当我使用NUGET安装时:

Install-Package Twilio -Version 4.7.2

I have no idea why it wasn't working 我不知道它为什么不起作用

PS: I'm not sure if it was relevant but I also installed this using NUGET console it mighed helped or it might not but I felt like I had to mention it : PS:我不确定它是否相关,但是我也使用NUGET控制台安装了它,它的迁移帮助或者可能没有,但我觉得我必须提到它:

Install-Package RestSharp 

See if you can get any help from the below code: 看看您是否可以从以下代码获得任何帮助:

/// <summary>
    /// Sends alert as SMS 
    /// </summary>
    /// <param name="details"></param>
    /// <returns></returns>
    public static Message SendSms(DeliveryDetails details)
    {
        var messageResult = new Message();
        try
        {
            if (details?.ToNumber != null)
            {
                var toNumberList = details.ToNumber.ToList();
                if (toNumberList.Count > 0)
                {
                    foreach (var toNumber in toNumberList)
                    {
                        messageResult = Twilio.SendMessage(FromNumber, toNumber, $"{details.Subject}\n\n{details.Message}");

                        if (messageResult == null)
                        {
                            logger.Error(string.Format(
                                "Error connecting to Twilio, message sending failed to {0}",
                                toNumber));
                        }
                        else if (messageResult.RestException != null)
                        {
                            logger.Error(string.Format("Twilio Error Message Description - {0}",
                                messageResult.RestException.Message));
                        }
                        else
                        {
                            logger.Info(String.Format("SMS {0} deliverd to {1}", messageResult.Body, messageResult.To));
                        }
                    }
                }
                else
                {
                    logger.Error("ToNumber List Empty");
                }
            }
            else
            {
                logger.Error("ToNumber List Null");
            }
        }
        catch (Exception e)
        {
            logger.Error(String.Format("An error occurred while sending the message\n{0}", e.Message));
        }

        return messageResult;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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