Azure Web App Bot-OAuth完成后触发事件

时间:2018-08-31 23:00:51

标签: node.js azure botframework bots azure-bot-service

我正在遵循此示例,以通过Salesforce实现OAuth, https://github.com/Microsoft/BotBuilder/blob/master/Node/examples/basics-oauth/app.js

代码

var bot = new builder.UniversalBot(connector, function (session) {
    builder.OAuthCard.create(connector, session, connectionName, "Please sign in", "Sign in", (createSignInErr, signInMessage) => {
        if (signInMessage) {
            session.send(signInMessage);
            session.userData.activeSignIn = true;
        } else {
            session.send("Something went wrong trying to sign you in.");
        }
    });
}).on("event", (event) => {         // Handle 'event' activities
        if (event.name == 'tokens/response') {
            // received a TokenResponse, which is how the Azure Bot Service responds with the user token after an OAuthCard
            bot.loadSession(event.address, (err, session) => {
                let tokenResponse = event.value;
                session.send('You are now signed in with token: ' + tokenResponse.token);
                session.userData.activeSignIn = false;
            });
        }
    });

最后,有一个绑定.on("event", (event) => { console.log(event); }之类的事件无法正常工作。

问题 我能够对用户进行身份验证而没有任何问题。但是,不确定如何为oAuth complete添加处理程序。

更新

登录Button在MS Teams上不起作用,因此,我重新编写了如下代码,

var login = (session) => {
    const config = require('config');
    const botConfig = config.get('BOT');
    const connectionName = botConfig.OAUTH_CONNECTION_NAME;
    console.log('address', session.message.address);
    connector.getSignInLink(session.message.address, connectionName, function (getSignInLinkErr, link) {
        var msg = new builder.Message(session)
            .attachments([
                new builder.SigninCard(session)
                    .text("Please click this link to sign in first.")
                    .button("signin", link)
            ]);
        session.send(msg);
        builder.Prompts.text(session, "You must first sign into your account.");
    });
}
bot.dialog('signinPrompt', [
    (session, args) => {
        login(session);
    },
    (session, results) => {
        console.log('results', results);
    },
    (session, results) => {
        console.log('results', results);
    }
]);

bot.dialog('GreetingDialog', [
    (session, args, next) => {
        const isSignedIn = salesforce.isSignedIn(connector, session);
        if (!isSignedIn) {
            session.beginDialog('signinPrompt');
        } else {
            next();
        }
    },
    (session, results, next) => {
        console.log('session userdata', session.userData);
    }
]).triggerAction({
    matches: 'Greeting'
});

bot.on('trigger', function (message) {
    var queuedMessage = message.value;
    var reply = new builder.Message()
        .address(queuedMessage.address)
        .text('This is coming from the trigger: ' + queuedMessage.text);
    bot.send(reply);
});

bot.on("event", (event) => {         // Handle 'event' activities
    if (event.name == 'tokens/response') {
        // received a TokenResponse, which is how the Azure Bot Service responds with the user token after an OAuthCard
        bot.loadSession(event.address, (err, session) => {
            let tokenResponse = event.value;
            session.send('You are now signed in with token: ' + tokenResponse.token);
            session.userData.activeSignIn = false;
        });
    }
});

connector.onInvoke((event, cb) => {
    if (event.name == 'signin/verifyState') {
        // received a MS Team's code verification Invoke Activity
        bot.loadSession(event.address, (err, session) => {
            let verificationCode = event.value.state;
            // Get the user token using the verification code sent by MS Teams
            connector.getUserToken(session.message.address, connectionName, verificationCode, (err, result) => {
                session.send('You are now signed in with token: ' + result.token);
                session.userData.activeSignIn = false;
                cb(undefined, {}, 200);
            });
        });
    } else {
        cb(undefined, {}, 200);
    }
});

身份验证完成后,我能够获得魔术代码,但无法对其进行验证。我真的不确定此oauth回调如何工作,在完成oauth之后,我们如何获取信息?我被困在这里,无法继续进行。 .on('trigger').on('event').onInvoke无效。

真的很感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

所以看来您这里有2个问题。

  1. 除非您将机器人框架域添加到清单中(如token.botframework.com),否则开箱即用的登录按钮将无法在团队中使用。我将在下面发布完整清单的示例。您尝试的方式也可以,但是如果没有密码,将不允许登录。

  2. 您没有收到来自团队的调用活动。为此,您需要向清单应用程序添加清单。我将在下面发布一个示例,但是有一个documentation page here可能也有帮助。

这是清单的样子,您需要在文件夹中包括图标部分中使用的2张图像,然后将其压缩以将其上载到团队中

{
    "$schema": "https://statics.teams.microsoft.com/sdk/v1.0/manifest/MicrosoftTeams.schema.json",
    "manifestVersion": "1.0",
    "version": "1.0.0",
    "id": "{Your bot's app ID}",
    "packageName": "bot.builder",
    "developer": {
        "name": "{Your Name}",
        "websiteUrl": "https://example.com/",
        "privacyUrl": "https://example.com/privacy",
        "termsOfUseUrl": "https://example.com/app-tos"
    },
    "name": {
        "short": "{Name of your app}"
    },
    "description": {
        "short": "A Bot",
        "full": "This app displays information."
    },
     "icons": {
        "outline": "{Required image}",
        "color": "{Required image}"
     },
    "accentColor": "#0099ff",
    "bots": [
        {
            "botId": "{Your bot's app ID}",
            "isNotificationOnly": false,
            "needsChannelSelector": "true",
            "scopes": [
                "personal",
                "team"
            ]
        }
    ],
  "validDomains": [
     "token.botframework.com"
  ]
}