如何在我的网站上嵌入yammer私人信息

How to embed yammer private messages on my website?

本文关键字:yammer 信息 我的 网站      更新时间:2023-09-26

我知道如何嵌入具有特定ID的提要。我已经做过了。现在我想实现以下功能:如果用户收到私人消息,它将出现在嵌入的提要上。在我看来,最好的选择是嵌入整个"聊天窗口",但我在网上没有找到任何代码示例。我该怎么做?

您不能像使用提要那样真正嵌入私有消息,因为Yammer的RESTAPI(包括私有消息)需要通过OAuth 2.0进行身份验证。这意味着您必须创建一个Yammer API应用程序,该应用程序将要求您的用户登录并允许您访问他们的消息。这里和这里的文档中描述了这一点的总体概念。

Yammer提供了几个您可以使用的SDK,其中一个是Javascript SDK。我拼凑了一个简单的例子,说明如何要求用户登录,然后它会显示他们的私人消息。请注意,这是一个非常简单的解决方案,我只是在一次一对一的对话中测试了它。

<!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>

来自messages/private.json API端点的响应是一个JSON文件,您可以通过它。它包括有关消息和参与对话的用户的信息。