// If your activity returns a value, derive from CodeActivity<TResult> // and return the value from the Execute method. protected override void Execute(CodeActivityContext context) { // Obtain the runtime value of the Text input argument //string text = context.GetValue(this.Text); TelegramProp telegramDetails = (TelegramProp)context.DataContext.GetProperties()["telegramDetails"].GetValue(context.DataContext); var botToken = telegramDetails.authToken; var messageText = Message_Text.Get(context); if (messageText == null) { throw new ArgumentException("Message text input is missing"); } var chatID_str = Chat_Name.Get(context); if (chatID_str == null) { throw new ArgumentException("Channel name input is missing"); } var botClient = new TelegramBotClient(botToken); try { Message message = botClient.SendTextMessageAsync(chatID_str, messageText).GetAwaiter().GetResult(); } catch (Telegram.Bot.Exceptions.ChatNotFoundException) { throw new Exception("Input Channel " + chatID_str + " does not exist"); } catch (Telegram.Bot.Exceptions.ChatNotInitiatedException) { throw new Exception("Input Channel " + chatID_str + " has not yet initiated a chat with bot yet"); } catch (Telegram.Bot.Exceptions.ApiRequestException) { throw new Exception("Input Bot Token - Represents an API error"); } catch (System.Exception ex) { throw new Exception("Telegram Send Message Failed, Exception:" + ex.Message); } }
// If your activity returns a value, derive from CodeActivity<TResult> // and return the value from the Execute method. protected override void Execute(CodeActivityContext context) { // Obtain the runtime value of the Text input argument //string text = context.GetValue(this.Text); TelegramProp telegramDetails = (TelegramProp)context.DataContext.GetProperties()["telegramDetails"].GetValue(context.DataContext); var botToken = telegramDetails.authToken; var photopath = Image_Path.Get(context); if (photopath == null) { throw new ArgumentException("Photo Path missing"); } var chatID_str = Chat_Name.Get(context); if (chatID_str == null) { throw new ArgumentException("Channel missing"); } var image_text = Image_Text.Get(context); if (image_text == null) { image_text = ""; } var botClient = new TelegramBotClient(botToken); string file = photopath; var fileName = file.Split(Path.DirectorySeparatorChar).Last(); //using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read)) //{ // Message Photo = botClient.SendPhotoAsync(chatID, fileStream, image_text).GetAwaiter().GetResult(); //} try { using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read)) { Message Photo = botClient.SendPhotoAsync(chatID_str, fileStream, image_text).GetAwaiter().GetResult(); } } catch (Telegram.Bot.Exceptions.ChatNotFoundException) { throw new Exception("Input Channel " + chatID_str + "does not exist"); } catch (Telegram.Bot.Exceptions.ChatNotInitiatedException) { throw new Exception("Input Channel " + chatID_str + " has not yet initiated a chat with bot yet"); } catch (Telegram.Bot.Exceptions.ApiRequestException) { throw new Exception("Input Bot Token - Represents an API error"); } catch (System.Exception ex) { throw new Exception("Telegram Send Image Failed, Exception:" + ex.Message); } }