Пример #1
0
        private async Task <bool> HandleFromOAuthCallbackResponse(IDialogContext context, IMessageActivity msg)
        {
            // 拿出 user data 裏面的資料
            if (context.UserData.TryGetValue(BotUtility.AccessToken, out string token) == false)
            {
                return(false);
            }
            var userProfile = await GoogleOAuthHelper.GetUserInfo(token);

            var reply = context.MakeMessage();

            reply.Speak       = reply.Text = $"Login success. your name is: {userProfile.Name}";
            reply.Attachments = new List <Attachment>()
            {
                new HeroCard("Login success",
                             $"your name is: {userProfile.Name}",
                             $"locale: {userProfile.Locale}",
                             new List <CardImage>()
                {
                    new CardImage()
                    {
                        Url = userProfile.Picture, Alt = "Hero Card Image Alt"
                    }
                }).ToAttachment()
            };
            await context.PostAsync(reply);

            return(true);
        }
Пример #2
0
        private async Task CheckLogin(IDialogContext context, IMessageActivity msg)
        {
            if (context.UserData.TryGetValue(BotUtility.AccessToken, out string token))
            {
                await context.PostAsync("you are already logined.");
            }
            else
            {
                // 保存這次對話的記錄,登入完畢後要 ResumeAsync 回到原本的對話
                var conversationReference = context.Activity.ToConversationReference();

                string authUrl = GoogleOAuthHelper.GetGoogleLoginURL(conversationReference, BotUtility.OAuthCallbackURL);

                var reply = context.MakeMessage();

                reply.Text = "Please login in using this card";
                reply.Attachments.Add(SigninCard.Create("You need to authorize me",
                                                        "Login to Google!",
                                                        authUrl
                                                        ).ToAttachment());
                await context.PostAsync(reply);
            }

            context.Wait(MessageReceivedAsync);
        }
        public async Task <HttpResponseMessage> Get(string code, string state, CancellationToken cancellationToken)
        {
            // 從 state 參數轉換為原本的 ConversationReference
            ConversationReference conversationReference = UrlToken.Decode <ConversationReference>(state);

            // 請求拿到 Google OAuth 的 Access Token
            var accessToken = await GoogleOAuthHelper.ExchangeCodeForGoogleAccessToken(code, BotUtility.OAuthCallbackURL);

            var msg = conversationReference.GetPostToBotMessage();

            // 取得目前談話對象的容器,並且把 UserData 加入 Access Token
            using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, msg))
            {
                IStateClient sc       = scope.Resolve <IStateClient>();
                BotData      userData = sc.BotState.GetUserData(msg.ChannelId, msg.From.Id);
                userData.SetProperty(BotUtility.AccessToken, accessToken.AccessToken);
                sc.BotState.SetUserData(msg.ChannelId, msg.From.Id, userData);
            }

            // 設定 ResumeAsync 回到 MessagesController 的識別值 (例如: 使用 token 關鍵字, 真實案例不適合這樣用)
            msg.Text = "token:" + accessToken.AccessToken;

            // 要記得使用 RsumeAsync 才能夠接回原本的 converstaion
            await Conversation.ResumeAsync(conversationReference, msg);

            return(Request.CreateResponse("ok"));
        }
Пример #4
0
 public LeavesController(
     ApiClient apiClient,
     AuthHelper authHelper,
     GoogleOAuthHelper googleOAuthHelper)
 {
     this.authHelper        = authHelper;
     this.apiClient         = apiClient;
     this.googleOAuthHelper = googleOAuthHelper;
 }