コード例 #1
0
ファイル: Program.cs プロジェクト: Fel0ny/TeleSharp
        public static void DownloadFileFromMessage(Message message, string savePath)
        {
            // Make storage path
            savePath = Path.Combine(savePath, "Storage");
            savePath = Path.Combine(savePath, message.From.Username ?? message.From.Id.ToString());
            if (!Directory.Exists(savePath))
                Directory.CreateDirectory(savePath);

            FileDownloadResult fileInfo = null;
            if (message.Document != null)
                fileInfo = Bot.DownloadFileById(message.Document.FileId, savePath);

            // Download video if exists
            if (message.Video != null)
                fileInfo = Bot.DownloadFileById(message.Video.FileId, savePath);

            // Download audio if exists
            if (message.Voice != null)
                fileInfo = Bot.DownloadFileById(message.Voice.FileId, savePath);

            // Download photo if exists
            if (message.Photo != null)
                foreach (PhotoSize photoSize in message.Photo)
                    fileInfo = Bot.DownloadFileById(photoSize.FileId, savePath);

            // Download sticker if exists
            if (message.Sticker != null)
                fileInfo = Bot.DownloadFileById(message.Sticker.FileId, savePath);
            //
            if (fileInfo != null)
                Console.WriteLine($"File : {fileInfo.FilePath} Size : {fileInfo.FileSize} was downloaded successfully");
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Fel0ny/TeleSharp
 private static void GetLocationContactFromMessage(Message message, MessageSender sender)
 {
     if (message.Location != null)
     {
         Console.WriteLine($"Location :({message.Location.Latitude},{message.Location.Longitude})");
         Bot.SendMessage(new SendMessageParams
         {
             ChatId = sender.Id.ToString(),
             Text = $"You Send location",
             ReplyToMessage = message
         });
     }
     else if (message.Contact != null)
     {
         Console.WriteLine($"Contact :({message.Contact.FirstName},{message.Contact.LastName},{message.Contact.PhoneNumber})");
         Bot.SendMessage(new SendMessageParams
         {
             ChatId = sender.Id.ToString(),
             Text = $"You Send Contact",
             ReplyToMessage = message
         });
     }
 }
コード例 #3
0
ファイル: TeleSharp.cs プロジェクト: Fel0ny/TeleSharp
        /// <summary>
        /// Forward a message from one chat to another.
        /// </summary>
        /// <param name="message">Message to forwar.</param>
        /// <param name="sender">User/group to send to</param>
        /// <returns>Message that was forwarded</returns>
        public Message ForwardMessage(Message message, MessageSender sender)
        {
            if (message == null)
                throw new ArgumentNullException(nameof(message));

            if (sender == null)
                throw new ArgumentNullException(nameof(sender));

            var request = Utils.GenerateRestRequest(Resources.Method_ForwardMessage, Method.POST,
                new Dictionary<string, string>
                {
                    {Resources.HttpContentType, Resources.HttpMultiPartFormData}
                }
                , new Dictionary<string, object>
                {
                    {Resources.Param_ChatId, sender.Id},
                    {Resources.Param_FromChatId, message.Chat?.Id ?? message.From.Id},
                    {Resources.Param_MessageId, message.MessageId}
                });

            return _botClient.Execute<Message>(request).Data;
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: Fel0ny/TeleSharp
        /// <summary>
        /// Read received messages of bot in infinity loop
        /// </summary>
        private static void OnMessage(Message message)
        {
            // Get mesage sender information
            MessageSender sender = (MessageSender)message.Chat ?? message.From;

            Console.WriteLine(message.Text ?? "");
            // If user joined to bot say welcome
            if ((!string.IsNullOrEmpty(message.Text)) && (message.Text == "/start"))
            {
                string welcomeMessage =
                    $"Welcome {message.From.Username} !{Environment.NewLine}My name is {Bot.Me.Username}{Environment.NewLine}I made using TeleBot : http://www.github.com/Fel0ny/TeleSharp";

                Bot.SendMessage(new SendMessageParams
                {
                    ChatId = sender.Id.ToString(),
                    Text = welcomeMessage
                });
                return;
            }

            string baseStoragePath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

            // If any file exists in message download it
            DownloadFileFromMessage(message, baseStoragePath);
            // If Send Location or Contact
            GetLocationContactFromMessage(message, sender);

            if (string.IsNullOrEmpty(message.Text) || string.IsNullOrEmpty(baseStoragePath))
                return;

            try
            {
                string sampleData = Path.Combine(baseStoragePath, "SampleData");

                if (!string.IsNullOrEmpty(message.Text))
                    switch (message.Text.ToLower())
                    {
                        case "time":
                            {
                                Bot.SendMessage(new SendMessageParams
                                {
                                    ChatId = sender.Id.ToString(),
                                    Text = DateTime.Now.ToLongDateString()
                                });
                                break;
                            }

                        case "location":
                            {
                                Bot.SendLocation(sender, "50.69421", "3.17456");
                                break;
                            }

                        case "sticker":
                            {
                                Bot.SendSticker(sender, System.IO.File.ReadAllBytes(Path.Combine(sampleData, "sticker.png")));
                                break;
                            }

                        case "photo":
                            {
                                string photoFilePath = Path.Combine(sampleData, "sticker.png");

                                Bot.SetCurrentAction(sender, ChatAction.UploadPhoto);
                                Bot.SendPhoto(sender, System.IO.File.ReadAllBytes(photoFilePath),
                                    Path.GetFileName(photoFilePath), "This is sample photo");
                                break;
                            }

                        case "video":
                            {
                                string videoFilePath = Path.Combine(sampleData, "video.mp4");

                                Bot.SetCurrentAction(sender, ChatAction.UploadVideo);
                                Bot.SendVideo(sender, System.IO.File.ReadAllBytes(videoFilePath),
                                    Path.GetFileName(videoFilePath), "This is sample video");
                                break;
                            }

                        case "audio":
                            {
                                string audioFilePath = Path.Combine(sampleData, "audio.mp3");

                                Bot.SetCurrentAction(sender, ChatAction.UploadAudio);
                                Bot.SendAudio(sender, System.IO.File.ReadAllBytes(audioFilePath),
                                    Path.GetFileName(audioFilePath));
                                break;
                            }

                        case "document":
                            {
                                string documentFilePath = Path.Combine(sampleData, "Document.txt");

                                Bot.SetCurrentAction(sender, ChatAction.UploadDocument);
                                Bot.SendDocument(sender, System.IO.File.ReadAllBytes(documentFilePath),
                                    Path.GetFileName(documentFilePath));
                                break;
                            }

                        case "keyboard":
                            {
                                Bot.SendMessage(new SendMessageParams
                                {
                                    ChatId = sender.Id.ToString(),
                                    Text = "This is sample keyboard :",
                                    CustomKeyboard = new ReplyKeyboardMarkup
                                    {
                                        Keyboard = new List<List<KeyboardButton>>
                                {
                                    new List<KeyboardButton>
                                    {
                                        new KeyboardButton { Text="send location",RequestContact=false,RequestLocation=true },
                                        new KeyboardButton {Text="contact",RequestContact=true,RequestLocation=false },
                                        new KeyboardButton {Text="cancel",RequestContact=false,RequestLocation=false }
                                    }
                                }
                                    },
                                    ReplyToMessage = message
                                });

                                break;
                            }
                        case "cancel":
                            {
                                Bot.SendMessage(new SendMessageParams
                                {
                                    ChatId = sender.Id.ToString(),
                                    Text = $"You choose keyboard command : {message.Text}",
                                });
                                break;
                            }

                        default:
                            {
                                Bot.SendMessage(new SendMessageParams
                                {
                                    ChatId = sender.Id.ToString(),
                                    Text = "Unknown command !",
                                });

                                break;
                            }
                    }
            }
            catch (Exception ex)
            {

            }
        }