Search code examples
amazon-web-servicesswift3xamarin.iosamazon-cognitoamazon-lex

Amazon LEX is returning error with iOS Swift


Im trying to integrate Amazon LEX into my application. Initially i added the Amazon cognito and from that i got the Cognito Id. Next when i tried to communicate with the LEX is returning an error saying like this

Error Domain=com.amazonaws.AWSLexErrorDomain Code=0 "null" UserInfo={NSLocalizedDescription=null, NSLocalizedFailureReason=AccessDeniedException:http://internal.amazon.com/coral/com.amazon.coral.service/}

AppDelegate Code :

{
        // Override point for customization after application launch.


        AWSDDLog.sharedInstance.logLevel = .verbose
        let credentialProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: “given the Id”)
        let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialProvider)
        AWSServiceManager.default().defaultServiceConfiguration = configuration


        credentialProvider.getIdentityId().continueWith(block: { (task) -> AnyObject? in

            if (task.error != nil)
            {
                print("Error: " + task.error!.localizedDescription)
            }
            else
            {
                let cognitoId = task.result!
                print("Cognito Id is, \(cognitoId)")
            }

            return task

        })

        let chatConfig = AWSLexInteractionKitConfig.defaultInteractionKitConfig(withBotName: "BookTrip", botAlias: "Chatting")
        AWSLexInteractionKit.register(with: configuration!, interactionKitConfiguration: chatConfig, forKey: "AWSLexVoiceButton")

        chatConfig.autoPlayback = false
        //AWSLexInteractionKit.register(with: configuration!, interactionKitConfiguration: chatConfig, forKey: "chatConfig")
    return true
    }

The viewController code :

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

       (self.voiceButton)?.delegate = self
   // (self.voiceButton as AWSLexVoiceButton).delegate = self


    }

    func voiceButton(_ button: AWSLexVoiceButton, on response: AWSLexVoiceButtonResponse) {
        DispatchQueue.main.async(execute: {
            // `inputranscript` is the transcript of the voice input to the operation
            print("Input Transcript: \(String(describing: response.inputTranscript))")
            if let inputTranscript = response.inputTranscript {
                self.input.text = "\"\(inputTranscript)\""
            }
            print("on text output \(String(describing: response.outputText))")
            self.output.text = response.outputText
        })
    }

    public func voiceButton(_ button: AWSLexVoiceButton, onError error: Error) {
        print("error \(error)")
    }

Do i need to pass the cognito Id along with LEX. Could anyone please help me what is wrong here. Thanks in advance.


Solution

  • Finally i figured out the solution. The issue was with IAM permission. You have to attach permission policy in IAM console. Hope this help you.