How to embed yammer private messages on my website?
Asked Answered
D

1

6

I know how to embed a feed which has a certain ID. I already did it. Now I'd like to implement the following functionality: If a user receives a private message, it will appear on an embedded feed. The best option in my opinion would be to embed the whole "chat window", but I didn't find a single code sample on the web. How can I do that?

Disrate answered 6/5, 2016 at 18:26 Comment(2)
Have you looked at the Yammer API? Here's a particular page on requesting private messages.Dickerson
Well I did, but their documentation doesn't explain exactly how to use it. They didn't provide any code snippet or a working exampleDisrate
K
2

You cannot really embed private messages like you can with feeds, because Yammer's REST APIs (incl. private messages) require authentication via OAuth 2.0. That means you have to create a Yammer API application which will ask your users to log in and allow you to access their messages. The overall concept of that described in their documentation here and here.

Yammer provides several SDKs you can use, one of them is the Javascript SDK. I pieced togehter a simple example of how you can ask users to log in and then it will display their private messages. Mind you, this is a very simple solution, I just tested it on a single one-to-one conversation.

<!DOCTYPE HTML>
<html>
<head>
    <script type="text/javascript" data-app-id="YOUR-APP-CLIENT-ID" src="https://c64.assets-yammer.com/assets/platform_js_sdk.js"></script>
</head>
<body>
<span id="yammer-login"></span>
<div id="messages"></div>
<script>
yam.connect.loginButton('#yammer-login', function (resp) {
    if (resp.authResponse) {
      document.getElementById('yammer-login').innerHTML = 'Welcome to Yammer!';
    }
});

var msgdiv = document.querySelector("#messages");

yam.getLoginStatus(
  function(response) {
    if (response.authResponse) {
      console.log("logged in");
      var myId = response.authResponse.user_id;
      yam.platform.request({
        url: "messages/private.json",
        method: "GET",
        success: function (response) {
            console.log("The request was successful.");
            var usernames = {};
            response.references.forEach(function(ref){
                if(ref.type === "user") {
                    usernames[ref.id] = ref.full_name;
                }
            });
            response.messages.forEach(function(message){
                var msg = document.createElement("span");
                msg.innerHTML = usernames[message.sender_id] + ": " + message.body.parsed + "<br/>";
                msgdiv.appendChild(msg);
            })
        },
        error: function (response) {
            console.log("There was an error with the request.");
            console.dir(private);
        }
      });
    }
    else {
      console.log("not logged in")
    }
  }
);
</script>
</body>
</html>

The response from the messages/private.json API endpoint is a JSON file that you can go through. It includes information about the message and the users involved in the conversation.

Katushka answered 15/5, 2016 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.