Things used in this project

Hardware components:
Echo
Amazon Alexa Amazon Echo
×1
Software apps and online services:
Dp image kit 02
Amazon Alexa Alexa Skills Kit
Screen%20shot%202015 07 20%20at%206.10.26%20pm
Amazon Web Services AWS Lambda

Schematics

Schematic
Schematic kbdvan82r3

Code

App code (index.js)JavaScript
You need to install "request" module to work the code properly.
// the module is required for http get request
var request = require("request")
var ayahNum = 0;
var surahNum = 0;
// Route the incoming request based on type (LaunchRequest, IntentRequest,
// etc.) The JSON body of the request is provided in the event parameter.
exports.handler = function (event, context) {
    try {
        console.log("event.session.application.applicationId=" + event.session.application.applicationId);

        /**
         * Uncomment this if statement and populate with your skill's application ID to
         * prevent someone else from configuring a skill that sends requests to this function.
         */

       // if (event.session.application.applicationId !== "") {
       //     context.fail("Invalid Application ID");
       //  }

        if (event.session.new) {
            onSessionStarted({requestId: event.request.requestId}, event.session);
        }

        if (event.request.type === "LaunchRequest") {
            onLaunch(event.request,
                event.session,
                function callback(sessionAttributes, speechletResponse) {
                    context.succeed(buildResponse(sessionAttributes, speechletResponse));
                });
        } else if (event.request.type === "IntentRequest") {
            onIntent(event.request,
                event.session,
                function callback(sessionAttributes, speechletResponse) {
                    context.succeed(buildResponse(sessionAttributes, speechletResponse));
                });
        } else if (event.request.type === "SessionEndedRequest") {
            onSessionEnded(event.request, event.session);
            context.succeed();
        }
    } catch (e) {
        context.fail("Exception: " + e);
    }
};

/**
 * Called when the session starts.
 */
function onSessionStarted(sessionStartedRequest, session) {
    // add any session init logic here
}

/**
 * Called when the user invokes the skill without specifying what they want.
 */
function onLaunch(launchRequest, session, callback) {
    getWelcomeResponse(callback)
}

/**
 * Called when the user specifies an intent for this skill.
 */
function onIntent(intentRequest, session, callback) {

    var intent = intentRequest.intent
    var intentName = intentRequest.intent.name;
  
    // dispatch custom intents to handlers here
    if (intentName == "GetAyahIntent") {
        ayahNum = intentRequest.intent.slots.ayah.value;
        handleGetAyahIntent(intent, session, callback)
    }else if (intentName == "GetSurahIntent") {
        surahNum = intentRequest.intent.slots.surah.value;
        handleGetSurahIntent(intent, session, callback)
    }else if ("AMAZON.HelpIntent" === intentName) {
        getHelp(callback);
    }else if ("AMAZON.StopIntent" === intentName || "AMAZON.CancelIntent" === intentName) {
        handleSessionEndRequest(callback);
	}else {
         throw "Invalid intent"
    }
}

/**
 * Called when the user ends the session.
 * Is not called when the skill returns shouldEndSession=true.
 */
function onSessionEnded(sessionEndedRequest, session) {
	console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId + ", sessionId=" + session.sessionId);
}

// ------- Skill specific logic -------

function getWelcomeResponse(callback) {
    var speechOutput = "Welcome! to the Quran Translator. You can ask, Alexa, ask Quran Translator Ayah Number 5."

    var reprompt = "You can ask, Alexa, ask Quran Translator Ayah Number 5?"

    var header = "Get Info"

    var shouldEndSession = false

    var sessionAttributes = {
        "speechOutput" : speechOutput,
        "repromptText" : reprompt
    }

    callback(sessionAttributes, buildSpeechletResponse(header, speechOutput, reprompt, shouldEndSession))

}

function handleGetAyahIntent(intent, session, callback) {

    var speechOutput = "We have an error"

    getJSON(function(data) {
        if (data != "ERROR") {
            var speechOutput = data
        }
        callback(session.attributes, buildSpeechletResponseWithoutCard(speechOutput, "", true))
    })

}

function handleGetSurahIntent(intent, session, callback) {

    var speechOutput = "We have an error"

    getJSONsurah(function(data) {
        if (data != "ERROR") {
            var speechOutput = data
        }
        callback(session.attributes, buildSpeechletResponseWithoutCard(speechOutput, "", true))
    })

}

function urlAyah(ayah) {
    var address = "http://api.alquran.cloud/ayah/" + ayah + "/editions/quran-simple,en.asad"
    //return "http://api.alquran.cloud/ayah/5/editions/quran-simple,en.asad"
    return address
}

function urlSurah(surah) {
    var address = "http://api.alquran.cloud/surah/" +surah+ "/en.asad"
    //var address = "http://api.alquran.cloud/surah/114/en.asad"
    return address
}


