GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — xcomponent/Examples/xc.slack at master · xcomponent/xcomponent

Expand file tree

/

xc.slack

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

xc.slack

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
images
images
 
 
slackproject
slackproject
 
 
README.md
README.md
 
 
build-linux.sh
build-linux.sh
 
 
build-osx.sh
build-osx.sh
 
 
build.cake
build.cake
 
 
build.ps1
build.ps1
 
 

README.md

Outline

Slack Gateway

This project is a simple gateway to Slack. With this gateway you will be able to send messages to a Slack channel using XComponent.

Note: For this sample you should have a Slack account. On Slack, you should create a WebHook. For more details about WebHook, please refers to the Slack api documentation

Overview

XComponent is a platform to create, monitor and share microservices. If you want to have more details about microservices, you should read Martin Fowler's article.

In the Slack sample we're actually going to run two different pieces of software concurrently:

  • [Slack microservice] - A microservice which sends Merry Christmas messages to a Slack channel
  • [Console Application] - A simple application to communicate with the microservice

Build the project

Execute the following script:

PS > ./build.ps1

Run the "Hello World" example

Execute the following script :

PS > ./run.cmd

Note: RabbitMQ has to be running (default configuration)

Open XComponent project

Execute the following script at the root:

PS > ./xcstudio.cmd

Some details about the project

1. Slack proxy microservice

Slack proxy component

The Slack microservice contains only one component: the SlackProxy. The api to send messages on Slack is directly embedded in the Triggered methods of this component. With the code below, we have the hability to post messages on Slack. It is easy to customize this component to send your customs messages. By default, the component sends a *Merry Christmas message :)

try {
        SlackPublisher slackPublisher = new SlackPublisher();
        slackPublisher.UrlWithToken = sendMessage.SlackUrlWithToken;
        slackPublisher.Channel = sendMessage.SlackChannel;
        slackPublisher.SlackUser = sendMessage.SlackUser;
        if (!string.IsNullOrEmpty(sendMessage.Color))
        {
            slackPublisher.Color = sendMessage.Color;
        }
        if (!string.IsNullOrEmpty(sendMessage.Text))
        {
            slackPublisher.Text = sendMessage.Text;
        }
        if (!string.IsNullOrEmpty(sendMessage.IconEmoji))
        {
            slackPublisher.IconEmoji = sendMessage.IconEmoji;
        }
        if (!string.IsNullOrEmpty(sendMessage.MessageImage))
        {
            slackPublisher.MessageImage = sendMessage.MessageImage;
        }
        if (!string.IsNullOrEmpty(sendMessage.MessageTitle))
        {
            slackPublisher.MessageTitle = sendMessage.MessageTitle;
        }
        slackPublisher.SendMessage();
        sender.Success(context);
 } catch (Exception ex) {
        publishMessage.Message = ex.ToString();
        sender.Error(context);
 }

2. The console application

The console application embeds the XComponent client Api. This way, the application is able to communicate with the Slack microservice. This application needs several inputs:

  • The name of the Slack channel
  • The WebHook url
  • The name of the user

The console source code is quite simple:

  using (var mySlackGateway = new ApiWrapper<SlackGatewayApi>())
	        {
	            ClientApiOptions clientApiOptions = new ClientApiOptions();
	                //fill this object to override default xcApi parameters

	            if (myXChristmasApi.Init(mySlackGateway.Api.DefaultXcApiFileName, clientApiOptions))
	            {
	                using (AutoResetEvent autoResetEvent = new AutoResetEvent(false))
	                {
                        mySlackGateway.Api.SlackProxy_Component.PublishMessage_StateMachine.Published_State.InstanceUpdated +=
                         instance =>
                         {
                             Console.WriteLine("Message has been successfully published!");
                             autoResetEvent.Set();
                         };

                        mySlackGateway.Api.SlackProxy_Component.PublishMessage_StateMachine.Error_State.InstanceUpdated +=
                           instance =>
                           {
                               Console.WriteLine("Error while publishing message: " + instance.PublicMember.Message);
                               autoResetEvent.Set();
                           };

                        mySlackGateway.Api.SlackProxy_Component.SlackProxy_StateMachine.SendEvent(new SendMessage()
                        {
                            SlackChannel = cmdLineOptions.Channel,
                            SlackUrlWithToken = cmdLineOptions.WebHookUrl,
                            SlackUser = cmdLineOptions.User,
                            // MessageTitle = "My first slack message",
                            // Text = "Hello from slack",
                            //  MessageImage = "http://www2.xcomponent.com/wp-content/uploads/2015/12/logo-340x1561.png",
                            // IconEmoji = ":christmas_tree:" // http://www.emoji-cheat-sheet.com/
                        });

	                    autoResetEvent.WaitOne();
	                }

                    Console.WriteLine("Press any key to leave...");
                    Console.ReadKey();
                }
	            else
	            {
                    Console.WriteLine("Can't initialize client Api !");
                }
	        }
	    }

If the console application runs correctly, you will have a post like this on your Slack channel :)

Slack proxy component

Questions?

If you have any questions about this sample, please create a Github issue for us!

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.