Пример #1
0
        private static void Main(string[] args)
        {
            Bot = new TeleSharp.TeleSharp("152529427:AAFOizfzWycWHnJoQghmRAbN5IlBInd-wSe8");
            Bot.SendMessage(new SendMessageParams
            {
                ChatId         = "39699831",
                Text           = "Test msg !",
                InlineKeyboard = new InlineKeyboardMarkup
                {
                    InlineKeyboard = new List <List <InlineKeyboardButton> >
                    {
                        new List <InlineKeyboardButton>
                        {
                            new InlineKeyboardButton {
                                Text = "CallbackData", CallbackData = "Ok", SwitchInlineQuery = string.Empty, SwitchInlineQueryCurrentChat = string.Empty, Url = string.Empty
                            },
                        },
                        new List <InlineKeyboardButton>
                        {
                            new InlineKeyboardButton {
                                Text = "SwitchInlineQueryCurrentChat", CallbackData = string.Empty, SwitchInlineQuery = string.Empty, SwitchInlineQueryCurrentChat = "OK", Url = string.Empty
                            },
                        },
                        new List <InlineKeyboardButton>
                        {
                            new InlineKeyboardButton {
                                Text = "Url", CallbackData = string.Empty, SwitchInlineQuery = string.Empty, SwitchInlineQueryCurrentChat = string.Empty, Url = "http://dualp.ir"
                            },
                        },
                        new List <InlineKeyboardButton>
                        {
                            new InlineKeyboardButton {
                                Text = "SwitchInlineQuery", SwitchInlineQuery = "سلام", Url = string.Empty, CallbackData = string.Empty, SwitchInlineQueryCurrentChat = string.Empty
                            }
                        }
                    }
                }
            });
            Bot.OnMessage       += OnMessage;
            Bot.OnInlineQuery   += OnInlineQuery;
            Bot.OnCallbackQuery += Bot_OnCallbackQuery;



            Console.WriteLine(@"TeleSharp initialized");

            Console.WriteLine($"Hi, My Name is : {Bot.Me.Username}");

            Console.ReadLine();
        }
Пример #2
0
        /// <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 (string.IsNullOrEmpty(message.Text) || string.IsNullOrEmpty(baseStoragePath))
            {
                return;
            }

            try
            {
                string sampleData = Path.Combine(baseStoragePath, "SampleData");
                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 <string> >
                                {
                                    new List <string>
                                    {
                                        "Yes",
                                        "No",
                                        "Cancel"
                                    }
                                }
                            },
                            ReplyToMessage = message
                        });

                    break;
                }

                case "yes":
                case "no":
                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)
            {
            }
        }