Shoutbox

Need Help with my First Script - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Need Help with my First Script (/showthread.php?tid=93004)

Need Help with my First Script by SamV522 on 11-24-2009 at 04:54 PM

Okay, so this is my first time creating a Messenger Plus! script though I do have quite a bit of a programming/scripting background.

Anyway, I'm trying to write an AI, or more so an autoresponder that uses an xml list of responses for personal use and I've got it just about all set up, I just need to add a few things here and there.

The problem is, when I log into MSN, MSN crashes... I'm not sure what I've done wrong, but I think it's "ActiveXObject("Microsoft.XMLDOM")" on line 8.  Though I'm not sure what to replace this with, can anyone help me with this?
Here is the full source: http://msnplus.pastebin.com/ma78e704


RE: Need Help with my First Script by SourSpud on 11-24-2009 at 04:58 PM

I'm still learning myself but why do you have an empty function?

JScript code:
function OnEvent_Uninitialize(MessengerExit)
{
}


also try signing in as 'away' see if it still crashes.
RE: Need Help with my First Script by SamV522 on 11-24-2009 at 05:00 PM

It was put there by default when I started a new script.  I hadn't gotten around to deleteing it yet, I was searching for a way to fix my problem.


RE: Need Help with my First Script by SourSpud on 11-24-2009 at 05:02 PM

try signing in as Away still if msn still crashes.


RE: Need Help with my First Script by SamV522 on 11-24-2009 at 05:12 PM

It's not crashing when I sign in anymore, but it's crashing whenever I recieve a message that needs to be responded to.


RE: Need Help with my First Script by CookieRevised on 11-24-2009 at 05:14 PM

I don't have much time to debug it, but here are a few things which I can say about good practice:

1) Your status doesn't have anything to do with it.

2) Don't use the OnEvent_Initialize() function to read in the XML. As you can read in the scripting documentation, the OnEvent_Initialize() function should return as quickly as possible and is not a good place to do process intensive tasks.

Instead use the OnEvent_SigninReady() function to do that. It also makes more sense because if you are never going to sign in, it is useless to read in the XML and set things up.

3) Before using ChatWnd.SendMessage you should always check if you can actually send a message by using ChatWnd::EditChangeAllowed. Again, see official scripting documentation.

Now, the above pointers don't realy fix anything. But they will prevent possible problems in the long run.

4) There is probably something wrong with the ActiveX object you use. Try to get some documentation about it and study it carefully.


RE: Need Help with my First Script by SamV522 on 11-24-2009 at 05:43 PM

Okay, I did what you said CookieRevised, and it seem's to be working ok now.  I just need to get the responses working now.

Thanks for all your help! :)


RE: Need Help with my First Script by SamV522 on 11-24-2009 at 06:10 PM

Ok. So I've tried quite a few different things with sending a response, but it crashes whenever I try and send a message...
What am I doing wrong?

code:
function OnEvent_ChatWndSendMessage(ChatWnd,Origin, Message, MessageKind){
    if (Message.substr(0,6).toLowerCase() == "/AwayI"){
        var param = Message.substr(7);
        switch(param){
            case "enable":
                AI.State = true;
                break;
            case "disable":
                AI.State = false;
                break;
            case "commands":
                alert("Enable\nDisable\nCommands");
                break;
            /*default:
                alert("Invalid Command! Type \"/AwayI Commands\" to see a list of valid commands");
                break;
                */
        }
    }
}


RE: Need Help with my First Script by matty on 11-24-2009 at 06:20 PM

Alert cannot be used the scripting engine.


RE: Need Help with my First Script by SamV522 on 11-24-2009 at 06:24 PM

Is that the only thing?

Edit:
I guess not because it's still crashing.


RE: Need Help with my First Script by Spunky on 11-24-2009 at 06:25 PM

