Exemple #1
0
		static void Main(string[] args)
		{
			/* REPLACE THESE VALUES WITH YOUR OWN */
			var useSlackBot = false; // Set to true to use real Slack bot, rather than console.
			var slackToken = Environment.GetEnvironmentVariable("SLACK_BOT", EnvironmentVariableTarget.User); // Slack bot access token.
			var fbUrl = Environment.GetEnvironmentVariable("SLACK_BOT_FB_URL", EnvironmentVariableTarget.User); // FogBugz installation url.
			var fbToken = Environment.GetEnvironmentVariable("SLACK_BOT_FB_TOKEN", EnvironmentVariableTarget.User); // FogBugz access token.

			var handlers = new Handler[]
			{
				new EchoHandler(),
				new CountdownHandler(),
				new FogBugzCaseHandler(new Uri(fbUrl), fbToken),
				new SlowEchoHandler(),
				new MentionHandler(),
			};

			if (useSlackBot || args != null && args.Contains("slack"))
				RunSlackBotAsync(slackToken, handlers).Wait();
			else
			{
				Console.ForegroundColor = ConsoleColor.Yellow;
				Console.WriteLine("Using ConsoleBot by default. Please edit the values at the top of Program.cs to connect to a real Slack instance.");
				Console.WriteLine("Type 'quit' to quit.");
				Console.ResetColor();
				Console.WriteLine();
				RunConsoleBot(handlers);
			}
		}
Exemple #2
0
		static void RunConsoleBot(Handler[] handlers)
		{
			var bot = new ConsoleBot();

			foreach (var handler in handlers)
				bot.RegisterHandler(handler);

			bot.HandleInput();
		}
Exemple #3
0
		/// <summary>
		/// Used privately to pass a message on to each handler.
		/// </summary>
		async Task SendMessageToHandlerAsync(Channel channel, User user, string text, bool botIsMentioned, Handler handler)
		{
			try
			{
				await handler.OnMessage(channel, user, text, botIsMentioned, cancellationSource.Token);
			}
			catch (Exception ex)
			{
				await SendMessage(channel, ex.ToString());
			}
		}
Exemple #4
0
		static async Task RunSlackBotAsync(string token, Handler[] handlers)
		{
			Debug.Listeners.Add(new ConsoleTraceListener());

			using (var bot = await SlackBot.Connect(token))
			{
				foreach (var handler in handlers)
					bot.RegisterHandler(handler);

				Console.WriteLine("Press a key to disconnect...");
				Console.ReadKey();
				Console.WriteLine();

				await bot.Disconnect();
			}
		}
Exemple #5
0
		/// <summary>
		/// Registers a handler with the bot.
		/// </summary>
		public void RegisterHandler(Handler handler)
		{
			handler.SetBot(this);
			handlers.Add(handler);
		}