public void Start() { logger.Info("Starting bot"); TelegramRequest Tr = new TelegramRequest(MainSettings.Default.Token, MainSettings.Default.API_URL); Tr.MessageText += Tr_MessageText; Tr.MessageSticker += Tr_MessageSticker; Tr.MessagePhoto += Tr_MessagePhoto; Tr.MessageVideo += Tr_MessageVideo; Tr.MessageDocument += Tr_MessageDocument; Tr.MessageLocation += Tr_MessageLocation; Tr.MessageContact += Tr_MessageContact; Tr.MessageVoice += Tr_MessageVoice; DBAPI Db = new DBAPI(MainSettings.Default.DB_DataSource, MainSettings.Default.DB_UserID, MainSettings.Default.DB_Password, MainSettings.Default.DB_InitialCatalog); Db.RegTransactionEvent += RegisterTransaction; logger.Info("Connection to DB"); Db.Connect(); logger.Info("Start pooling telegram API"); Tr.GetUpdates(); //Console.ReadLine(); }
private void Tr_MessageText(object sendr, MessageText e) { logger.Debug($"New message: message_id:{e.message_id} user_id:{e.from.id} chat_id:{e.chat.id} username:{e.from.username} name: {e.from.first_name} {e.from.last_name} date: {e.date} message:'{e.text}'"); Method m = new Method(MainSettings.Default.Token, MainSettings.Default.API_URL); DBAPI d = new DBAPI(MainSettings.Default.DB_DataSource, MainSettings.Default.DB_UserID, MainSettings.Default.DB_Password, MainSettings.Default.DB_InitialCatalog); d.RegTransactionEvent += RegisterTransaction; d.Connect(); string answer; string message = e.text.Replace("@DeeBeeTeeBot", ""); message = message.Replace("@DeeBeeTeeTestBot", ""); string command_params = ""; int space = message.IndexOf(" "); string command; if (space == -1) { command = message; } else { command = message.Substring(0, space); command_params = message.Substring(space, message.Length - space); } logger.Info("Получение команды " + command); logger.Info("Проверка изоляции чата" + command); bool _isolated = d.CheckIsolated(e.chat.id); string _username = e.from.username; if (_isolated) { logger.Debug("Чат изолирован!"); _username = e.chat.id.ToString() + "___" + e.from.username; logger.Debug("New user name" + _username); message = message.Replace("@", "@" + e.chat.id.ToString() + "___"); logger.Debug("New message" + message); command_params = command_params.Replace("@", "@" + e.chat.id.ToString() + "___"); logger.Debug("New command_params" + command_params); } if (command.StartsWith("/isolate_")) { answer = d.Command_isolate(command, e.chat.id, command_params); } else if (command.StartsWith("/tid_")) { answer = d.Command_tid(command, command_params); } else { switch (command) { case "/balance": answer = d.Command_balance(_username, command_params); break; case "/b": answer = d.Command_balance(_username, command_params); break; case "/details": answer = d.Command_details(_username, command_params); break; case "/d": answer = d.Command_details(_username, command_params); break; case "/hello": answer = d.Command_hello(); break; case "/help": answer = d.Command_help(command_params); break; case "/start": answer = d.Command_start(_username, e.from.id); break; case "/t": answer = d.Command_transaction(_username, message); break; case "/transaction": answer = d.Command_transaction(_username, message); break; case "/j": answer = d.Command_journal(_username, command_params); break; case "/journal": answer = d.Command_journal(_username, command_params); break; case "/isolate": answer = d.Command_isolate_request(_username, e.chat.id, command_params); break; case "/private": answer = d.Command_private(_username, command_params); break; //Admins command case "/a_balance": answer = d.Command_a_balance(message); break; case "/a_details": answer = d.Command_a_details(message); break; case "/terminate9616": answer = "Emergency shutdown"; TelegramRequest TelR = sendr as TelegramRequest; TelR.ForceTerminate = true; break; default: answer = $"Извините я команду '{command}' не поддерживаю. Поддерживаемые команды можно посмотреть /help"; break; } } if (_isolated) { answer = answer.Replace("@" + e.chat.id.ToString() + "___", "@"); } m.SendMessage(answer, e.chat.id); d.UpdateChat(e.chat.id, e.chat.type, e.from.id, e.from.username, e.chat.title); // Сообщение мне! m.SendMessage("New " + e.chat.type + " message from @" + e.from.username + ": " + message, 2730927); d.Disconnect(); }