Example #1
0
 public static string ReplaceVars(string input, Session ses = null)
 {
     if (ses == null)
     {
         ses = new Session();
     }
     return input.Replace("$token", ses.Token).Replace("$user", ses.User).Replace("$date", DateTime.Now.ToShortDateString()).Replace("$time", DateTime.Now.ToShortTimeString().Replace("$nl", "\n"));
 }
Example #2
0
        static async Task Run()
        {
            fs = JsonConvert.DeserializeObject<FestivalSettings>(System.IO.File.ReadAllText(settingsPath));
            Bot = new Api(fs.token);
            var me = await Bot.GetMe();
            Console.WriteLine("Connection to account \"{0}\" established.", me.Username);
            var offset = 0;

            while (true)
            {
                fs = JsonConvert.DeserializeObject<FestivalSettings>(System.IO.File.ReadAllText(settingsPath));
                var updates = await Bot.GetUpdates(offset);

                foreach (var update in updates)
                {
                    if (update.Message.Type == MessageType.TextMessage)
                    {
                        Session ps = new Session();
                        foreach (Session ses in sessions)
                        {
                            if (ses.User == update.Message.Chat.Username)
                            {
                                ps = ses;
                            }
                        }

                        if (ps.Live)
                        {
                            //Forward users message to console
                            var f = (Form1)Application.OpenForms[0];
                            f.AddUserMessage(update.Message.Chat.Username + ": " + update.Message.Text, "#1ED760");
                        }
                        else
                        {
                            if (update.Message.Text == "/start")
                            {
                                //Typing...
                                if (fs.showTyping)
                                {
                                    await Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
                                    await Task.Delay(1000 * fs.typingDuration);
                                }

                                //Generate token
                                Token = RandomString(fs.tokenLength);

                                //Generate and add session
                                Session s = new Session(update.Message.Chat.Username, Token, sessions.Count.ToString(), update.Message.Chat.Id);
                                sessions.Add(s);

                                //Send message
                                var t = await Bot.SendTextMessage(update.Message.Chat.Id, ReplaceVars(fs.startMessage, s));
                            }
                            else if (update.Message.Text == "/help")
                            {
                                //Typing...
                                if (fs.showTyping)
                                {
                                    await Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
                                    await Task.Delay(1000 * fs.typingDuration);
                                }

                                //Send message
                                var t = await Bot.SendTextMessage(update.Message.Chat.Id, ReplaceVars(fs.helpMessage));
                            }
                            else if (update.Message.Text == "/topic")
                            {
                                //Typing...
                                if (fs.showTyping)
                                {
                                    await Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
                                    await Task.Delay(1000 * fs.typingDuration);
                                }

                                //Token-keyboard
                                var to = new ReplyKeyboardMarkup();
                                to.Keyboard = new string[][] {
                                    new string[] { Token }
                                };
                                to.OneTimeKeyboard = true;
                                to.ResizeKeyboard = true;

                                //Send token-question
                                var tokenans = await Bot.SendTextMessage(update.Message.Chat.Id, "Gib bitte dein Anfrage-Token ein.", false, 0, to);

                                //Ignore latest
                                offset = update.Id + 1;

                                //Wait for answer
                                var tanswer = await Bot.GetUpdates(offset);
                                while (tanswer.Length < 1)
                                {
                                    tanswer = await Bot.GetUpdates(offset);
                                }

                                //Check for session
                                Session s = new Session();
                                foreach (Session ses in sessions)
                                {
                                    if (ses.Token == tanswer[0].Message.Text && ses.User == update.Message.Chat.Username)
                                    {
                                        s = ses;
                                    }
                                }

                                //Typing...
                                if (fs.showTyping)
                                {
                                    await Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
                                    await Task.Delay(1000 * fs.typingDuration);
                                }

                                //Topic-keyboard
                                //    var r = new ReplyKeyboardMarkup();
                                //    r.Keyboard = new string[][] {
                                //    new string[] { "SkrivaPrisma", "Polar" },
                                //    new string[] { "Floe", "LockWatch" },
                                //    new string[] { "Repo", "Website"}
                                //};
                                //    r.OneTimeKeyboard = true;
                                //    r.ResizeKeyboard = true;
                                var r = FestivalSettingsHelper.ParseKeyboard(fs.topicKeyboard);

                                //Send topic-question
                                var t = await Bot.SendTextMessage(update.Message.Chat.Id, "Bitte wähle ein Problemthema.", false, 0, r);

                                //Ignore latest
                                offset = update.Id + 2;

                                //Wait for answer
                                var answer = await Bot.GetUpdates(offset);
                                while (answer.Length < 1)
                                {
                                    answer = await Bot.GetUpdates(offset);
                                }

                                //Assign result to session
                                s.SetTopic(answer[0].Message.Text);

                                //Typing...
                                if (fs.showTyping)
                                {
                                    await Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
                                    await Task.Delay(1000 * fs.typingDuration);
                                }

                                //Echo
                                var t2 = await Bot.SendTextMessage(answer[0].Message.Chat.Id, String.Format("Du hast \"{0}\" als Thema gewählt.", answer[0].Message.Text));
                            }
                            else if (update.Message.Text == "/message")
                            {
                                //Typing...
                                if (fs.showTyping)
                                {
                                    await Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
                                    await Task.Delay(1000 * fs.typingDuration);
                                }

                                //Token-keyboard
                                var to = new ReplyKeyboardMarkup();
                                to.Keyboard = new string[][] {
                                new string[] { Token }
                            };
                                to.OneTimeKeyboard = true;
                                to.ResizeKeyboard = true;

                                //Send token-question
                                var tokenans = await Bot.SendTextMessage(update.Message.Chat.Id, "Gib bitte dein Anfrage-Token ein.", false, 0, to);

                                //Ignore latest
                                offset = update.Id + 1;

                                //Wait for answer
                                var tanswer = await Bot.GetUpdates(offset);
                                while (tanswer.Length < 1)
                                {
                                    tanswer = await Bot.GetUpdates(offset);
                                }

                                //Check for session
                                Session s = new Session();
                                foreach (Session ses in sessions)
                                {
                                    if (ses.Token == tanswer[0].Message.Text && ses.User == update.Message.Chat.Username)
                                    {
                                        s = ses;
                                    }
                                }

                                //Typing...
                                if (fs.showTyping)
                                {
                                    await Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
                                    await Task.Delay(1000 * fs.typingDuration);
                                }

                                //Send message-question
                                var t = await Bot.SendTextMessage(update.Message.Chat.Id, "Bitte gib deine Nachricht ein.");

                                //Ignore latest
                                offset = update.Id + 2;

                                //Wait for answer
                                var answer = await Bot.GetUpdates(offset);
                                while (answer.Length < 1)
                                {
                                    answer = await Bot.GetUpdates(offset);
                                }

                                //Assign result to session
                                s.SetMessage(answer[0].Message.Text);

                                //Typing...
                                if (fs.showTyping)
                                {
                                    await Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
                                    await Task.Delay(1000 * fs.typingDuration);
                                }

                                //Echo
                                var t2 = await Bot.SendTextMessage(answer[answer.Length - 1].Message.Chat.Id, String.Format("Du hast uns \"{0}\" geschrieben.", answer[answer.Length - 1].Message.Text));
                            }
                            else if (update.Message.Text == "/support")
                            {
                                //Typing...
                                if (fs.showTyping)
                                {
                                    await Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
                                    await Task.Delay(1000 * fs.typingDuration);
                                }

                                //Token-keyboard
                                var to = new ReplyKeyboardMarkup();
                                to.Keyboard = new string[][] {
                                new string[] { Token }
                            };
                                to.OneTimeKeyboard = true;
                                to.ResizeKeyboard = true;

                                //Send token-question
                                var tokenans = await Bot.SendTextMessage(update.Message.Chat.Id, "Gib bitte dein Anfrage-Token ein.", false, 0, to);

                                //Ignore latest
                                offset = update.Id + 1;

                                //Wait for answer
                                var tanswer = await Bot.GetUpdates(offset);
                                while (tanswer.Length < 1)
                                {
                                    tanswer = await Bot.GetUpdates(offset);
                                }

                                //Check for session
                                Session s = new Session();
                                foreach (Session ses in sessions)
                                {
                                    if (ses.Token == tanswer[0].Message.Text && ses.User == update.Message.Chat.Username)
                                    {
                                        s = ses;
                                    }
                                }

                                //Submit session
                                Console.WriteLine("====================");
                                Console.WriteLine("Id: " + s.Id);
                                Console.WriteLine("Date: " + update.Message.Date.ToShortDateString() + " Time: " + update.Message.Date.ToShortTimeString());
                                Console.WriteLine("Token: " + s.Token);
                                Console.WriteLine("User: "******"Topic: " + s.Topic);
                                Console.WriteLine("Message: " + s.Message);
                                Console.WriteLine("====================");

                                //Set support
                                s.Support = true;

                                //Refresh ListView
                                var f = (Form1)Application.OpenForms[0];
                                System.Threading.ThreadStart startInfo = new System.Threading.ThreadStart(f.refreshSessions);
                                System.Threading.Thread refresh = new System.Threading.Thread(startInfo);
                                refresh.Start();

                                //Typing...
                                if (fs.showTyping)
                                {
                                    await Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
                                    await Task.Delay(1000 * fs.typingDuration);
                                }

                                //Send message
                                var t = await Bot.SendTextMessage(update.Message.Chat.Id, "Deine Support-Anfrage wurde uns übermittelt.\nBitte benutze für eine neue Anfrage zuerst den /start Befehl.");
                            }
                            else if (false)
                            {
                                var r = new ReplyKeyboardMarkup();
                                r.Keyboard = new string[][] {
                                new string[] { "Ja", "Nein" }
                            };
                                r.OneTimeKeyboard = true;
                                r.ResizeKeyboard = true;
                                var t = await Bot.SendTextMessage(update.Message.Chat.Id, "Hilfe erforderlich?", false, 0, r);
                            }
                        }
                    }

                    if (update.Message.Type == MessageType.PhotoMessage)
                    {
                        var file = await Bot.GetFile(update.Message.Photo.LastOrDefault()?.FileId);

                        Console.WriteLine("Received Photo: {0}", file.FilePath);

                        var filename = file.FileId + "." + file.FilePath.Split('.').Last();

                        using (var profileImageStream = File.Open(filename, FileMode.Create))
                        {
                            await file.FileStream.CopyToAsync(profileImageStream);
                        }
                    }

                    offset = update.Id + 1;
                }

                await Task.Delay(1000);
            }
        }