|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Data; |
| 5 | +using System.Drawing; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using System.Windows.Forms; |
| 10 | +using System.Net.Http.Headers; |
| 11 | +using System.Net.Http; |
| 12 | +using System.IO; |
| 13 | +using Newtonsoft.Json; |
| 14 | +using Newtonsoft.Json.Linq; |
| 15 | + |
| 16 | + |
| 17 | +namespace XqLineNotifier |
| 18 | +{ |
| 19 | + public partial class MainForm : Form |
| 20 | + { |
| 21 | + public MainForm() |
| 22 | + { |
| 23 | + InitializeComponent(); |
| 24 | + } |
| 25 | + |
| 26 | + private string lineToken = ""; |
| 27 | + private static string AppJsonFilePath = "./lineToken.json"; |
| 28 | + private static string XqMonitorPath = "./"; |
| 29 | + private static string XqLogFileExtension = "*.xqlog"; |
| 30 | + |
| 31 | + public void CreateFileWatcher(string path) |
| 32 | + { |
| 33 | + // Create a new FileSystemWatcher and set its properties. |
| 34 | + FileSystemWatcher watcher = new FileSystemWatcher(); |
| 35 | + watcher.Path = path; |
| 36 | + /* Watch for changes in LastAccess and LastWrite times, and |
| 37 | + the renaming of files or directories. */ |
| 38 | + watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite |
| 39 | + | NotifyFilters.FileName | NotifyFilters.DirectoryName; |
| 40 | + |
| 41 | + // Add event handlers. |
| 42 | + // Only watch *.xqlog files |
| 43 | + watcher.Filter = XqLogFileExtension; |
| 44 | + watcher.Changed += new FileSystemEventHandler(OnChanged); |
| 45 | + watcher.Created += new FileSystemEventHandler(OnCreatedAsync); |
| 46 | + watcher.Deleted += new FileSystemEventHandler(OnChanged); |
| 47 | + watcher.Renamed += new RenamedEventHandler(OnRenamed); |
| 48 | + |
| 49 | + // Begin watching. |
| 50 | + watcher.EnableRaisingEvents = true; |
| 51 | + } |
| 52 | + |
| 53 | + // Define the event handlers. |
| 54 | + private async void OnCreatedAsync(object source, FileSystemEventArgs e) |
| 55 | + { |
| 56 | + // Specify what is done when a file is changed, created, or deleted. |
| 57 | + Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); |
| 58 | + |
| 59 | + try |
| 60 | + { |
| 61 | + if (this.lineToken.Trim() != "") |
| 62 | + { |
| 63 | + string message = File.ReadAllText(e.FullPath, Encoding.GetEncoding("Big5")); |
| 64 | + |
| 65 | + HttpClient client = new HttpClient(); |
| 66 | + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this.lineToken); |
| 67 | + var form = new FormUrlEncodedContent(new[] |
| 68 | + { |
| 69 | + new KeyValuePair<string, string>("message", message) |
| 70 | + }); |
| 71 | + HttpResponseMessage result = await client.PostAsync(new Uri("https://notify-api.line.me/api/notify"), form); |
| 72 | + } |
| 73 | + File.Delete(e.FullPath); |
| 74 | + } |
| 75 | + catch (Exception ex) |
| 76 | + { |
| 77 | + MessageBox.Show(ex.ToString()); |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + private static void OnChanged(object source, FileSystemEventArgs e) |
| 83 | + { |
| 84 | + // Specify what is done when a file is changed, created, or deleted. |
| 85 | + Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); |
| 86 | + } |
| 87 | + |
| 88 | + private static void OnRenamed(object source, RenamedEventArgs e) |
| 89 | + { |
| 90 | + // Specify what is done when a file is renamed. |
| 91 | + Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath); |
| 92 | + } |
| 93 | + |
| 94 | + public class AppSettings |
| 95 | + { |
| 96 | + public string LineToken { get; set; } |
| 97 | + } |
| 98 | + |
| 99 | + private AppSettings readAppSettings() |
| 100 | + { |
| 101 | + var jsonString = System.IO.File.ReadAllText(AppJsonFilePath); |
| 102 | + AppSettings appSettings = JsonConvert.DeserializeObject<AppSettings>(jsonString); |
| 103 | + return appSettings; |
| 104 | + } |
| 105 | + |
| 106 | + private void checkXqLineNotifyPathExists() |
| 107 | + { |
| 108 | + if (!Directory.Exists(XqMonitorPath)) |
| 109 | + { |
| 110 | + Directory.CreateDirectory(XqMonitorPath); |
| 111 | + } |
| 112 | + } |
| 113 | + private void createAppSettingsFile() |
| 114 | + { |
| 115 | + var appJsonObj = new AppSettings() { LineToken = "" }; |
| 116 | + System.IO.File.WriteAllText(AppJsonFilePath, JsonConvert.SerializeObject(appJsonObj)); |
| 117 | + } |
| 118 | + |
| 119 | + private void initAppSettings() |
| 120 | + { |
| 121 | + |
| 122 | + try |
| 123 | + { |
| 124 | + if (!System.IO.File.Exists(AppJsonFilePath)) |
| 125 | + { |
| 126 | + this.createAppSettingsFile(); |
| 127 | + } |
| 128 | + AppSettings appSettings = this.readAppSettings(); |
| 129 | + lineToken = appSettings.LineToken; |
| 130 | + txtLineToken.Text = lineToken; |
| 131 | + } |
| 132 | + catch (Exception e) |
| 133 | + { |
| 134 | + this.createAppSettingsFile(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private void MainForm_Load(object sender, EventArgs e) |
| 139 | + { |
| 140 | + checkXqLineNotifyPathExists(); |
| 141 | + initAppSettings(); |
| 142 | + createXqMonitorPath(); |
| 143 | + deleteAllXqLog(); |
| 144 | + CreateFileWatcher(XqMonitorPath); |
| 145 | + } |
| 146 | + |
| 147 | + private void createXqMonitorPath() { |
| 148 | + if (!System.IO.Directory.Exists(XqMonitorPath)) { |
| 149 | + System.IO.Directory.CreateDirectory(XqMonitorPath); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private void deleteAllXqLog() { |
| 154 | + foreach (string sFile in System.IO.Directory.GetFiles(XqMonitorPath, XqLogFileExtension)) |
| 155 | + { |
| 156 | + System.IO.File.Delete(sFile); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private void btnSave_Click(object sender, EventArgs e) |
| 161 | + { |
| 162 | + txtLineToken.Text = txtLineToken.Text.Trim(); |
| 163 | + AppSettings appSettings = this.readAppSettings(); |
| 164 | + appSettings.LineToken = txtLineToken.Text; |
| 165 | + string jsonString = JsonConvert.SerializeObject(appSettings); |
| 166 | + System.IO.File.WriteAllText(AppJsonFilePath, jsonString); |
| 167 | + this.lineToken = txtLineToken.Text; |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments