forked from Vonage/vonage-dotnet-code-snippets
/
SMSController.cs
119 lines (97 loc) · 3.38 KB
/
SMSController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using Nexmo.Api;
using System.Diagnostics;
using System.Web.Http;
using System.Web.Mvc;
namespace NexmoDotNetQuickStarts.Controllers
{
public class SMSController : Controller
{
public Client Client { get; set; }
public SMSController()
{
Client = new Client(creds: new Nexmo.Api.Request.Credentials
{
ApiKey = "NEXMO_API_KEY",
ApiSecret = "NEXMO_API_SECRET"
});
}
public ActionResult Index()
{
return View();
}
[System.Web.Mvc.HttpGet]
public ActionResult Send()
{
return View();
}
[System.Web.Mvc.HttpPost]
public ActionResult Send(string to)
{
var TO_NUMBER = to;
var results = Client.SMS.Send(request: new SMS.SMSRequest
{
from = "Acme Inc",
to = TO_NUMBER,
text = "A test SMS sent using the Nexmo SMS API"
});
if (results.messages.Count >= 1)
{
if (results.messages[0].status == "0")
Debug.WriteLine("Message sent successfully.");
else
Debug.WriteLine($"Message failed with error: {results.messages[0].error_text}");
}
return View("Index");
}
[System.Web.Mvc.HttpGet]
public ActionResult SendUnicodeSMS()
{
return View();
}
[System.Web.Mvc.HttpPost]
public ActionResult SendUnicodeSMS(string to, string text)
{
var TO_NUMBER = to;
var results = Client.SMS.Send(request: new SMS.SMSRequest
{
from = "Acme Inc",
to = TO_NUMBER,
text = "こんにちは世界",
type = "unicode"
});
return View("Index");
}
[System.Web.Mvc.HttpGet]
public ActionResult Receive([FromUri]SMS.SMSInbound response)
{
if (null != response.to && null != response.msisdn)
{
Debug.WriteLine("------------------------------------");
Debug.WriteLine("INCOMING TEXT");
Debug.WriteLine("From: " + response.msisdn);
Debug.WriteLine(" Message: " + response.text);
Debug.WriteLine("------------------------------------");
return new HttpStatusCodeResult(200);
}
else
{
Debug.WriteLine("------------------------------------");
Debug.WriteLine("Endpoint was hit.");
Debug.WriteLine("------------------------------------");
return new HttpStatusCodeResult(200);
}
}
[System.Web.Mvc.HttpGet]
public ActionResult DLR([FromUri]SMS.SMSDeliveryReceipt response)
{
Debug.WriteLine("------------------------------------");
Debug.WriteLine("DELIVERY RECIEPT");
Debug.WriteLine("Message ID: " + response.messageId);
Debug.WriteLine("From: " + response.msisdn);
Debug.WriteLine("To: " + response.to);
Debug.WriteLine("Status: " + response.status);
Debug.WriteLine("------------------------------------");
return new HttpStatusCodeResult(200);
}
}
}