quote:
Originally posted by SamV522
Ok. So I've tried quite a few different things with sending a response, but it crashes whenever I try and send a message...
What am I doing wrong?
code:
function OnEvent_ChatWndSendMessage(ChatWnd,Origin, Message, MessageKind){
    if (Message.substr(0,6).toLowerCase() == "/AwayI"){
        var param = Message.substr(7);
        switch(param){
            case "enable":
                AI.State = true;
                break;
            case "disable":
                AI.State = false;
                break;
            case "commands":
                alert("Enable\nDisable\nCommands");
                break;
            /*default:
                alert("Invalid Command! Type \"/AwayI Commands\" to see a list of valid commands");
                break;
                */
        }
    }
}



Unless alert is your own custom function, it will fail. MP!L doesn't have an alert function. You can call a msgbox by using the relevant Win32 APIs or you can use Debug.Print to see it in the debug area. It shouldn't crash though, unless you just mean theres an error...

EDIT: Too slow, had to read other tabs first ¬_¬

RE: Need Help with my First Script by SamV522 on 11-24-2009 at 06:32 PM

No, msn just plain crashes... I'll post my current source.
http://msnplus.pastebin.com/mab45fd3


RE: Need Help with my First Script by matty on 11-24-2009 at 06:53 PM

quote:
Originally posted by Spunky
Debug.Print
It is Debug.Trace not Debug.Print...

At what point does it crash? What message or command are you sending and it crashes?
RE: Need Help with my First Script by SamV522 on 11-24-2009 at 07:16 PM

It crashes whenever I send any message
And on a side note: The AwayI command doesn't work.
I added this to ScriptInfo.xml in the right place and all but whenever I try and use it it says the command is not recognised.

code:
<ScriptCommands>
    <Command>
        <Name>AwayI</Name>
        <Description>Execute an AwayI command</Description>
        <Parameters>&lt;Command&gt;</Parameters>
    </Command>
</ScriptCommands>


RE: Need Help with my First Script by matty on 11-24-2009 at 07:19 PM

Post all of your code here. use the [code=js][/code] and [code=xml][/code] tags to make it more readable.

Also you need to return ''; in the OnEvent_ChatWndSendMessage() when you are sending a custom command. Otherwise Plus! tries to parse the command and if it doesn't match one of the Plus! commands it errors out.


RE: Need Help with my First Script by SamV522 on 11-24-2009 at 07:23 PM

AwayI.js

Javascript code:
// Name: AwayI.js
// Author: SamV522
// Purpose: A.I. to respond to any messages while you're away.
 
var AI = {};
AI.Name = "Sophie";
AI.UnkownResponse = "I do not know how to respond to that, sorry.";
AI.Status = true;
AI.Responses = {};
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
 
function verify(){
    if (xmlDoc.readyState != 4){
        return false;
    }
}
 
function loadXML(xmlFile)
{
    xmlDoc.async="false";
    xmlDoc.onreadystatechange=verify;
    xmlDoc.load(xmlFile);
    xmlObj=xmlDoc.documentElement;
}
 
function OnEvent_SigninReady()
{
    if( AI.Status){
        loadXML("coms.xml");
        var In = "";
        var Out = "";
        for(i=0;i!=xmlObj.childNodes.length;i++){
            if(xmlObj.childNodes(i).childNodes(0).getAttribute("CaseSenseitive")=="false"){
                // The response IS case sensitive
                In = xmlObj.childNodes(i).childNodes(0).text;
                In = In.split(",");
                Out = xmlObj.childNodes(i).childNodes(1).text;
                Out = Out.split(",");
                for(i2=0;i2!=In.length;i2++){
                    AI.Responses[In[i2]].Resp = Out;
                    AI.Responses[In[i2]].CaseSens = false;
                }
            }else{
               
            }
        }
    }
}
 
function OnEvent_ChatWndReceiveMessage(ChatWnd,Origin, Message, MessageKind)
{
    var inMsg = Message;
    var resp = "Error!";
    for(i=0;i!=AI.Responses.length;i++){
        if(AI.Responses[Message]){
            if(AI.Responses[Message].CaseSens){
                inMsg = inMsg.toLowerCase();
            }else{
                inMsg = inMsg;
            }
           
            if(inMsg==AI.Responses[inMsg]){
                resp = AI.Responses[Message][Math.floor(Math.random()*Responses[Message].length)];
            }else{
                resp = AI.UnkownResponse;
            }
        }
        ChatWnd.SendMessage(resp);
    }
}
 
