Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.cs]

# CS8602: Dereference of a possibly null reference.
dotnet_diagnostic.CS8602.severity = silent
12 changes: 7 additions & 5 deletions ChatBot-For-Support-Telegram/ChatBotForSupport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@
<UseWindowsForms>False</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<ApplicationIcon>unnamed.ico</ApplicationIcon>
<AssemblyVersion>1.0.2</AssemblyVersion>
<FileVersion>$(AssemblyVersion)</FileVersion>
<Version>$(AssemblyVersion)</Version>
</PropertyGroup>

<ItemGroup>
<Content Include="unnamed.ico" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="Telegram.Bot" Version="17.0.0" />
<None Include="..\.editorconfig" Link=".editorconfig" />
</ItemGroup>

<ItemGroup>
<Reference Include="BeOpen.Common.Cache">
<HintPath>..\packages\netstandard2.0\BeOpen.Common.Cache.dll</HintPath>
</Reference>
<PackageReference Include="Common.Cache" Version="0.1.1" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="Telegram.Bot" Version="17.0.0" />
</ItemGroup>

</Project>
26 changes: 19 additions & 7 deletions ChatBot-For-Support-Telegram/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BeOpen.Common.Cache;
using Common.Cache;
using ChatBotForSupport.DTO;
using ChatBotForSupport.UpdateHandlers;
using System.ComponentModel;
Expand All @@ -10,28 +10,40 @@ namespace ChatBotForSupport
{
internal static class Program
{
//AdminsDictionary Key - id админа в Телеграмм | Value - null
public static readonly SerializableCache<long, string> AdminsDictionary = new SerializableCache<long, string>($"{Directory.GetCurrentDirectory()}\\adminsDictionary.json");
//AdminsDictionary Key - предназначение учетной записи | Value - id админа в Телеграмм
public static readonly SerializableCache<string, long> AdminsDictionary = new SerializableCache<string, long>($"{Directory.GetCurrentDirectory()}\\adminsDictionary.json");
//MessageDictionary Key - id сообщения у админа об обращение | Value - от кого поступило обращение
public static readonly SerializableCache<long, MessageDictionary> MessageDictionary = new SerializableCache<long, MessageDictionary>($"{Directory.GetCurrentDirectory()}\\messageDictionary.json");
//AnswerModeDictionary Key - Для кого включен режим ответа | Value - на какое сообщение включен режим
public static readonly SerializableCache<long, AnswerModeDictionary> AnswerModeDictionary = new SerializableCache<long, AnswerModeDictionary>($"{Directory.GetCurrentDirectory()}\\answerModeDictionary.json");
//BotPublicKeyDictionary Key - Название ключа | Value - Ключ Апи бота
public static readonly SerializableCache<string, string> BotPublicKeyDictionary = new SerializableCache<string, string>($"{Directory.GetCurrentDirectory()}\\botPublicKeyDictionary.json");
//BotMessageDictionary Key - Тип сообщения | Value - Текст сообщения
public static readonly SerializableCache<string, string> BotMessageDictionary = new SerializableCache<string, string>($"{Directory.GetCurrentDirectory()}\\botMessageDictionary.json");

public static BackgroundWorker _bw;
private static readonly string _publicKey = BotPublicKeyDictionary.KeyValuePair.FirstOrDefault().Value;//Configuration.Default.publicKey;
private static int _offset = 0;
public static bool StopProgram = false;
public static long SuperAdminId;
public static long DebugChatId;

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (!AdminsDictionary.Contains(441224506))
AdminsDictionary.AddOrUpdate(441224506, "");
if (AdminsDictionary.TryGetById("superAdmin", out long adminId))
SuperAdminId = adminId;
if (AdminsDictionary.TryGetById("debugChat", out long debugChatId))
DebugChatId = debugChatId;
if (BotMessageDictionary.Count() == 0)
{
BotMessageDictionary.AddOrUpdate("start", "");
BotMessageDictionary.AddOrUpdate("help", "");
}

var inner = Task.Factory.StartNew(() =>
{
Bot();
Expand Down Expand Up @@ -79,7 +91,7 @@ static async void _bw_DoWork(object sender, DoWorkEventArgs e)
}
catch (Exception ex)
{
await Bot.SendTextMessageAsync("441224506", "ERROR WHILE GETTIGN UPDATES - " + ex);
await Bot.SendTextMessageAsync(DebugChatId, "ERROR WHILE GETTIGN UPDATES - " + ex);
}
foreach (var update in updates)
{
Expand Down Expand Up @@ -133,7 +145,7 @@ static async void _bw_DoWork(object sender, DoWorkEventArgs e)
catch (Exception ex)
{
_offset = 0;
await Bot.SendTextMessageAsync("441224506", ex.Message + "-" + ex.StackTrace);
await Bot.SendTextMessageAsync(DebugChatId, ex.Message + "-" + ex.StackTrace);
_bw.RunWorkerAsync(_publicKey);
}
}
Expand Down
24 changes: 15 additions & 9 deletions ChatBot-For-Support-Telegram/UpdateHandlers/CallbackHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,37 @@ public async static Task CallbackHandlerAsync(Update update, TelegramBotClient b
switch (callbackQuery.Data)
{
case "Ответить":
var keyboard = new InlineKeyboardMarkup(new[]
{
if (!Program.AnswerModeDictionary.Contains(update.CallbackQuery.From.Id))
{
var keyboard = new InlineKeyboardMarkup(new[]
{
new []
{
InlineKeyboardButton.WithCallbackData("Отменить ответ.")
}
});
var responseNotification = await bot.SendTextMessageAsync(update.CallbackQuery.From.Id, $"Введи сообщение для отправки пользователю🔽", replyMarkup: keyboard, parseMode: ParseMode.Markdown);
Program.AnswerModeDictionary.AddOrUpdate(update.CallbackQuery.From.Id, new AnswerModeDictionary() { InlineMessageId = callbackQuery.Message.MessageId, ResponseNotificationId = responseNotification.MessageId });
});
var responseNotification = await bot.SendTextMessageAsync(update.CallbackQuery.From.Id, $"Введи сообщение для отправки пользователю🔽", replyMarkup: keyboard, parseMode: ParseMode.Markdown);
Program.AnswerModeDictionary.AddOrUpdate(update.CallbackQuery.From.Id, new AnswerModeDictionary() { InlineMessageId = callbackQuery.Message.MessageId, ResponseNotificationId = responseNotification.MessageId });
}
break;
case "Add Admin":
await bot.SendTextMessageAsync(update.CallbackQuery.From.Id, $"Comming soon... 🤗");
await bot.DeleteMessageAsync(update.CallbackQuery.From.Id, callbackQuery.Message.MessageId);
break;
case "Отменить ответ":
var modeData = Program.AnswerModeDictionary.GetById(update.CallbackQuery.From.Id);
Program.AnswerModeDictionary.Delete(update.CallbackQuery.From.Id);
await bot.DeleteMessageAsync(update.CallbackQuery.From.Id, modeData.ResponseNotificationId);
if (Program.AnswerModeDictionary.Contains(update.CallbackQuery.From.Id))
{
var modeData = Program.AnswerModeDictionary.GetById(update.CallbackQuery.From.Id);
Program.AnswerModeDictionary.Delete(update.CallbackQuery.From.Id);
await bot.DeleteMessageAsync(update.CallbackQuery.From.Id, modeData.ResponseNotificationId);
}
break;
case "Restart":
await bot.SendTextMessageAsync(update.CallbackQuery.From.Id, $"Бот был перезапущен при помощи команды /restart , данную команду запустил @{update?.CallbackQuery?.From?.Username} - {update?.CallbackQuery?.From?.FirstName}");
throw new Exception($"Бот был перезапущен при помощи команды /restart , данную команду запустил @{update?.CallbackQuery?.From?.Username} - {update?.CallbackQuery?.From?.FirstName}");
case "Stop":
foreach (var admin in Program.AdminsDictionary.KeyValuePair)
await bot.SendTextMessageAsync(admin.Key, $"Bot stopped by - @{update?.CallbackQuery?.From?.Username} - {update?.CallbackQuery?.From?.FirstName}");
await bot.SendTextMessageAsync(admin.Value, $"Bot stopped by - @{update?.CallbackQuery?.From?.Username} - {update?.CallbackQuery?.From?.FirstName}");
Process.GetCurrentProcess().Kill();
break;
case "Remove Admin":
Expand Down
Loading