-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTelegramProtocol.cs
185 lines (170 loc) · 7.3 KB
/
TelegramProtocol.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace TrixieBot
{
public class TelegramProtocol : BaseProtocol
{
private TelegramBotClient bot;
private User me;
public TelegramProtocol(Config config) : base(config)
{
this.config = config;
}
public override async Task<bool> Start()
{
Console.WriteLine(DateTime.Now.ToString("M/d HH:mm") + " Telegram Protocol starting...");
bot = new TelegramBotClient(config.Keys.TelegramKey);
me = await bot.GetMeAsync().ConfigureAwait(false);
Console.WriteLine(DateTime.Now.ToString("M/d HH:mm") + " " + me.Username + " started at " + DateTime.Now);
// Start RSS / Atom processing if there are any items for this protocol
Console.WriteLine(DateTime.Now.ToString("M/d HH:mm") + " Checking for Telegram RSS...");
var offset = 0;
//await bot.LeaveChatAsync(1234).ConfigureAwait(false); // Example
while (true)
{
var updates = Array.Empty<Update>();
try
{
updates = await bot.GetUpdatesAsync(offset).ConfigureAwait(false);
}
catch (TaskCanceledException)
{
// Don't care
}
catch (Exception ex)
{
Console.WriteLine(DateTime.Now.ToString("M/d HH:mm") + " ERROR WHILE GETTING UPDATES - " + ex);
}
foreach (var update in updates)
{
offset = update.Id + 1;
try
{
switch (update.Type)
{
case UpdateType.Message:
if (update.Message.Text != null)
{
Processor.TextMessage(this, config, update.Message.Chat.Id.ToString(),
update.Message.Text.Replace("@" + me.Username, ""),
update.Message.From.Username, update.Message.From.FirstName + " " + update.Message.From.LastName,
update.Message.ReplyToMessage == null ? "" : update.Message.ReplyToMessage.Text);
}
break;
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR WHILE PROCESSING:\r\n" + ex);
}
}
await Task.Delay(1000).ConfigureAwait(false);
}
}
public override void SendFile(string destination, string Url, string filename = "", string referrer = "https://duckduckgo.com")
{
Console.WriteLine(DateTime.Now.ToString("M/d HH:mm") + " " + destination + " > " + Url);
SendStatusTyping(destination);
var httpClient = new ProHttpClient()
{
ReferrerUri = referrer
};
using var stream = httpClient.DownloadData(Url).Result;
var inputFile = InputFile.FromStream(stream);
if (filename?.Length == 0)
{
filename = Url.Substring(Url.LastIndexOf("/") + 1, 9999);
}
bot.SendDocumentAsync(destination, inputFile, caption: filename);
}
public override void SendImage(string destination, string Url, string caption, string referrer = "https://duckduckgo.com")
{
Console.WriteLine(DateTime.Now.ToString("M/d HH:mm") + " " + destination + " > " + Url + " : " + caption);
SendStatusTyping(destination);
try
{
var httpClient = new ProHttpClient()
{
ReferrerUri = referrer
};
using var stream = httpClient.DownloadData(Url).Result;
var inputFile = InputFile.FromStream(stream);
var extension = ".jpg";
if (Url.Contains(".gif") || Url.Contains("image/gif"))
{
extension = ".gif";
}
else if (Url.Contains(".png") || Url.Contains("image/png"))
{
extension = ".png";
}
else if (Url.Contains(".tif"))
{
extension = ".tif";
}
else if (Url.Contains(".bmp"))
{
extension = ".bmp";
}
SendStatusUploadingPhoto(destination);
if (extension == ".gif")
{
bot.SendDocumentAsync(destination, inputFile);
}
else
{
bot.SendPhotoAsync(destination, inputFile, caption: caption?.Length == 0 ? Url : caption);
}
}
catch (System.Net.Http.HttpRequestException ex)
{
Console.WriteLine("Unable to download " + ex.HResult + " " + ex.Message);
SendPlainTextMessage(destination, Url);
}
catch (System.Net.WebException ex)
{
Console.WriteLine("Unable to download " + ex.HResult + " " + ex.Message);
SendPlainTextMessage(destination, Url);
}
catch (Exception ex)
{
Console.WriteLine(Url + " Threw: " + ex.Message);
SendPlainTextMessage(destination, "The Great & Powerful Trixie got bored while waiting for that to download. Try later. " + ex.Message);
}
}
public override void SendHTMLMessage(string destination, string message)
{
Console.WriteLine(DateTime.Now.ToString("M/d HH:mm") + " " + destination + " > " + message);
bot.SendTextMessageAsync(destination, message, parseMode: ParseMode.Html);
}
public override void SendLocation(string destination, float latitude, float longitude)
{
bot.SendLocationAsync(destination, latitude, longitude);
}
public override void SendMarkdownMessage(string destination, string message)
{
Console.WriteLine(DateTime.Now.ToString("M/d HH:mm") + " " + destination + " > " + message);
bot.SendTextMessageAsync(destination, message, parseMode: ParseMode.Markdown);
}
public override void SendPlainTextMessage(string destination, string message)
{
Console.WriteLine(DateTime.Now.ToString("M/d HH:mm") + " " + destination + " > " + message);
bot.SendTextMessageAsync(destination, message);
}
public override void SendStatusTyping(string destination)
{
bot.SendChatActionAsync(destination, ChatAction.Typing);
}
public override void SendStatusUploadingFile(string destination)
{
bot.SendChatActionAsync(destination, ChatAction.UploadDocument);
}
public override void SendStatusUploadingPhoto(string destination)
{
bot.SendChatActionAsync(destination, ChatAction.UploadPhoto);
}
}
}