Shoutbox

Help with code - 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: Help with code (/showthread.php?tid=87079)

Help with code by Jonte135 on 11-04-2008 at 07:38 PM

Me, with help of my friend, made this code (mainly my friend :P) and I want it to react on words that I have in a file. The problem is that it responds to anything now. In the file I have:

quote:
hi
hello
hey
howdy

My friend also told me that I should but the code in functions, I know what functions are and how they work but I don't really understand how I should do it. Help please? :D

JScript code:
var curWord = 0;
 
function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, Message, MessageKind) {
    if(Origin != Messenger.MyName) {
    var parts = Message.split(" ");
    curWord = 0;
        while(1) {
       
         var ofr, ofr1, ofr2, a1, f1, txt; //ofr = open file read
            var ForReading = 1;
            ofr = new ActiveXObject("Scripting.FileSystemObject");
            ofr1 = ofr.OpenTextFile("C:\\Program\\Messenger Plus! Live\\Scripts\\B.O.T._ Being Optimized for Troubleshooting\\text.txt",ForReading);
            ofr2 = ofr1.ReadAll();
            ofr1.Close();
            var i;
            var Message = " ";
            while (i<10) {
        Message = ofr2.split("\r\n");
            }
        if(parts[curWord] == Message[i]){
     
        var ofa, ofa1, ofa2, a1, f1, txt; //ofa = open file answer
            var ForReading = 1;
            ofa = new ActiveXObject("Scripting.FileSystemObject");
            ofa1 = ofa.OpenTextFile("C:\\Program\\Messenger Plus! Live\\Scripts\\B.O.T._ Being Optimized for Troubleshooting\\text.txt",ForReading);
            ofa2 = ofa1.ReadAll();
            ofa1.Close();
           
            var sendMessage = " ";
        sendMessage = ofa2.split("\r\n");
           ChatWnd.SendMessage(sendMessage[Math.round(Math.random()%4)]);
        ofa2 = 0;
            break;
        }
       
        if(curWord == 150) {
            break;
        }
        curWord += 1;
    }
    }
} //function OnEvent


RE: Help with code by SmokingCookie on 11-04-2008 at 07:43 PM

First, why do you use the long path in OpenTextFile(), while you can also use MsgPlus.ScriptFilesPath + "\\text.txt"?

Second.. Give me a couple of seconds and I'll try to figure something out..

I think you're doing things a bit too complicated..

[See last post]


RE: Help with code by Jonte135 on 11-05-2008 at 06:33 PM

It works, just now when someone says anything it replies :|


RE: Help with code by SmokingCookie on 11-05-2008 at 06:50 PM

Sorry for prev. post, forgot to read :P

I'm working on something for you ;)
See my first post


RE: Help with code by roflmao456 on 11-05-2008 at 06:57 PM

iirc, activexobjects aren't recommended to be defined as a global variable
try this (looking at your code..):):

JScript code:
function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, Message, MsgKind){
    if(Origin!=Messenger.MyName){
        var file=new ActiveXObject("Scripting.FileSystemObject").OpenTextFile(MsgPlus.ScriptFilesPath+"\\text.txt",1,1);
        var text=file.ReadAll().split("\r\n");
        file.close();
        for(i in text)if(new RegExp(text[i],"i").test(Message)){
            ChatWnd.SendMessage(text[Math.floor(Math.random()*text.length)]);
            break;
            }
     }
}


RE: Help with code by Jonte135 on 11-05-2008 at 07:00 PM

Now it doesn't work at all :S


RE: Help with code by SmokingCookie on 11-05-2008 at 07:30 PM

Got it!

[last post]


RE: Help with code by Jonte135 on 11-06-2008 at 07:20 PM

Now it only does it once, if I don't close the window, I want it to reply everytime :o


RE: Help with code by SmokingCookie on 11-06-2008 at 07:42 PM

Post updated ;)


RE: Help with code by Jonte135 on 11-06-2008 at 07:49 PM

It worked most of the time, but when the friend that tested it said this:

quote:
I would need some sort of software *on* your computer, that was (1) running, (2) not being blocked by some AV or Firewall, and (3) able to send me screenshots or something like that of your desktop
it responded :| It shouldn't do that :(
Now it doesn't respond at all :|
RE: Help with code by SmokingCookie on 11-06-2008 at 08:16 PM

That;s what I meant with the last sentence

