Recently I wanted to add messages to Yammer to inform people about the deployment of components being performed.  I wanted to add something to my deployment scripts to make this happen in an automated fashion.

I was looking into the Yammer API options and some samples but while they initially looked ok I found it was quite painful trying to workout how to use it from a non user interactive process.  Most of the samples seem to assume your using Yammer from an MVC app or a Windows Store app where a user is involved.

In my scenario I wanted to create a console application to wrap up some functionality to put together a nice useful message and then post it to Yammer.  I worked out how to do it in the end and thought it was worth sharing so others dont have the same pain.

Step 1 – Register your App

Step one is where you will register your app with Yammer.  You will go to a url something like the below:

https://www.yammer.com/[My yammer tenant]/client_applications?from=nav

This will take you to the page to register your app and you can click the “Register Application” button to add a new one.

1

On the new application registration you will need to provide some details about your app.  The settings can be pretty much whatever you want, the only one that is really important is the redirect url which you can provide any value for it, but you need to keep a note of that value for later.  I used the value http://localhost/myapp.

When you click “Register new Application” you will be redirected to a screen which will show the details for your client id, client secret and redirect url.  Make sure to keep a note of these as you will need them later.

Step 2 – Create a Service Account style user

The next step is to create a service account style user to post the messages as.  You dont want to use your own user because the token you get could be compromised and someone could post messages as you.

In our case we have Office 365/Azure AD connected to Yammer so we will create a new cloud only account in Azure AD and set it up so that it can be registered with Yammer.

We will also create a new group which we will send the messages to.  This means that people can choose to see deployment notifications rather than everyone getting them.  A user would join the deployment notifications group to get the messages.

Once you have created the group you will see in the url the id for that group and you will need to keep a note of this for later.

 

Step 4 – Get the Access Token

Now for the bit that was confusing.  When the user is not interactively involved I was struggling to workout how to get the access token from the API but it is much simpler than that.  I can put a couple of url’s into google chrome and get the token which is long lived and i can reuse it from my code.

1. – Get the application code

The first step is to use the url below but replace the query string values with those you saved earlier.

https://www.yammer.com/oauth2/authorize?client_id=[client id]&response_type=code&redirect_uri=[redirect url]

If you are logged into Yammer as the service account then put this into chrome and you will be presented with a screen where you will be asked to allow access to this application.  Click “Allow”.  You will then be redirected to a blank screen but the url will include a query string parameter called “code”.  Take the value from this parameter and keep it for the next step.

2. – Get the access token

Now to get the access token format the below url with the parameters you have.

https://www.yammer.com/oauth2/access_token.json?client_id=[client id]&client_secret=[client secret]&code=[code]

Put this into chrome and you will be presented with a json response message.  From this message you need to get the token value which is at the below json path.

{ “access_token” : { “token”

You can also just search for token and you will find it.  Keep the token value for later.

Step 5 – Create the Console Application

Next I created a console application which could use a NuGet package called Yammer.SimpleAPI.  With this I can use the token I retrieved and send a message to Yammer with 2 lines of code, example below:

var client = new YammerClient(token);
client.PostMessage(“Hello World”, Convert.ToInt64(groupId), “Topic”);

You can see this is now very easy and I could add a little extra information to my console application to make it post a useful message like this:

“Deployment Completed

Component Name: AppFx.Tools.Yammer.DeploymentNotification
Environment Name: Prod
Version Number: 2015.06.01.1
Machine Name: CSC-SERVER02
Deployed at: 6/12/2015 10:05:51 PM”

The topic allows you to relate messages so you could see ones about similar topics.  I chose to use the component name being deployed as a topic so i can see messages for one component easily filtered.

The console application could be executed with a command like below:

AppFx.Tools.Yammer.DeploymentNotification [access token] [group id] [deployment environment] [component name] [version number]

You can see above I can easily pass some information from my deployment script to the tool which it can use to format a nice message to be put on Yammer.

 

Other Ideas

Now that we can easily use the Yammer Simple API component there are a few ideas where I could use this:

  • I might look at getting BizTalk to post interesting information for users.  Perhaps an easy way to tell users that a file has been received if you get occasional files which BizTalk processes
  • I could drive the yammer api dll from powershell if I wanted to do deployment notifications in a different way
  • I can build console apps or tools which drive Yammer without the need for interactive user involvement

 

Sample Code

The sample code for this is available from the following location:

http://cscblogsamples.blob.core.windows.net/publicblogsamples/AppFx.Tools.Yammer.DeploymentNotification.zip

 

 

Buy Me A Coffee