-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.cs
366 lines (319 loc) · 14.1 KB
/
Code.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;
// Tor
using System.Net;
using System.Net.Http;
using Microsoft.Extensions.Configuration;
using System.Reflection.Metadata;
namespace BotTestTelegram
{
class Program
{
// Global variables
static int blockLevel = 0;
static bool messDeleted = false;
static string[] badWords = new string[] { "bad word", "badword" };
static string[] veryBadWords = new string[] { "very bad word", "verybadword" };
static int pollMessageId = 0;
static async Task Main(string[] args)
{
// Tor Proxy Example - Use only if you want active the proxy
// Note that Tor has to be active at all times for the bot to work.
// Open the torcc file with a text editor (Found in Tor Browser\Browser\TorBrowser\Data\Tor)
// Add the following lines (this is an example, you can modify the lines):
// EntryNodes { NL}
// ExitNodes { NL}
// StrictNodes 1
// SocksPort 127.0.0.1:9050
/* WebProxy proxy = new("socks5://127.0.0.1:9050"); //This line tells tor to listen on port 9050 for any socks connections. You can change the port to anything you want (9050 is just the default), only make sure to use the same port in your code.
HttpClient httpClient = new(
new SocketsHttpHandler { Proxy = proxy, UseProxy = true }
);
var botClient = new TelegramBotClient("TOKEN", httpClient);*/
// Bot Token without Tor Proxy
var botClient = new TelegramBotClient("TOKEN");
// Cancellation token
using var cts = new CancellationTokenSource();
// Get bot information
var me = await botClient.GetMe();
// Write a welcome message to the console
Console.WriteLine($"\nHello! I'm {me.Username} and I'm your Bot!");
// Receiver optionsabilire la connessione. Rifiuto persistente del computer di destinazione. (127.0.0.1:9050)
var receiverOptions = new ReceiverOptions
{
AllowedUpdates = { } // Receive all update types
};
// Start receiving updates using DefaultUpdateHandler
botClient.StartReceiving(
new DefaultUpdateHandler(HandleUpdateAsync, HandleErrorAsync),
receiverOptions,
cts.Token
);
// Keep the application running until a key is pressed
Console.ReadKey();
// Send cancellation request to stop the bot
cts.Cancel();
}
// Method to handle updates
static async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
// Check if the update contains a message
if (update.Message == null)
return;
// Check if the message is of type text
if (update.Message.Type != MessageType.Text)
return;
// Set variables
var chatId = update.Message.Chat.Id;
var messageText = update.Message.Text.ToLower();
var messageId = update.Message.MessageId;
var firstName = update.Message.From?.FirstName ?? "";
var lastName = update.Message.From?.LastName ?? "";
var userId = update.Message.From?.Id ?? 0;
var date = update.Message.Date;
// Display date and time in console when a message is received
Console.WriteLine($"\nDate message --> {date:yyyy/MM/dd - HH:mm:ss}");
// Display the message, chat ID, and user info in console
Console.WriteLine($"Received a '{messageText}' message in chat {chatId} from user:\n{firstName} - {lastName} - {userId}");
// Check if the user is a member of the required channels
try
{
var getChatMember1 = await botClient.GetChatMember("@tryerthyhdtyhd", userId);
var getChatMember2 = await botClient.GetChatMember("@tryerthyhdtyhd", userId);
// If the user is not a member, prompt them to join
if (getChatMember1.Status == ChatMemberStatus.Left || getChatMember2.Status == ChatMemberStatus.Left)
{
// Create buttons with the channel URLs to follow
InlineKeyboardMarkup inlineKeyboard = new InlineKeyboardMarkup(new[]
{
new []
{
InlineKeyboardButton.WithUrl(text: "Channel 1", url: "https://t.me/tryerthyhdtyhd"),
InlineKeyboardButton.WithUrl(text: "Channel 2", url: "https://t.me/tryerthyhdtyhd"),
},
});
await botClient.SendMessage(
chatId,
text: "Before using the bot, you must follow these channels",
replyMarkup: inlineKeyboard,
cancellationToken: cancellationToken);
return; // Exit early since user needs to follow channels
}
}
catch (ApiRequestException ex)
{
Console.WriteLine($"Error fetching chat member: {ex.Message}");
return; // Exit early if unable to fetch chat member
}
// Handle the /vulgarity command to change the block level
if (messageText == "/vulgarity")
{
switch (blockLevel)
{
case 0:
blockLevel = 1;
await botClient.SendMessage(
chatId,
text: "Vulgarity: \"Medium block\".");
return;
case 1:
blockLevel = 2;
await botClient.SendMessage(
chatId,
text: "Vulgarity: \"Hard block\".");
return;
case 2:
blockLevel = 0;
await botClient.SendMessage(
chatId,
text: "Vulgarity: \"Block disabled\".");
return;
}
}
// Vulgarity block - bad words
foreach (var badWord in badWords)
{
if (messageText.Contains(badWord) && blockLevel == 2 && !messDeleted)
{
messDeleted = true;
await botClient.DeleteMessage(chatId, messageId, cancellationToken);
await botClient.SendMessage(
chatId,
$"{firstName} {lastName}, you can't say that.");
}
}
// Vulgarity block - very bad words
foreach (var veryBadWord in veryBadWords)
{
if (messageText.Contains(veryBadWord) && (blockLevel == 1 || blockLevel == 2) && !messDeleted)
{
messDeleted = true;
await botClient.DeleteMessage(chatId, messageId, cancellationToken);
await botClient.SendMessage(
chatId,
$"{firstName} {lastName}, you can't say that.");
}
}
messDeleted = false;
// Respond to specific commands
if (messageText == "hello")
{
await botClient.SendMessage(
chatId,
$"Hello {firstName} {lastName}",
cancellationToken: cancellationToken);
}
//Sending sticker message
if (messageText == "sticker")
{
await botClient.SendSticker(chatId, "https://telegrambots.github.io/book/docs/sticker-dali.webp");
}
// Sending voice
if (messageText == "voice")
{
await using Stream stream = File.OpenRead("/path/to/voice-nfl_commentary.ogg");
await botClient.SendVoice(chatId, stream, duration: 36);
}
// Send a meme photo
if (messageText == "meme")
{
await botClient.SendPhoto(
chatId,
"https://i.redd.it/uhkj4abc96r61.jpg",
caption: "<b>MEME</b>",
parseMode: ParseMode.Html);
}
// Send audio file
if (messageText == "sound")
{
await botClient.SendAudio(
chatId,
"https://github.com/TelegramBots/book/raw/master/src/docs/audio-guitar.mp3");
}
// Send video
if (messageText == "video")
{
await botClient.SendVideo(
chatId,
"https://raw.githubusercontent.com/TelegramBots/book/master/src/docs/video-countdown.mp4",
thumbnail: "https://telegrambots.github.io/book/2/docs/thumb-clock.jpg",
supportsStreaming: true);
}
//Send a Video Note
if (messageText == "video note")
{
//Download the Sea Waves video to your disk for this example.
await using Stream stream = File.OpenRead("/path/to/video-waves.mp4");
await botClient.SendVideoNote(chatId, stream,
duration: 47, length: 360); // value of width/height
}
// Send album of photos
if (messageText == "album")
{
await botClient.SendMediaGroup(
chatId,
new IAlbumInputMedia[]
{
new InputMediaPhoto("https://cdn.pixabay.com/photo/2017/06/20/19/22/fuchs-2424369_640.jpg"),
new InputMediaPhoto("https://cdn.pixabay.com/photo/2017/04/11/21/34/giraffe-2222908_640.jpg"),
});
}
// Send document
if (messageText == "doc")
{
await botClient.SendDocument(
chatId,
"https://github.com/TelegramBots/book/raw/master/src/docs/photo-ara.jpg",
"<b>Ara bird</b>. <i>Source</i>: <a href=\"https://pixabay.com\">Pixabay</a>",
parseMode: ParseMode.Html);
}
// Send an animation (GIF)
if (messageText == "gif")
{
await botClient.SendAnimation(
chatId,
"https://raw.githubusercontent.com/TelegramBots/book/master/src/docs/video-waves.mp4",
"Waves");
}
// Create a poll
if (messageText == "poll")
{
var pollMessage = await botClient.SendPoll(
chatId,
"How are you?",
new InputPollOption[]
{
"Good!",
"I could be better.."
});
// Save the poll message ID
pollMessageId = pollMessage.MessageId;
Console.WriteLine($"\nPoll number: {pollMessageId}!");
}
// Close the poll
if (messageText == "close poll" && pollMessageId != 0)
{
await botClient.StopPoll(
chatId,
pollMessageId);
Console.WriteLine($"\nPoll number {pollMessageId} is closed!");
}
// Send a contact
if (messageText == "contact")
{
await botClient.SendContact(
chatId,
phoneNumber: "+1234567890",
firstName: "Han",
lastName: "Solo",
vcard: "BEGIN:VCARD\n" +
"VERSION:3.0\n" +
"N:Solo;Han\n" +
"ORG:Scruffy-looking nerf herder\n" +
"TEL;TYPE=voice,work,pref:+1234567890\n" +
"EMAIL:[email protected]\n" +
"END:VCARD");
}
// Send a venue
if (messageText == "roma location")
{
await botClient.SendVenue(
chatId,
latitude: 41.9027835f,
longitude: 12.4963655f,
title: "Rome",
address: "Rome, via Daqua 8, 08089");
}
// Send a location
if (messageText == "send me a location")
{
await botClient.SendLocation(
chatId: chatId,
latitude: 41.9027835f,
longitude: 12.4963655f);
}
}
// Method to handle errors
static Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
{
// Error message based on exception type
var errorMessage = exception switch
{
ApiRequestException apiRequestException
=> $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
_ => exception.ToString()
};
// Print error message to console
Console.WriteLine(errorMessage);
return Task.CompletedTask;
}
}
}