public string GenerateAccessToken()
        {
            BotAccessTokenResponse response = BotAPIStub.GenerateAccessToken(this.BotUserId);

            if (!ErrorValidator.IsResponseValid(response) && ErrorValidator.IsAccessTokenExpired(response))
            {
                this.KaizalaSession.GenerateAccessToken();
                response = BotAPIStub.GenerateAccessToken(this.BotUserId);
                if (!ErrorValidator.IsResponseValid(response))
                {
                    //TODO : throw exception
                }
            }
            this.AccessToken = response.accessToken;
            return(this.AccessToken);
        }
Esempio n. 2
0
        private SendMessageResponse SendSingleMessage(string groupId, string request)
        {
            NameValueCollection header = new NameValueCollection
            {
                { Constants.ACCESS_TOKEN_STRING, this.Session.GetAccessToken() }
            };

            SendMessageResponse response = AsyncHelpers.RunSync <SendMessageResponse>(() => APICallHandler.PostAsync <SendMessageResponse>(header, string.Format(Constants.SEND_MESSAGE_IN_GROUP_URL, Session.GetApiEndPoint(), groupId), request));

            if (!ErrorValidator.IsResponseValid(response) && ErrorValidator.IsAccessTokenExpired(response))
            {
                header.Remove(Constants.ACCESS_TOKEN_STRING);
                header.Add(Constants.ACCESS_TOKEN_STRING, Session.GenerateAccessToken());
                response = AsyncHelpers.RunSync <SendMessageResponse>(() => APICallHandler.PostAsync <SendMessageResponse>(header, string.Format(Constants.SEND_MESSAGE_IN_GROUP_URL, Constants.API_ROOT, groupId), request));
            }
            return(response);
        }
        public BotAccessTokenResponse GenerateAccessToken(string botUserId)
        {
            //preparing header
            NameValueCollection header = new NameValueCollection();

            header.Add(Constants.ACCESS_TOKEN_STRING, this.session.GetAccessToken());
            BotAccessTokenResponse response = AsyncHelpers.RunSync <BotAccessTokenResponse>(() => APICallHandler.GetAsync <BotAccessTokenResponse>(header, string.Format(Constants.GENERATE_BOT_ACCESS_TOKEN, Constants.API_ROOT, botUserId)));

            if (!ErrorValidator.IsResponseValid(response) && ErrorValidator.IsAccessTokenExpired(response))
            {
                header.Remove(Constants.ACCESS_TOKEN_STRING);
                header.Add(Constants.ACCESS_TOKEN_STRING, this.session.GenerateAccessToken());
                response = AsyncHelpers.RunSync <BotAccessTokenResponse>(() => APICallHandler.GetAsync <BotAccessTokenResponse>(header, string.Format(Constants.GENERATE_BOT_ACCESS_TOKEN, Constants.API_ROOT, botUserId)));
            }

            return(response);
        }
        public ErrorDetails DeleteBotUser(string botUserId)
        {
            //preparing header
            NameValueCollection header = new NameValueCollection();

            header.Add(Constants.ACCESS_TOKEN_STRING, this.session.GetAccessToken());
            CreateBotUserResponse response = AsyncHelpers.RunSync <CreateBotUserResponse>(() => APICallHandler.DeleteAsync <CreateBotUserResponse>(header, string.Format(Constants.UPDATE_BOT_USER_URL, Constants.API_ROOT)));

            if (!ErrorValidator.IsResponseValid(response) && ErrorValidator.IsAccessTokenExpired(response))
            {
                header.Remove(Constants.ACCESS_TOKEN_STRING);
                header.Add(Constants.ACCESS_TOKEN_STRING, this.session.GenerateAccessToken());
                response = AsyncHelpers.RunSync <CreateBotUserResponse>(() => APICallHandler.DeleteAsync <CreateBotUserResponse>(header, string.Format(Constants.UPDATE_BOT_USER_URL, Constants.API_ROOT)));
            }

            return(response);
        }
        /// <summary>
        /// Add Bot User to Group.
        /// </summary>
        /// <param name="accessToken">The AccessToken.</param>
        /// <param name="groupId">The Group Id.</param>
        /// <param name="addBotToGroupRequest">The AddBotToGroupRequest.</param>
        public ErrorDetails AddBotUserToGroup(string groupId, AddBotToGroupRequest addBotToGroupRequest)
        {
            //preparing header
            NameValueCollection header = new NameValueCollection();

            header.Add(Constants.ACCESS_TOKEN_STRING, this.session.GetAccessToken());
            string request = JsonConvert.SerializeObject(addBotToGroupRequest);
            CreateBotUserResponse response = AsyncHelpers.RunSync <CreateBotUserResponse>(() => APICallHandler.PostAsync <CreateBotUserResponse>(header, string.Format(Constants.ADD_BOT_TO_GROUP_URL, Constants.API_ROOT, groupId), request));

            if (!ErrorValidator.IsResponseValid(response) && ErrorValidator.IsAccessTokenExpired(response))
            {
                header.Remove(Constants.ACCESS_TOKEN_STRING);
                header.Add(Constants.ACCESS_TOKEN_STRING, this.session.GenerateAccessToken());
                response = AsyncHelpers.RunSync <CreateBotUserResponse>(() => APICallHandler.PostAsync <CreateBotUserResponse>(header, string.Format(Constants.ADD_BOT_TO_GROUP_URL, Constants.API_ROOT, groupId), request));
            }
            return(response);
        }
        public CreateBotUserResponse CreateBotUser(CreateBotUserRequest botUserRequest)
        {
            //preparing header
            NameValueCollection header = new NameValueCollection();

            header.Add(Constants.ACCESS_TOKEN_STRING, this.session.GetAccessToken());
            string request = JsonConvert.SerializeObject(botUserRequest);
            CreateBotUserResponse response = AsyncHelpers.RunSync <CreateBotUserResponse>(() => APICallHandler.PostAsync <CreateBotUserResponse>(header, string.Format(Constants.CREATE_BOT_USER_URL, Constants.API_ROOT), request));

            if (!ErrorValidator.IsResponseValid(response) && ErrorValidator.IsAccessTokenExpired(response))
            {
                header.Remove(Constants.ACCESS_TOKEN_STRING);
                header.Add(Constants.ACCESS_TOKEN_STRING, this.session.GenerateAccessToken());
                response = AsyncHelpers.RunSync <CreateBotUserResponse>(() => APICallHandler.PostAsync <CreateBotUserResponse>(header, string.Format(Constants.CREATE_BOT_USER_URL, Constants.API_ROOT), request));
            }

            return(response);
        }