-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelegramModule.cs
More file actions
156 lines (133 loc) · 5.9 KB
/
TelegramModule.cs
File metadata and controls
156 lines (133 loc) · 5.9 KB
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
using CameraMicTelegram;
using System;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types;
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Polling;
namespace TelegramController
{
public class TelegramModule
{
private static TelegramBotClient botClient;
private static ChatId FromChatId { get; set; }
private static ChatId ToChatId { get; set; }
private ChatId MyId { get; set; }
private static Message MyMessage { get; set; }
private static string picName { get; set; }
private static string caption { get; set; }
public TelegramModule()
{
//TelegramModule bot = new TelegramModule();
botClient = new TelegramBotClient(Token.Get());
using CancellationTokenSource cts = new();
ReceiverOptions receiverOptions = new()
{
AllowedUpdates = Array.Empty<UpdateType>() // receive all update types except ChatMember related updates
};
botClient.StartReceiving(
updateHandler: HandleUpdateAsync,
pollingErrorHandler: HandlePollingErrorAsync,
receiverOptions: receiverOptions,
cancellationToken: cts.Token
);
var me = botClient.GetMeAsync();
Console.WriteLine($"Start listening for @{me}");
Console.ReadLine();
async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
// Only process Message updates: https://core.telegram.org/bots/api#message
if (update.Message is not { } message)
return;
// Only process text messages
if (message.Text is not { } messageText)
return;
//FromChatId = "asd";
FromChatId = message.Chat.Id;
long chatId = message.Chat.Id;
ToChatId = message.Chat.Id;
Console.WriteLine($"Received a '{messageText}' message in chat {FromChatId}.");
await TeloSendMessage("Seni bulacam olum" + FromChatId + "seni bulacam.", FromChatId);
// Echo received message text
switch (messageText)
{
case "Get":
await SendPic(ToChatId);
break;
case "Face":
await FaceSendPic(ToChatId);
break;
case "Watch":
await WatchAndGet(ToChatId);
break;
case "send":
await TeloSendMessage();
break;
default: break;
}
}
Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
{
var ErrorMessage = exception switch
{
ApiRequestException apiRequestException
=> $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
_ => exception.ToString()
};
Console.WriteLine(ErrorMessage);
return Task.CompletedTask;
}
}
public async Task<TelegramModule> SendPic(ChatId chatId)
{
picName = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
await CameraModule.TakeSavePic(picName);
await using Stream stream = System.IO.File.OpenRead(Environment.CurrentDirectory + "/assets/" + picName + ".jpg");
Message message = await botClient.SendPhotoAsync(
chatId: chatId,
photo: InputFile.FromStream(stream),
caption: "Taken from comfuture",
parseMode: ParseMode.Html,
cancellationToken: default(CancellationToken));
return this;
}
public async Task<TelegramModule> FaceSendPic(ChatId chatId = default)
{
string picName = DateTime.Now.ToString("FACE_yyyy-MM-dd_HH-mm-ss");
await CameraModule.FaceAndSave(picName);
await using Stream stream = System.IO.File.OpenRead(Environment.CurrentDirectory + "/assets/" + picName + ".jpg");
Message message = await botClient.SendPhotoAsync(
chatId: ToChatId,
photo: InputFile.FromStream(stream),
caption: "Taken from comfuture",
parseMode: ParseMode.Html,
cancellationToken: default(CancellationToken));
return this;
}
public async Task<TelegramModule> WatchAndGet(ChatId chatId = default)
{
string picName = DateTime.Now.ToString("FACE_yyyy-MM-dd_HH-mm-ss");
await CameraModule.WatchAndSee(picName);
return this;
}
public async Task<TelegramModule> TeloSendPic(string picName = default)
{
await using Stream stream = System.IO.File.OpenRead(Environment.CurrentDirectory + "/assets/" + picName + ".jpg");
Message message = await botClient.SendPhotoAsync(
chatId: ToChatId,
photo: InputFile.FromStream(stream),
caption: "Taken from comfuture",
parseMode: ParseMode.Html,
cancellationToken: default(CancellationToken));
return this;
}
public async Task<TelegramModule> TeloSendMessage(string message = default, ChatId ToChatId = default)
{
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: ToChatId,
text: "Söyle ona sebastiyan:\n" + message,
cancellationToken: default(CancellationToken));
return this;
}
}
}