function OnEvent_ChatWndSendMessage(ChatWnd, Message){
    if (Message.substr(0,6).toLowerCase() == "/AwayI"){
        var param = Message.substr(7);
        switch(param){
            case "enable":
                AI.State = true;
                return "";
            case "disable":
                AI.State = false;
                return "";
            case "commands":
                var WindowOptions=MsgPlus.CreateWnd("Commands.xml","CommandsWND");
                return "";
            /*default:
                alert("Invalid Command! Type \"/AwayI Commands\" to see a list of valid commands");
                break;
                */

        }
    }
}
 
function OnEvent_MenuClicked(sMenuId, nLocation, ChatWnd)
{
    switch(sMenuId){
        case "enableAI":
            AI.Status = true;
            break;
        case "disableAI":
            AI.Status = false;
            break;
        case "commandList":
            var WindowOptions=MsgPlus.CreateWnd("Interfaces.xml","commandsWND");
            break;
    }
}


ScriptInfo.xml:
XML code:
<?xml version="1.0"?>
<ScriptInfo xmlns="urn:msgplus:scripts" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="1.0">
<Information>
    <Name>AwayI MSN Bot</Name>
    <Description>An MSN Bot by SamV522</Description>
    <Version>1.0</Version>
</Information>
<ScriptMenu>
    <MenuEntry Id="enableAI">Enable</MenuEntry>
    <MenuEntry Id="disableAI">Disable</MenuEntry>
    <MenuEntry Id="commandList">Commands</MenuEntry>
 </ScriptMenu>
 
<ScriptCommands>
    <Command>
        <Name>AwayI</Name>
        <Description>Execute an AwayI command</Description>
        <Parameters>&lt;Command&gt;</Parameters>
    </Command>
</ScriptCommands>
</ScriptInfo>


RE: Need Help with my First Script by SamV522 on 11-24-2009 at 07:55 PM

Well... I fixed it  it was the if(AI.Responses[Message]) statement.
I changed it just to if(!AI.Responses[Message]){ return; }


RE: Need Help with my First Script by Spunky on 11-24-2009 at 08:00 PM

quote:
Originally posted by matty
quote:
Originally posted by Spunky
Debug.Print
It is Debug.Trace not Debug.Print...

At what point does it crash? What message or command are you sending and it crashes?

erm... =(
RE: Need Help with my First Script by matty on 11-24-2009 at 08:29 PM

quote:
Originally posted by Spunky
quote:
Originally posted by matty
quote:
Originally posted by Spunky
Debug.Print
It is Debug.Trace not Debug.Print...

At what point does it crash? What message or command are you sending and it crashes?

erm... =(
Go back to VB coding :P
RE: Need Help with my First Script by CookieRevised on 11-24-2009 at 08:35 PM

Your OnEvent_ChatWndSendMessage function is wrong. For starters the main if-then-statement will never return true since you change everything to lowercase and compare it with a mixed case string. Second, you must(!) return the original message if it isn't a command of your command.

corrected:

Javascript code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message){
    if (Message.substr(0,6).toLowerCase() == "/awayi"){        var param = Message.substr(7);
        switch(param){
            case "enable":
                AI.State = true;
                return "";
            case "disable":
                AI.State = false;
                return "";
            case "commands":
                var WindowOptions=MsgPlus.CreateWnd("Commands.xml","CommandsWND");
                return "";
            /*default:
                alert("Invalid Command! Type \"/AwayI Commands\" to see a list of valid commands");
                break;
                */
        }
    } else {        return Message;    }
}


---------

In the OnEvent_ChatWndReceiveMessage function you first must check if you actually can send a message. See point 3 in my previous post.

And also, just as in OnEvent_ChatWndSendMessage, you must(!) return the original message if you're not going to change it.