使用Twilio-C发送消息#

时间:2016-05-25 04:24:54

标签: c# twilio

Hy ......我正在学习twilio,我在这里看到了一篇帖子http://www.markhagan.me/Samples/Send-SMS-Using-Twilio-ASPNet

我已经制作了自己的代码,因为在上面的网站" sendSMSMessage"现已弃用,但这是我的代码:

using System.Text;
using System.Threading.Tasks;
using Twilio;
namespace SMSUsingTwilio
{
    class Program
    {
        static void Main(string[] args)
        {
            String ACCOUNT_SID = "ACMYSID"; 
            String AUTH_TOKEN = "40MYAUTHTOKEN";

            TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

            Message Response = client.SendMessage("(732)305-8856", "+6285220446195", "Hellow Hyosoka Poipo :D");


            Console.WriteLine(Response.Status);
            Console.WriteLine(Response.AccountSid);

            Console.WriteLine("SMS Berhasil di kirim");
            Console.ReadLine();    
        }
    }
}

问题是我没有给我的电话号码留言,甚至我在C#项目中也没有得到任何回复: enter image description here

那么这里有什么不对......?请帮忙..非常感谢你...... :)

4 个答案:

答案 0 :(得分:3)

看到David先生回答后,我意识到我的电话号码尚未验证。请转到此链接以验证我的电话号码:

https://www.twilio.com/user/account/phone-numbers/verified

之后,我运行我的项目agian,结果如下: enter image description here

Yeeeiii ...非常感谢你的评论和回答......我真的很感激......:)

答案 1 :(得分:2)

首先,您需要从Numget Manager安装Twillio NuGet软件包 除此以外, 您可以编写代码Install-Package Twilio 在“程序包管理器”控制台中。

  • 您需要从https://www.twilio.com创建Twillo帐户
  • 现在您将获得AccountId,AuthToken等
  • 然后,您将需要在项目中实现以下代码:
public async Task<string> SendTwilioSMS(string phoneNumber, string SMS)
{
            string returnMessage = string.Empty;
            string bodySMS = SMS;
            var twilioAccountSid = "AC754fec249d22766caf0ae4e58a158271";
            var twilioAuthToken = "cfecd5ff4d751677fc2e2875e3739b55";
            var twilioMessagingServiceSid = "MGd57fcc863cb37dcff135aca43b4bb7d1";
            var twilioPhoneNumber = "+919714285344";

            bodySMS = SMS;
            TwilioClient.Init(twilioAccountSid, twilioAuthToken);
            try
            {
                MessageResource twillioResult = await MessageResource.CreateAsync(
                   to: new PhoneNumber(phoneNumber),
                   from: new PhoneNumber(twilioPhoneNumber),
                   body: bodySMS,
                   messagingServiceSid: twilioMessagingServiceSid
                   );
                returnMessage = "Message sent";
            }
            catch (Exception err)
            {
                returnMessage = err.Message;
            }
            return returnMessage;
 }

答案 2 :(得分:1)

以上看起来很好:

var message = client.SendMessage("(732)305-8856", "+6285220446195", "Hellow Hyosoka Poipo :D");

示例:

static void Main(string[] args) 
{ 
    string AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 
    string AuthToken = "[AuthToken]"; 
    var twilio = new TwilioRestClient(AccountSid, AuthToken); 

    var message = twilio.SendMessage("(732)305-8856", "+6285220446195", "Hellow Hyosoka Poipo :D"); 
    ); 
    Console.WriteLine(message.Sid); 
 } 

检查一下: https://www.twilio.com/docs/api/rest/sending-messages

我无法识别您的电话号码,如果上述示例不起作用,则会导致您的帐户或号码格式出现问题。

答案 3 :(得分:0)

如果是.Net Core,则应从appsettings文件中获取该值。

public async Task SendSMS()
{
    var sid = "0845-373A-90fy-5790";
    var authToken = "5a983-498f94-2849o8934-28455s9";
    try
    {
        TwilioClient.Init(sid , authToken );
    
        var message = await MessageResource.CreateAsync(
            body: "Hi",
            from: new Twilio.Types.PhoneNumber("+12564598"),
            to: new Twilio.Types.PhoneNumber("9467345243"));
    }
    catch (Exception ex)
    {
        //Log Exception
    }
}