LOL works too well: I'm signed in on 2 accounts, I send "hello" to myself and the script keeps responding :P Don't try this at home :P

Updated code:
[see last post]


RE: Help with code by Jonte135 on 11-06-2008 at 08:53 PM

It seems to work, is there anyway to put that code into functions so I can easily do the same thing over and over but with a different text file? And how does

quote:
/* devide by number of lines */
work? :s
RE: Help with code by SmokingCookie on 11-06-2008 at 08:59 PM

Yes you can:

JScript code:
function myFunc /* Function's name */ (Argument /* I believe no. of arguments is unlimited. Can be anything */ ) {
      Debug.Trace("Hello World!");
      Debug.Trace("  The argument was: " + Argument);
}
 
function OnEvent_Initialize(bMgStart) {
      myFunc("HI there!");
}


Now I can imagine you'd want to use the current user's email as file name right?


RE: Help with code by Jonte135 on 11-06-2008 at 09:02 PM

Thanks :) I will reply if I have some other problems later :)


RE: Help with code by Jonte135 on 11-07-2008 at 04:04 PM

Found a problem, if someone says like "oh hi" it doesn't respond. I want it to react if it's in a sentence too :)


RE: Help with code by SmokingCookie on 11-07-2008 at 05:27 PM

Then you should put "oh hi" in the list as well, as the script can not think like "this sentence is a greeting, but the other one is not". I'm sorry..


RE: Help with code by matty on 11-07-2008 at 06:52 PM

I don't understand some of your logic. Why would you read the entire file then seperate it by line when you can do this

JScript code:
function ReadFile(File) {
    var oLineArray = new Array();
    var oFile = FileSys.OpenTextFile(File, 1 /* ForReading */, false);
    while (!oFile.AtEndOfStream) oLineArray.push(oFile.ReadLine());
    return oLineArray;
}


RE: RE: Help with code by Jonte135 on 11-07-2008 at 11:53 PM

quote:
Originally posted by SmokingCookie
Then you should put "oh hi" in the list as well, as the script can not think like "this sentence is a greeting, but the other one is not". I'm sorry..
I mean that the code itself will look if there is, for example, hi in something that it receives. So "hi" is the same for the code as "bla bla bla bla bla hi bla bla bla bla" because it looks for "hi".

EDIT: It also has some kind of delay when I talk to my bot (:P). But then when, because I have the same script, it goes on it answers immediately. I want it to do immediately all the time.
RE: Help with code by SmokingCookie on 11-08-2008 at 02:40 PM

I'm afraid the computers do not yet allow to do it all in a split-second: they're not fast enough to check if a file exists, read (or create) it, create an array of text, close the file again, check for matches and return a random number, rounded and between 0 and the length of the array. That's a lot.

Second: if I let the script respond to "bla bla bla bla bla hi bla bla bla bla", it'll also respond to "hightlight", "high" and such.


RE: Help with code by Jonte135 on 11-08-2008 at 07:30 PM

How does other scripts do then? Some downloadable scripts have so it answers immediately. And also isn't it possible to separate words like it will only respond to "hi" as in " hi "?


RE: Help with code by SmokingCookie on 11-08-2008 at 07:51 PM

Because they load the file on startup, so you can not add any words to the file during runtime.

And the second thing: you'll have to specify " hi " in your files if you want to.

[offtopic] ooh 400 posts :o) [/offtopic]


RE: Help with code by Jonte135 on 11-08-2008 at 07:56 PM

Well the loading thingy solved itself, it was just when I talked with the bot that it took a long time.

Instead of adding every single possibility like " hi " can't you do so it will only react if it matches the first word? Nothing else.

[offtopic] Congratulations :) [/offtopic]


RE: Help with code by SmokingCookie on 11-08-2008 at 07:58 PM

I might be able to do that, but it's ben a busy day so.. |-) might take a while :P

[offtopic] Thx :) [/offtpoic]

EDIT:: Okay, I've got it, but the message must not end with a dot (.)

JScript code:
var FileSys = new ActiveXObject("Scripting.FileSystemObject");
var BasePath = MsgPlus.ScriptFilesPath + "\\"; // I like when stuff ends with a backslash
var TXTNAME = ""; // store for later use
 
function OnEvent_Initialize(bMsgStart) {
    if(!bMsgStart) {
        TXTNAME = BasePath + "text.txt";
    }
}
 