function getJSON(callback) {

     request.get(urlAyah(ayahNum), function(error, response, body) {
        var d = JSON.parse(body)
        var suraNumber = d.data[0].surah.number
        var ahyaNumber = d.data[0].number
        var meaning = d.data[1].text
        var surahName = d.data[1].surah.englishName
        var result = "Sura Number " + suraNumber + ". Name of the surah: " +surahName + ". Ahya number " + ahyaNumber + "." + " Meaning: " + meaning + "."
             callback(result);
      
     })
}

function getJSONsurah(callback) {

     request.get(urlSurah(surahNum), function(error, response, body) {
        var d = JSON.parse(body)
        var suraNumber = d.data.number
        var suraName = d.data.englishName
        var totalAyah = d.data.numberOfAyahs
        var i
        var meaning = ""
        for(i=0; i<totalAyah; i++) {
            meaning += "Ahya Number " + (i+1) + ". Meaning: " + d.data.ayahs[i].text + ". "
        }

        var result = "Surah number " + suraNumber + ". Name of the surah: " + suraName + ". " + meaning
             callback(result);
      
     })
}


function getHelp(callback) {
    var cardTitle = "Help";
    var speechOutput = "By using the Quran Translator you can learn the English meaning of any surah or ayah. Suppose, to learn first surah of the Quran you can say, Alexa, ask Quran translator surah 1. To learn first ayah say, ayah 1." ;
    var repromptText = "Would you like to learn any surah or ayah?";
    var shouldEndSession = false;

    callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));

}


function handleSessionEndRequest(callback) {
    var cardTitle = "Session Ended";
    var speechOutput = "Thank you for using Alexa Quran Translator, Have a nice day!";
    var shouldEndSession = true;
    callback({}, buildSpeechletResponse(cardTitle, speechOutput, null, shouldEndSession));
}


// ------- Helper functions to build responses for Alexa -------

function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
    return {
        outputSpeech: {
            type: "PlainText",
            text: output
        },
        card: {
            type: "Simple",
            title: title,
            content: output
        },
        reprompt: {
            outputSpeech: {
                type: "PlainText",
                text: repromptText
            }
        },
        shouldEndSession: shouldEndSession
    };
}

function buildSpeechletResponseWithoutCard(output, repromptText, shouldEndSession) {
    return {
        outputSpeech: {
            type: "PlainText",
            text: output
        },
        reprompt: {
            outputSpeech: {
                type: "PlainText",
                text: repromptText
            }
        },
        shouldEndSession: shouldEndSession
    };
}

function buildResponse(sessionAttributes, speechletResponse) {
    return {
        version: "1.0",
        sessionAttributes: sessionAttributes,
        response: speechletResponse
    };
}
uttarences.txtC/C++
GetAyahIntent ayah number {ayah}
GetAyahIntent ayah {ayah}

GetSurahIntent surah number {surah}
GetSurahIntent surah {surah}
intent.jsonJSON
{
  "intents": [
    {
      "slots": [
        {
          "name": "ayah",
          "type": "AMAZON.NUMBER"
        }
      ],
      "intent": "GetAyahIntent"
    },
    {
      "slots": [
        {
          "name": "surah",
          "type": "AMAZON.NUMBER"
        }
      ],
      "intent": "GetSurahIntent"
    },
    {
      "intent": "AMAZON.StartOverIntent"
    },
    {
      "intent": "AMAZON.RepeatIntent"
    },
    {
      "intent": "AMAZON.HelpIntent"
    },
    {
      "intent": "AMAZON.YesIntent"
    },
    {
      "intent": "AMAZON.NoIntent"
    },
    {
      "intent": "AMAZON.StopIntent"
    },
    {
      "intent": "AMAZON.CancelIntent"
    }
  ]
}
Complete Source Code in Github
This repository contains all the necessary files including node module.

Credits

Taifur1
Md. Khairul Alam

Engineer, developer, maker, hacker & tech lover.

Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

IoT Red Phone
Intermediate
  • 574
  • 6

Work in progress

The phone will ring if you have an alert in your AWS Cloudwatch. If you pick up the handset, it tells you whats wrong.

Real-Time Workspace Occupancy Sensing Based on AWS IoT
Intermediate
  • 530
  • 5

Sensor-based presence detection for individual workstations: capturing occupancy trends and space utilization in real-time.

Movie History - Keep Your Movie Watching Footprint
Intermediate
  • 85
  • 3

Full instructions

An Alexa skill remembers your movie footprint.

How To Run Amazon Alexa On Windows 10 PC
Intermediate
  • 186
  • 2

Protip

Alexa wants to meet Cortana. Now you can run Amazon Alexa voice service in several platforms like macOS, Linux, Windows and Raspbian.

Run Google Assistant on Your Amazon Echo
Intermediate
  • 5,996
  • 37

Full instructions

This is an implementation of the Google Assistant API wrapped as an Alexa skill.

Alexa Skill - UK Public Transport
Intermediate
  • 159
  • 3

Full instructions

Alexa Skill to plan trips with different mode of public transportation and know more about train stations it in UK.

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login