Question

I have been working on a script for a while now, and have had many questions asked. I also rewrote a lot of the code as per some peoples suggestions. Yet, I still have this one question that has been unanswered. Below is the code for a "Siri" like iMessage PA. Basically, when I say commands through iMessage to it, it responds.

I have been trying desperately to make it have a bit more intelligence by having it reply in sentences. You can see in the code, when it asks me "...are you ok?" It should look for either "yes" or "no", but only when the first question as been asked. Yet currently it just reponds with the initial "I am fine sir, are you ok?" and then does nothing after that when I tell it yes or no.

Just to be sure its not the main code I have added a plain "hello world" to see if will respond normally which it does. Below is the code:

using terms from application "Messages"
    on message received theMessage from theBuddy for theChat

        if theMessage is "hello" then
            helloresponse()
        end if

        if theMessage contains "Are you ok" then
            areyouok()
            if theMessage is "yes" then
                happyResponse()
            else
                if theMessage is "no" then
                    unhappyResponse()
                end if
            end if
        end if

        if theMessage contains "hello world" then
            helloworldresponse()
        end if

    end message received

end using terms from

on helloresponse()
    tell application "Messages"
        send "Hey sir!" to buddy "email@1.com" of service "E:email@2.com"
    end tell
end helloresponse

on happyResponse()
    tell application "Messages"
        send "you said yes, yey!" to buddy "email@1.com" of service "E:email@2.com"
    end tell
end happyResponse

on unhappyResponce()
    tell application "Messages"
        send "You said no, thats a shame!" to buddy "email@1.com" of service "E:email@2.com"
    end tell
end unhappyResponce

on areyouok()
    tell application "Messages"
        send "I am fine sir, are you ok?" to buddy "email@1.com" of service "E:email@2.com"
    end tell
end areyouok

on helloworldresponse()
    tell application "Messages"
        send "World replies: Hello." to buddy "email@1.com" of service "E:email@2.com"
    end tell
end helloworldresponse

Thanks for any help people can give me!

Was it helpful?

Solution

If I am reading this right.

you get a message "Are you ok"

The script response is "I am fine sir, are you ok?"

you get a message back "yes" or "no"

But the part of the script that is waiting for the "yes" or "no" message is buried inside the if block for "I am fine sir, are you ok?". So it will never get trigged.

**UPDATE

Here is a way to test your logic.

create two scripts.

The first one will be the one with the logic. And you will run it in Applescript editor.

property lastMessage : ""

set theMessage to load script "/PATH/TO/SECOND/SCRIPT/secondScript.scpt"
run theMessage

if theMessage is "hello" then
    helloresponse()
end if

if theMessage contains "Are you ok" then

    areyouok()

end if

if theMessage is "yes" and lastMessage is "I am fine sir, are you ok?" then
    happyResponse()
else if theMessage is "no" and lastMessage is "I am fine sir, are you ok?" then
    unhappyResponse()

end if


if theMessage contains "hello world" then
    helloworldresponse()
end if




on helloresponse()
    say "Hey sir!"
end helloresponse

on happyResponse()
    say "you said yes, yey!"
    set lastMessage to ""
end happyResponse

on unhappyResponce()
    say "You said no, thats a shame!"
    set lastMessage to ""
end unhappyResponce

on areyouok()
    say "I am fine sir, are you ok?"
    set lastMessage to "I am fine sir, are you ok?"
end areyouok

on helloworldresponse()
    say "World replies: Hello."
end helloworldresponse

Take note of the property I have given it. Any variable passed into a property is stored there. And can be accessed when the script is run again later.

In this case I have set it to store the last response/message if that response was "I am fine sir, are you ok?"

I have also split your if blocks so that the happyResponse and unhappyResponse response will be triggered only with the "yes" or "no" message and as long as the last response stored in the property lastMessage is "I am fine sir, are you ok?".

In the first script is a line to load and run the second script.

The second script is used to give the messages. This script is edited and then saved.

for example the second script will just contain;

set theMessage to "Are you ok"

the second script is saved. And then you run the first script to pick up its theMessage and respond. which should be "I am fine sir, are you ok?

You then edit and save the second script with;

set theMessage to "yes"

And run the first script again. The response should be; "you said yes, yey!"

You will get the response you are expecting

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top