function OnEvent_ChatWndReceiveMessage(objChatWnd,strOrigin,strMessage,intMsgKind) {
    if(strOrigin != Messenger.MyName) {
        var objTextFile, strAllText, arrMessages; // variables for later usage
        if(!FileSys.FileExists(TXTNAME)) {
            objTextFile = FileSys.CreateTextFile(TXTNAME,false /* don't overwrite */,true /* Save as Unicode */ ); // If it doesn't exist, save empty file
        } else {;
            objTextFile = FileSys.OpenTextFile(TXTNAME,1 /* read */); // if it does exists, open
        }
        strAllText = objTextFile.ReadAll(); // read contents
        objTextFile.Close(); // close file
        arrMessages = strAllText.split("\r\n"); // get the array of newline characters
        var Pattern; // declare a variable
        var Words = strMessage.split(" ");
        for(var i = 0 /* declare a variable */ ; i < arrMessages.length /* make sure it's not beyond the end of the array */ ; i++ /* and increment it by 1 */ ) {
            Pattern = new RegExp(arrMessages[i],"gim"); // Global, (case) Insensitive, Multiline
            var Chunk = strMessage.substr(0,arrMessages[i].length); // Get the first chunk of text
//          var Chunk = Words[0]; // Get the first word
            if(Chunk.toLowerCase() == arrMessages[i].toLowerCase()) { /* is the beginning of the message arrMessages[i]? */
                objChatWnd.SendMessage(arrMessages[Math.round(Math.random()%(arrMessages.length - 1 /* devide by number of lines */) )]);
                return strMessage; // suspend execution
            }
        }
        return strMessage; // suspend execution
    }
}


RE: Help with code by Jonte135 on 11-08-2008 at 08:03 PM

Sure I can wait :) It will take way longer if I try to do it myself :D


RE: Help with code by SmokingCookie on 11-08-2008 at 08:07 PM

Okay, you may not have noticed it (email?), but I've updated the code again, after the first edit. Now it uses your email as text file (for eample "my@email.com.txt").


RE: Help with code by Jonte135 on 11-08-2008 at 08:14 PM

Why should I have it like that? ^o) *confused*


RE: Help with code by SmokingCookie on 11-08-2008 at 08:15 PM

To allow for multiple users :P

EDIT:: if it'd work at all (which is doesn't).. Changed back tot "text.txt" again :P


RE: Help with code by Jonte135 on 11-08-2008 at 08:18 PM

I don't get it :s


RE: Help with code by SmokingCookie on 11-08-2008 at 08:35 PM

Never mind, it works now. :P

What the script does:

  • Read the file Text.txt
  • Searches for all the nearest match of the current word (done in a loop), in a string from the beginning of the received message, all the way to the last character position of the current word in the loop ("hello" means char 1 to 5)
  • Sends a random message from the same file
  • Returns the received message to break the loop and suspend execution

Make sure you're not on 2 accounts at the same time. Sending "hello" will result in the script responding to its own messages. Your PC will not be made any faster by this..
RE: RE: Help with code by Jonte135 on 11-08-2008 at 09:10 PM

quote:
Originally posted by SmokingCookie
Your PC will not be made any faster by this..
Lol.

Thanks a lot! Do you know if it's possible to have different script settings for different users if you are logged in at the same time with two accounts?
RE: Help with code by SmokingCookie on 11-08-2008 at 09:13 PM

That is a lot of work, it's 22.13 over here.. Not today I'm afraid :P


RE: RE: Help with code by Jonte135 on 11-08-2008 at 09:15 PM

quote:
Originally posted by SmokingCookie
That is a lot of work, it's 22.13 over here.. Not today I'm afraid :P
Ya but do you know if it's possible through MSN+ itself?
RE: Help with code by Jonte135 on 11-16-2008 at 06:41 PM

Isn't it possible to just do like: "if email = [the bots email] { do code bla bla bla } else { do nothing }" ^o)

EDIT: It still replys when I say, for example, hit. The odd thing is that it only does it sometimes *-)


RE: Help with code by SmokingCookie on 11-23-2008 at 10:14 AM

It probably only does, when "hit" is the very first word of the sentence..

I do not have much time atm, I'll try something later today ;)


RE: RE: Help with code by Jonte135 on 11-23-2008 at 11:21 AM

quote:
Originally posted by SmokingCookie
It probably only does, when "hit" is the very first word of the sentence..

I do not have much time atm, I'll try something later today ;)
Ok, well take the time you need. I thought you forgot the thread that's all ;)