コード例 #1
0
        // 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 botClient = new TelegramBotClient(botToken);


            Int32         offset     = 0;
            Int32         offset_new = 0;
            List <string> Msg_arr    = new List <string> ( );

            var updates = botClient.GetUpdatesAsync(0).GetAwaiter().GetResult();

            foreach (var update in updates)
            {
                if (update.Message.Type == MessageType.Text)
                {
                    //Add messages from Telegram into Message List
                    Msg_arr.Add(update.Message.Text);
                }
                offset_new = update.Id + 1;
            }

            Str_arr.Set(context, Msg_arr);

            var update_new = botClient.GetUpdatesAsync(offset = offset_new).GetAwaiter().GetResult();
        }
コード例 #2
0
ファイル: SendMessage.cs プロジェクト: dineshary/Telegram_Bot
        // 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 = MessageText.Get(context);

            if (messageText == null)
            {
                throw new ArgumentException("MesageText missing");
            }

            var chatID = ChatID.Get(context);

            var chatID_str = Convert.ToString(chatID);

            if (chatID_str == null)
            {
                throw new ArgumentException("Chat-ID missing");
            }


            var botClient = new TelegramBotClient(botToken);

            Message message = botClient.SendTextMessageAsync(chatID, messageText).GetAwaiter().GetResult();
        }
コード例 #3
0
ファイル: SendMessage.cs プロジェクト: pkumarat/Telegram_Bot
        // 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 = Chat_ID.Get(context);

            var chatID_str = Convert.ToString(chatID);

            if (chatID_str == null)
            {
                throw new ArgumentException("Chat-ID input is missing");
            }


            var botClient = new TelegramBotClient(botToken);

            try
            {
                Message message = botClient.SendTextMessageAsync(chatID, messageText).GetAwaiter().GetResult();
            }

            catch (Telegram.Bot.Exceptions.ChatNotFoundException)
            {
                throw new Exception("Input Chat_ID " + chatID_str + " does not exist");
            }
            catch (Telegram.Bot.Exceptions.ChatNotInitiatedException)
            {
                throw new Exception("Input Chat_ID " + 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);
            }
        }
コード例 #4
0
        protected override void Execute(NativeActivityContext context)
        {
            if (BotToken.Get(context) == null)
            {
                throw new ArgumentException("BotToken");
            }

            telegramDetails = new TelegramProp(new NetworkCredential(String.Empty, BotToken.Get(context)).Password);

            if (Body != null)
            {
                //scheduling the execution of the child activities
                // and passing the value of the delegate argument
                context.ScheduleAction <TelegramProp>(Body, telegramDetails, OnCompleted, OnFaulted);
            }
        }
コード例 #5
0
        // 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 botClient = new TelegramBotClient(botToken);


            Int32         offset     = 0;
            Int32         offset_new = 0;
            List <string> Msg_arr    = new List <string> ( );

            var updates = botClient.GetUpdatesAsync(0).GetAwaiter().GetResult();

            foreach (var update in updates)
            {
                if (update.Message.Type == MessageType.Text)
                {
                    //Add messages from Telegram into Message List
                    Msg_arr.Add(update.Message.Text);
                }
                offset_new = update.Id + 1;
            }

            Str_arr.Set(context, Msg_arr);

            //var update_new = botClient.GetUpdatesAsync(offset = offset_new).GetAwaiter().GetResult();
            try
            {
                var update_new = botClient.GetUpdatesAsync(offset = offset_new).GetAwaiter().GetResult();
            }
            catch (Telegram.Bot.Exceptions.ApiRequestException)
            {
                throw new Exception("Input Bot Token - Represents an API error");
            }
            catch (System.Exception ex)
            {
                throw new Exception("Telegram Receive Message Failed, Exception:" + ex.Message);
            }
        }
コード例 #6
0
        // 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 = PhotoPath.Get(context);

            if (photopath == null)
            {
                throw new ArgumentException("Photo-Path missing");
            }

            var chatID = ChatID.Get(context);

            var chatID_str = Convert.ToString(chatID);

            if (chatID_str == null)
            {
                throw new ArgumentException("Chat-ID missing");
            }

            var image_text = Image_Text.Get(context);

            if (image_text == null)
            {
                image_text = "Image sent from Bot";
            }

            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();
            }
        }
コード例 #7
0
ファイル: SendPhoto.cs プロジェクト: pkumarat/Telegram_Bot
        // 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 = Chat_ID.Get(context);

            var chatID_str = Convert.ToString(chatID);

            if (chatID_str == null)
            {
                throw new ArgumentException("Chat-ID 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, fileStream, image_text).GetAwaiter().GetResult();
                }
            }
            catch (Telegram.Bot.Exceptions.ChatNotFoundException)
            {
                throw new Exception("Input Chat_ID " + chatID_str + "does not exist");
            }
            catch (Telegram.Bot.Exceptions.ChatNotInitiatedException)
            {
                throw new Exception("Input Chat_ID " + 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);
            }
        }