Ejemplo n.º 1
0
        public async Task <bool> Execute()
        {
            string botToken = _configuration.GetValue <string>("Bot:Token");

            Telegram.Bot.TelegramBotClient botClient = new Telegram.Bot.TelegramBotClient(botToken);
            BotSettingModel setting = LoadSetting();

            Telegram.Bot.Types.Update[] updates = await botClient.GetUpdatesAsync(setting.LastChatID + 1);

            string RegisterKey      = _configuration.GetValue <string>("Bot:RegisterKey");
            string DeleteAccountKey = _configuration.GetValue <string>("Bot:DeleteAccountKey");
            string GetResponseKey   = _configuration.GetValue <string>("Bot:GetResponseKey");

            foreach (Telegram.Bot.Types.Update update in updates)
            {
                string chatText = string.Empty;
                chatText = update?.Message?.Text ?? string.Empty;
                long chatId = update.Message.Chat.Id;

                if (chatText == RegisterKey)
                {
                    if (!setting.Receivers.Any(x => x == chatId.ToString()))
                    {
                        setting.Receivers.Add(chatId.ToString());
                        await botClient.SendTextMessageAsync(chatId, "به دریافت کنندگان اضافه شدی", replyMarkup : getKeyboard());
                    }
                    else
                    {
                        await botClient.SendTextMessageAsync(chatId, "قبلا اضافه شدی", replyMarkup : getKeyboard());
                    }
                }
                else if (chatText == DeleteAccountKey)
                {
                    if (setting.Receivers.Any(x => x == chatId.ToString()))
                    {
                        setting.Receivers.Remove(chatId.ToString());
                        await botClient.SendTextMessageAsync(chatId, "از دریافت کنندگان حذف شدی", replyMarkup : getKeyboard());
                    }
                    else
                    {
                        await botClient.SendTextMessageAsync(chatId, "توی لیست نیستی", replyMarkup : getKeyboard());
                    }
                }
                else if (chatText == "/start")
                {
                    await botClient.SendTextMessageAsync(chatId, "سلام، چیکار کنم برات ؟!", replyMarkup : getKeyboard());
                }
                else if (chatText == GetResponseKey)
                {
                    await _restApi.GetResponse(chatId);
                }
                else
                {
                    await botClient.SendTextMessageAsync(chatId, "نمیفهمم چی میگی", replyMarkup : getKeyboard());
                }
                setting.LastChatID = update.Id;
            }
            SaveSetting(setting);
            return(true);
        }
Ejemplo n.º 2
0
 public BotSettingModel LoadSetting()
 {
     if (!File.Exists(configFilePath))
     {
         SaveSetting(new BotSettingModel()
         {
             LastChatID = 0,
             Receivers  = new List <string>()
             {
             }
         });
     }
     using (FileStream settingFile = File.OpenRead(configFilePath))
     {
         StreamReader    logReader  = new StreamReader(settingFile);
         string          strSetting = logReader.ReadToEnd();
         BotSettingModel setting    = new BotSettingModel()
         {
             LastChatID = 0,
             Receivers  = new List <string>()
             {
             }
         };
         try
         {
             setting = JsonConvert.DeserializeObject <BotSettingModel>(strSetting);
         }
         catch { }
         logReader.Dispose();
         return(setting);
     }
 }
Ejemplo n.º 3
0
 private void SaveSetting(BotSettingModel model)
 {
     using (FileStream settingFile = File.Create(configFilePath))
     {
         StreamWriter logWriter = new StreamWriter(settingFile);
         logWriter.WriteLine(JsonConvert.SerializeObject(model));
         logWriter.Dispose();
     }
 }
Ejemplo n.º 4
0
        public async Task <bool> Execute()
        {
            BotResponseModel jsonResult = await Calculate();

            string strFormattedResult = JsonConvert.SerializeObject(jsonResult, Formatting.Indented);
            string botToken           = _configuration.GetValue <string>("Bot:Token");

            Telegram.Bot.TelegramBotClient botClient = new Telegram.Bot.TelegramBotClient(botToken);
            BotSettingModel setting = _bot.LoadSetting();

            foreach (string receiver in setting.Receivers)
            {
                Telegram.Bot.Types.ChatId chatId = new Telegram.Bot.Types.ChatId(receiver);
                await botClient.SendTextMessageAsync(chatId, strFormattedResult);
            }
            return(true);
        }