-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
108 lines (97 loc) · 3.42 KB
/
Program.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
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Runtime.InteropServices;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
namespace sendsms
{
class Program
{
// Define SMS info variables
static string To { get; set; }
static string From { get; set; }
static string Message { get; set; }
static void Main(string[] args)
{
// Check how many args have been given
switch (args.Length)
{
case 3:
To = args[0];
From = args[1];
Message = args[2];
break;
case 0:
Console.Write("To: ");
To = Console.ReadLine();
Console.Write("From: ");
From = Console.ReadLine();
Console.Write("Message: ");
Message = Console.ReadLine();
break;
default:
Console.WriteLine("Error");
// TODO helpful error message
Environment.Exit(0);
break;
}
// Grab information from config file
string ConfigFile = "";
bool IsWindows = false;
try
{
// Get the file for the current OS
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
IsWindows = false;
ConfigFile = File.ReadAllText("/etc/sendsms/config.json");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
IsWindows = true;
ConfigFile = File.ReadAllText("C:\\Program Files\\sendsms\\config.json");
}
else
{
Console.WriteLine("Unsupported operating system");
Environment.Exit(0);
}
}
catch (Exception)
{
Console.WriteLine("Could not find config file at " + IsWindows switch
{
false => "/etc/sendsms/config.json",
true => "C:\\Program Files\\sendsms\\config.json"
});
/*
Config file format:
{
"AccountSID": "INSERT Account SID",
"AuthToken": "INSERT AUTH TOKEN",
"ServiceSID": "INSERT MESSAGING SERVICE SID"
}
*/
Environment.Exit(0);
}
JObject Config = JObject.Parse(ConfigFile);
TwilioClient.Init((string)Config["AccountSID"], (string)Config["AuthToken"]);
var messageOptions = new CreateMessageOptions(
new PhoneNumber(To))
{
From = new PhoneNumber(From),
MessagingServiceSid = (string)Config["ServiceSID"],
Body = Message
};
var message = MessageResource.Create(messageOptions);
Console.WriteLine("\nStatus: " + message.Status);
if (message.ErrorMessage != null)
{
Console.WriteLine(message.ErrorMessage);
Environment.Exit((int)message.ErrorCode);
}
}
}
}