diff --git a/NoobDevBot(Stream)/App.config b/NoobDevBot(Stream)/App.config deleted file mode 100644 index 9fed51c..0000000 --- a/NoobDevBot(Stream)/App.config +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/NoobDevBot(Stream)/NoobDevBot(Stream).csproj b/NoobDevBot(Stream)/NoobDevBot(Stream).csproj deleted file mode 100644 index 6e93129..0000000 --- a/NoobDevBot(Stream)/NoobDevBot(Stream).csproj +++ /dev/null @@ -1,91 +0,0 @@ - - - - - Debug - AnyCPU - {27A6E8B1-F7C7-4CB7-AE69-E240ED7D9971} - Exe - Properties - NoobDevBot - NoobDevBot - v4.6 - 512 - true - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - ..\packages\NAudio.1.7.3\lib\net35\NAudio.dll - True - - - ..\packages\NAudio.Lame.1.0.4\lib\net20\NAudio.Lame.dll - True - - - ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll - True - - - - - ..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll - True - - - - - - - - - - - ..\packages\Telegram.Bot.10.3.1\lib\net46\Telegram.Bot.dll - True - - - - - - - - - - - - - Always - - - Always - - - - - \ No newline at end of file diff --git a/NoobDevBot(Stream)/Program.cs b/NoobDevBot(Stream)/Program.cs deleted file mode 100644 index fe69a06..0000000 --- a/NoobDevBot(Stream)/Program.cs +++ /dev/null @@ -1,99 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Telegram.Bot; -using Telegram.Bot.Args; -using Telegram.Bot.Types; -using Telegram.Bot.Types.ReplyMarkups; -using System.Speech.Synthesis; -using System.Speech.AudioFormat; -using System.Media; -using System.IO; -using NAudio.Wave; -using NAudio.Lame; - -namespace NoobDevBot -{ - class Program - { - static TelegramBotClient bot; - static ReplyKeyboardMarkup markup; - static SpeechSynthesizer spsy; - - static void Main(string[] args) - { - bot = new TelegramBotClient("232609616:AAGOJkDYQmSmUFhjToCt3JTtRnlWp3-TszE"); - markup = new ReplyKeyboardMarkup(); - spsy = new SpeechSynthesizer(); - - keyboard(); - bot.OnMessage += bot_OnMessage; - - bot.StartReceiving(); - - - - - - Console.ReadKey(); - } - - private static async void bot_OnMessage(object sender, MessageEventArgs e) - { - Console.WriteLine($"{e.Message.From.Username ?? e.Message.From.FirstName}: {e.Message.Text}"); - switch (e.Message.Text) - { - case "/nextStream": await bot.SendTextMessageAsync(e.Message.Chat.Id, DateTime.UtcNow.ToLongDateString(), replyMarkup: markup); break; - case "/sayHello": await bot.SendTextMessageAsync(e.Message.Chat.Id, $"Hallo {e.Message.From.Username ?? e.Message.From.FirstName}", replyMarkup: markup); break; - case "/speakHello": - - using (var stream = new MemoryStream()) - { - using (var outStream = new MemoryStream()) - { - - string filename = e.Message.From.Username ?? e.Message.From.FirstName + ".mp3"; - spsy.SetOutputToAudioStream(stream, new SpeechAudioFormatInfo(44100, AudioBitsPerSample.Sixteen, AudioChannel.Mono)); - - spsy.Speak($"Hallo {e.Message.From.Username ?? e.Message.From.FirstName}"); - stream.Position = 0; - int seconds; - WaveFormat format = new WaveFormat(44100, 1); - using (var mp3file = new LameMP3FileWriter(outStream, format, 128)) - { - stream.CopyTo(mp3file); - //((stream.Length) / (sampleRate * (bitRate / 8))) / channels - seconds = (int)(stream.Length / (44100 * 2)); //(int)wavefile.CurrentTime.TotalSeconds; - } - - outStream.Position = 0; - FileToSend fts = new FileToSend("Hello.mp3", outStream); - - await bot.SendAudioAsync(e.Message.Chat.Id, fts, seconds, "Cortana", "Hallo"); - - } - } - break; - default: await bot.SendTextMessageAsync(e.Message.Chat.Id, "I don't know, what do to with this command. Plase be so kind to use the keyboard, so you don't enter the wrong commands. Thank you very much.", replyMarkup: markup); break; - } - - } - - private static void keyboard() - { - var button00 = new KeyboardButton(); - button00.Text = "/sayHello"; - var button01 = new KeyboardButton(); - button01.Text = "/nextStream"; - var button02 = new KeyboardButton(); - button02.Text = "/speakHello"; - markup.Keyboard = new KeyboardButton[1][]; - markup.Keyboard[0] = new KeyboardButton[3]; - markup.Keyboard[0][0] = button00; - markup.Keyboard[0][1] = button01; - markup.Keyboard[0][2] = button02; - } - } -} diff --git a/NoobDevBot(Stream)/Properties/AssemblyInfo.cs b/NoobDevBot(Stream)/Properties/AssemblyInfo.cs deleted file mode 100644 index 180a66d..0000000 --- a/NoobDevBot(Stream)/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// Allgemeine Informationen über eine Assembly werden über die folgenden -// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, -// die einer Assembly zugeordnet sind. -[assembly: AssemblyTitle("NoobDevBot")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("NoobDevBot")] -[assembly: AssemblyCopyright("Copyright © 2016")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar -// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von -// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. -[assembly: ComVisible(false)] - -// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird -[assembly: Guid("27a6e8b1-f7c7-4cb7-ae69-e240ed7d9971")] - -// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: -// -// Hauptversion -// Nebenversion -// Buildnummer -// Revision -// -// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern -// übernehmen, indem Sie "*" eingeben: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/NoobDevBot(Stream)/libmp3lame.32.dll b/NoobDevBot(Stream)/libmp3lame.32.dll deleted file mode 100644 index 719126b..0000000 Binary files a/NoobDevBot(Stream)/libmp3lame.32.dll and /dev/null differ diff --git a/NoobDevBot(Stream)/libmp3lame.64.dll b/NoobDevBot(Stream)/libmp3lame.64.dll deleted file mode 100644 index 20f23ed..0000000 Binary files a/NoobDevBot(Stream)/libmp3lame.64.dll and /dev/null differ diff --git a/NoobDevBot(Stream)/packages.config b/NoobDevBot(Stream)/packages.config deleted file mode 100644 index 32f5ac7..0000000 --- a/NoobDevBot(Stream)/packages.config +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/NoobDevBot/App.config b/NoobDevBot/App.config index 458160b..fe7ee7c 100644 --- a/NoobDevBot/App.config +++ b/NoobDevBot/App.config @@ -1,8 +1,8 @@  -
+ - + - - - - - - \ No newline at end of file diff --git a/NoobDevBot/CommandManager.cs b/NoobDevBot/CommandManager.cs index 52d3fc9..0447303 100644 --- a/NoobDevBot/CommandManager.cs +++ b/NoobDevBot/CommandManager.cs @@ -17,7 +17,7 @@ public static class CommandManager private static CommandHandler commandHandler; private static TelegramBotClient telegramBot; private static ConcurrentDictionary> commandDictionary; - + public static void Initialize(TelegramBotClient telegramBot) { commandHandler = new CommandHandler(); @@ -34,6 +34,7 @@ public static bool Dispatch(string commandName, MessageEventArgs e) { if (commandName != null) { + Logger.Log($"User: {e.Message.From.Username ?? e.Message.From.FirstName} try to use {commandName}"); Console.WriteLine($"User: {e.Message.From.Username ?? e.Message.From.FirstName} try to use {commandName}"); commandName = commandName.ToLower(); @@ -65,8 +66,7 @@ private static bool deleteStream(MessageEventArgs e) } var command = new DeleteStreamCommand(telegramBot, e.Message.Chat.Id); - if (!command.Dispatch(e)) - return false; + command.FinishEvent += finishedCommand; @@ -82,9 +82,7 @@ private static bool insertStream(MessageEventArgs e) } var command = new InsertStreamCommand(telegramBot, e.Message.Chat.Id); - if (!command.Dispatch(e)) - return false; - + command.FinishEvent += finishedCommand; return commandDictionary.TryAdd(e.Message.Chat.Id, command.Dispatch); @@ -93,10 +91,7 @@ private static bool insertStream(MessageEventArgs e) private static bool nextStream(MessageEventArgs e) { var command = new NextStreamCommand(telegramBot, e.Message.Chat.Id); - if (!command.Dispatch(e)) - return false; - return command.Dispatch(e); } diff --git a/NoobDevBot/Commands/Command.cs b/NoobDevBot/Commands/Command.cs index 74169a6..c652b74 100644 --- a/NoobDevBot/Commands/Command.cs +++ b/NoobDevBot/Commands/Command.cs @@ -12,6 +12,8 @@ public class Command { public Func NextFunction { get; set; } + public bool Finished { get; protected set; } + public TOut Dispatch(TParameter parameter) => NextFunction(parameter); public delegate void FinishEventHandler(object sender, TParameter e); diff --git a/NoobDevBot/Commands/HelloCommand.cs b/NoobDevBot/Commands/HelloCommand.cs index 0ca0b64..cd15f52 100644 --- a/NoobDevBot/Commands/HelloCommand.cs +++ b/NoobDevBot/Commands/HelloCommand.cs @@ -23,10 +23,11 @@ public HelloCommand(TelegramBotClient telegramBot, long chatId) public bool WriteHello(MessageEventArgs e) { - Console.WriteLine($"{e.Message.From.Username ?? e.Message.From.FirstName}: {e.Message.Text}"); telegramBotClient.SendTextMessageAsync(id, $"Hallo {e.Message.From.Username ?? e.Message.From.FirstName}"); + NextFunction = null; RaiseFinishEvent(this, e); + Finished = true; return true; } } diff --git a/NoobDevBot/Commands/InsertStreamCommand.cs b/NoobDevBot/Commands/InsertStreamCommand.cs index 01f4be1..6dce56b 100644 --- a/NoobDevBot/Commands/InsertStreamCommand.cs +++ b/NoobDevBot/Commands/InsertStreamCommand.cs @@ -82,10 +82,13 @@ private void insertStream(MessageEventArgs e) { DatabaseManager.InsertNewStream(userId, date, streamTitle); DatabaseManager.Submit(); + DatabaseManager.GetNextStream(); AskUser("Dein Stream wurde erfolgreich eingetragen."); RaiseFinishEvent(this, e); + NextFunction = null; + Finished = true; } } } diff --git a/NoobDevBot/Commands/NextStreamCommand.cs b/NoobDevBot/Commands/NextStreamCommand.cs index 0b1e360..954a033 100644 --- a/NoobDevBot/Commands/NextStreamCommand.cs +++ b/NoobDevBot/Commands/NextStreamCommand.cs @@ -12,30 +12,31 @@ internal class NextStreamCommand : Command { private long id; + private streams nextStream; private TelegramBotClient telegramBot; public NextStreamCommand(TelegramBotClient telegramBot, long id) { this.telegramBot = telegramBot; this.id = id; + nextStream = DatabaseManager.NextStream; NextFunction = SendNextStream; } private bool SendNextStream(MessageEventArgs e) { - var stream = DatabaseManager.GetNextStream(); - string message; - if (stream != null) - message = $"Der Nächste Stream ist: {stream.title} am {stream.start} von {stream.user.name}"; + if (nextStream != null) + message = $"Der Nächste Stream ist: {nextStream.title} am {nextStream.start} von {nextStream.user.name}"; else message = "Leider konnte ich keinen Stream finden :("; telegramBot.SendTextMessageAsync(id, message); NextFunction = null; + Finished = true; RaiseFinishEvent(this, e); return true; diff --git a/NoobDevBot/DatabaseManager.cs b/NoobDevBot/DatabaseManager.cs index 4770cb4..5705b4a 100644 --- a/NoobDevBot/DatabaseManager.cs +++ b/NoobDevBot/DatabaseManager.cs @@ -10,11 +10,14 @@ namespace NoobDevBot { public static class DatabaseManager { - static NoobBotDatabaseDataContext context; + public static streams NextStream { get; private set; } + + private static NoobBotDatabaseDataContext context; public static void Initialize() { context = new NoobBotDatabaseDataContext(); + GetNextStream(); } public static bool UserExists(int id) => context.GetTable().Any(u => u.id == id); @@ -47,10 +50,21 @@ public static void InsertNewStream(int user, DateTime dateTime, string name) table.InsertOnSubmit(tempStream); } - public static streams GetNextStream() + public static void GetNextStream() { - var table = context.GetTable(); - return table.Where(s => s.start > DateTime.UtcNow).OrderBy(s => s.start).FirstOrDefault(); + Table table; + try + { + table = context.GetTable(); + } + catch (Exception e) + { + Console.WriteLine("Irgend so ein fehler"); + Logger.Log(e.Message, 2); + return; + } + + NextStream = table?.Where(s => s.start > DateTime.UtcNow)?.OrderBy(s => s.start)?.FirstOrDefault(); } public static List GetUserStreams(int id) => GetUser(id).streams.ToList(); diff --git a/NoobDevBot/Logger.cs b/NoobDevBot/Logger.cs new file mode 100644 index 0000000..7a43236 --- /dev/null +++ b/NoobDevBot/Logger.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NoobDevBot +{ + public static class Logger + { + private static StreamWriter logfile; + + static Logger() + { + logfile = new StreamWriter(File.OpenWrite("log.txt")); + logfile.AutoFlush = true; + logfile.BaseStream.Position = logfile.BaseStream.Length; + + } + + public static void StartLog() + { + appendLine("started"); + } + + public static void Log(string text, byte warningLevel = 0) => appendLine(text, warningLevel); + + public static void EndLog() + { + appendLine("end"); + logfile.Close(); + + } + + private static void appendLine(string text, byte warningLevel = 0) => logfile.WriteLine($"[{DateTime.Now}]({warningLevel}) {text}"); + + } +} diff --git a/NoobDevBot/NoobDevBot.csproj b/NoobDevBot/NoobDevBot.csproj index 00973ea..d98ff47 100644 --- a/NoobDevBot/NoobDevBot.csproj +++ b/NoobDevBot/NoobDevBot.csproj @@ -52,11 +52,11 @@ - ..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll + ..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll True - ..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll + ..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll True @@ -92,6 +92,7 @@ + True True @@ -147,13 +148,6 @@ - - - - Dieses Projekt verweist auf mindestens ein NuGet-Paket, das auf diesem Computer fehlt. Verwenden Sie die Wiederherstellung von NuGet-Paketen, um die fehlenden Dateien herunterzuladen. Weitere Informationen finden Sie unter "http://go.microsoft.com/fwlink/?LinkID=322105". Die fehlende Datei ist "{0}". - - -