Shoutbox

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

Help with some code by Jimbo on 10-02-2006 at 05:21 PM

At the moment i got this:

code:
function OnEventMessenger.Signin
{
chatwnd.sendmessage('hi')
{
{



i want it to send a message to all online contacts when i sign in. Why isn't it working
RE: Help with some code by saralk on 10-02-2006 at 05:44 PM

Firstly the function is OnEvent_Signin()

Secondly, ChatWnd.SendMessage only sends to the currently active window. I'm not going to write the code for you, but it'd be pretty simple to do.

code:
OnEvent_Signin(Email) {
//code
}

you would need to make the code iterate through every contact using Messenger.MyContacts

Actually, change of mind, i'll write it for you, but i wont test it or anything...

code:
OnEvent_Signin(Email) {
Debug.Trace("Contacts in the user's contact list:");
var Contacts = Messenger.MyContacts;
var e = new Enumerator(Contacts);
for(; !e.atEnd(); e.moveNext())
{
    var Contact = e.item();
    if (Contact.Status != 1) {
                    Messenger.OpenChat(Contact);
                    ChatWnd.SendMessage("MESSAGE");
         }
}
}

P.S. This is pretty basic stuff, most of the code was in the help files. I reccomend you a) look at the help files before asking for help and b) get a book teaching you javascript.
RE: RE: Help with some code by Jimbo on 10-02-2006 at 05:46 PM

What do you mean by this:

code:
OnEvent_Signin(Email) {
Debug.Trace("Contacts in the user's contact list:");
var Contacts = Messenger.MyContacts;
var e = new Enumerator(Contacts);
for(; !e.atEnd(); e.moveNext())
{
    var Contact = e.item();
    if (Contact.Status != 1) {
                    Messenger.OpenChat(Contact);
                    ChatWnd.SendMessage("MESSAGE");
         }
}
}

RE: Help with some code by saralk on 10-02-2006 at 05:51 PM

I just copied a load of the code from the built in help files, and so some of that code is left over.

Debug.Trace is a way of putting the files in the debugging box. Which you can activate by checking the option box from the scripts menu.
Developers use the trace as an easy way of finding out what is wrong with their code, for example if a bit of code such as

code:
if (Var1 == Var2) { ... }
didn't work, the developer might put
code:
Debug.Trace(Var1); Debug.Trace(Var2);
so they can find out if the variables are indeed the same.
RE: Help with some code by Jimbo on 10-02-2006 at 05:53 PM

Oh, ok. Thanks Saralk
EDIT: One last quick question, can scripts check if a certain person is online ans if they are, it automatically sends a message to them?


RE: Help with some code by deAd on 10-02-2006 at 06:09 PM

134jimbodude, you can. Off the top of my head:

code:
var sEmail = "email@address.com";
var oContact = Messenger.MyContacts.GetContact(sEmail);
if(oContact.Status != 1){
                    var ChatWnd = Messenger.OpenChat(sEmail);
                    ChatWnd.SendMessage("hi");
}

quote:
Originally posted by saralk
Secondly, ChatWnd.SendMessage only sends to the currently active window.
No. It sends a message to whatever window the variable "ChatWnd" is assigned to.

quote:
Originally posted by saralk
get a book teaching you javascript.
Plus! uses JScript. JScript is not javascript. You can find the JScript documentation on MSDN.
RE: Help with some code by CookieRevised on 10-02-2006 at 06:11 PM

quote:
Originally posted by saralk
code:
OnEvent_Signin(Email) {
Debug.Trace("Contacts in the user's contact list:");
var Contacts = Messenger.MyContacts;
var e = new Enumerator(Contacts);
for(; !e.atEnd(); e.moveNext())
{
    var Contact = e.item();
    if (Contact.Status != 1) {
                    Messenger.OpenChat(Contact);
                    ChatWnd.SendMessage("MESSAGE");
         }
}
}

correction:
code:
OnEvent_Signin(Email) {
Debug.Trace("Contacts in the user's contact list:");
var Contacts = Messenger.MyContacts;
var e = new Enumerator(Contacts);
for(; !e.atEnd(); e.moveNext())
{
    var Contact = e.item();
    if (Contact.Status != 1) {
                    var ChatWnd = Messenger.OpenChat(Contact);
                    if (ChatWnd.EditChangeAllowed) ChatWnd.SendMessage("MESSAGE");
       }
}
}

quote:
Originally posted by 134jimbodude
Oh, ok. Thanks Saralk
EDIT: One last quick question, can scripts check if a certain person is online ans if they are, it automatically sends a message to them?
yes

what saralk's code does is:

1) get the object which holds all your contacts (var Contacts = Messenger.MyContacts;)

2) iterate thru each contact (enumeration and the for loop) and check if they are online (if (Contact.Status != 1))

3) open a chat window (Messenger.OpenChat(Contact);)

4) send the message (ChatWnd.SendMessage("MESSAGE");)

So instead of only checking all your contacts if they are online or not, also check the email of the contact you currently are iterating and only send the message if it is the one you need.

Another (shorter) way is to do what dead showed. This is actually turning it around and instead of checking all the contacts, directly look up the contact you need.
RE: Help with some code by Jimbo on 10-02-2006 at 06:13 PM

But i want it to only work for certain contacts if they are online, not all contacts, is this possible

quote:
Originally posted by CookieRevised

so instead of messaging all the online contacts, also check the email the the contact object and only send the message if it is the one you need.

How would i do this?
RE: Help with some code by saralk on 10-02-2006 at 06:14 PM

quote:
Originally posted by deAd
Plus! uses JScript. JScript is not javascript. You can find the JScript documentation on MSDN.

Well you learn something new every day (I tought myself JScript using the examples from the Plus! documentation)

quote:
Originally posted by 134jimbodude
But i want it to only work for certain contacts if they are online, not all contacts, is this possible

Do you mean if a certain contact from a prespecified list?
RE: Help with some code by deAd on 10-02-2006 at 06:15 PM

Cookie, I believe 134jimbodude wants to check for a specific contact instead of all of them. The code I posted above should do that. Instead of looping through them, when you know the email it is better to just find that one exactly.
EDIT: you already updated your post :sad:

Also, saralk's code will not work. the ChatWnd variable is undefined and the message will not be sent...(just add "var ChatWnd = " before the OpenChat part and it'll work again)


RE: Help with some code by Jimbo on 10-02-2006 at 06:17 PM

quote:
Originally posted by deAd

code:
var sEmail = "email@address.com";
var oContact = Messenger.MyContacts.GetContact(sEmail);
if(oContact.Status != 1){
                    var ChatWnd = Messenger.OpenChat(sEmail);
                    ChatWnd.SendMessage("hi");
}


Thanks deAd
EDIT: Just added the coded and it sent 'hi' to my contact. Is there a way for it to only do it as a sign in?
Also whenever i go to plus preferences and click ok it sends 'hi' again:'( any way of stoppin this?
RE: Help with some code by deAd on 10-02-2006 at 06:21 PM

Sure.

code:
function OnEvent_Signin(sUserEmail){
var sEmail = "email@address.com";
var oContact = Messenger.MyContacts.GetContact(sEmail);
if(oContact.Status != 1){
var ChatWnd = Messenger.OpenChat(sEmail);
ChatWnd.SendMessage("hi");
}
}

RE: Help with some code by CookieRevised on 10-02-2006 at 06:21 PM

too many people replying at the same time.... :dodgy:

Dead, even that code is not correct (or to put it in another way: shouldn't be used)...

corrected:

code:
var sEmail = "email@address.com";
var oContact = Messenger.MyContacts.GetContact(sEmail);
if (oContact.Status != 1) {
                    var ChatWnd = Messenger.OpenChat(sEmail);
                    if (ChatWnd.EditChangeAllowed) ChatWnd.SendMessage("hi");
}


@134jimbodude: I'd suggest to really take up the advise and get a book about basic programming as the questions you ask are extremely basic programming things though.

Also be advised that it is highly annoying for your contacts to recieve a message from you each time you log in (the same goes for those auto-greeting scripts which greet every person as soon as someone signs in).
RE: Help with some code by deAd on 10-02-2006 at 06:23 PM

Cookie, that is just a check, and is not required. The same result will be produced if you do not add that if statement. Plus will not send the message if it can't.


RE: Help with some code by Jimbo on 10-02-2006 at 06:24 PM

So to put more emails in would i just do:

code:
function OnEvent_Signin(sUserEmail){
var sEmail = "email@address.com;email@adress2.com";
var oContact = Messenger.MyContacts.GetContact(sEmail);
if(oContact.Status != 1){
                    var ChatWnd = Messenger.OpenChat(sEmail);
                    ChatWnd.SendMessage("hi");
}
}

RE: Help with some code by saralk on 10-02-2006 at 06:25 PM

I feel like an idiot now. My code is nothing like what you actually wanted :(


RE: Help with some code by CookieRevised on 10-02-2006 at 06:25 PM

quote:
Originally posted by deAd
Cookie, that is just a check, and is not required. The same result will be produced if you do not add that if statement. Plus will not send the message if it can't.
That is not 'just' a check, it is actually very required as it is not always possible to send a message to a chat window (even if you just opened it).

quote:
Originally posted by saralk
I feel like an idiot now. My code is nothing like what you actually wanted :(
don't be, he first asked how to send a message to all online contacts which you indeed provided.

quote:
Originally posted by 134jimbodude
So to put more emails in would i just do:
code:
function OnEvent_Signin(sUserEmail){
var sEmail = "email@address.com;email@adress2.com";
var oContact = Messenger.MyContacts.GetContact(sEmail);
if(oContact.Status != 1){
                    var ChatWnd = Messenger.OpenChat(sEmail);
                    ChatWnd.SendMessage("hi");
}
}

no, absolutely not...

the variable sEmail is a string, it will be compared literally to the string which holds the email of the current contact object. Or in this specific code it will be passed to the GetContact function as the email to find.

"email@address.com;email@adress2.com" is not a valid email address and thus this will never work.

What you want is checking the email of the current contact object against a list or in other words against an array...

PS: make up your mind though, each time you request something different, which requires different code :p;)
RE: Help with some code by deAd on 10-02-2006 at 06:27 PM

quote:
Originally posted by 134jimbodude
So to put more emails in would i just do:
No. You'd need an array of emails to loop through.
code:
function OnEvent_Signin(sUserEmail){
var sEmails = new Array("email@address.com","email2@address.com");
for(item in sEmails){
var oContact = Messenger.MyContacts.GetContact(sEmails[item]);
if(oContact.Status != 1){
var ChatWnd = Messenger.OpenChat(sEmails[item]);
ChatWnd.SendMessage("hi");
}
}
}


quote:
Originally posted by CookieRevised
quote:
Originally posted by deAd
Cookie, that is just a check, and is not required. The same result will be produced if you do not add that if statement. Plus will not send the message if it can't.
That is not 'just' a check, it is actually very required as it is not always possible to send a message to a chat window (even if you just opened it).
If you do not include this check, the message will not be sent. If you do include the check, the message won't be sent. Either way, the message will be sent only when it can be. I don't see the necessity of it :S it's not like it causes problems or something?
RE: Help with some code by Jimbo on 10-02-2006 at 06:27 PM

so everyone would this work:

code:
function OnEvent_Signin(sUserEmail){
var sEmails = new Array("bla@bla2.com","bla@bla3.com");
for(item in sEmails){
var oContact = Messenger.MyContacts.GetContact(sEmails[item]);
if(oContact.Status != 1){
var ChatWnd = Messenger.OpenChat(sEmails[item]);
ChatWnd.SendMessage("hi");
}
}
}


RE: Help with some code by saralk on 10-02-2006 at 06:28 PM

quote:
Originally posted by 134jimbodude
so everyone would this work:
code:
function OnEvent_Signin(sUserEmail){
var sEmail = "bla@bla1.com;bla@bla2.com;bla@bla3.com";
var oContact = Messenger.MyContacts.GetContact(sEmail);
if(oContact.Status != 1){
var ChatWnd = Messenger.OpenChat(sEmail);
if (ChatWnd.EditChangeAllowed) ChatWnd.SendMessage("hi");
}
}



No, that requires a bit more thought, i'm working on it now.
RE: Help with some code by deAd on 10-02-2006 at 06:30 PM

See my code above :)

EDIT: here, reposted:

code:
function OnEvent_Signin(sUserEmail){
var sEmails = new Array("email@address.com","email2@address.com");
for(item in sEmails){
var oContact = Messenger.MyContacts.GetContact(sEmails[item]);
if(oContact.Status != 1){
var ChatWnd = Messenger.OpenChat(sEmails[item]);
ChatWnd.SendMessage("hi");
}
}
}

RE: Help with some code by Jimbo on 10-02-2006 at 06:31 PM

Thanks so much for everyones help


RE: RE: Help with some code by CookieRevised on 10-02-2006 at 06:31 PM

quote:
Originally posted by deAd
quote:
Originally posted by CookieRevised
quote:
Originally posted by deAd
Cookie, that is just a check, and is not required. The same result will be produced if you do not add that if statement. Plus will not send the message if it can't.
That is not 'just' a check, it is actually very required as it is not always possible to send a message to a chat window (even if you just opened it).
If you do not include this check, the message will not be sent. If you do include the check, the message won't be sent. Either way, the message will be sent only when it can be. I don't see the necessity of it :S it's not like it causes problems or something?
yes it will cause problems and errors.
RE: Help with some code by Jimbo on 10-02-2006 at 06:34 PM

so cookie would this work:

code:
function OnEvent_Signin(sUserEmail){
var sEmails = new Array("email@address.com","email2@address.com");
for(item in sEmails){
var oContact = Messenger.MyContacts.GetContact(sEmails[item]);
if(oContact.Status != 1){
var ChatWnd = Messenger.OpenChat(sEmail[item]);
                    if (ChatWnd.EditChangeAllowed) ChatWnd.SendMessage("hi");
}
}
}


RE: Help with some code by deAd on 10-02-2006 at 06:36 PM

Almost, there's a typo in it:

code:
function OnEvent_Signin(sUserEmail){
var sEmails = new Array("email@address.com","email2@address.com");
for(item in sEmails){
var oContact = Messenger.MyContacts.GetContact(sEmails[item]);
if(oContact.Status != 1){
var ChatWnd = Messenger.OpenChat(sEmails[item]);
                    if (ChatWnd.EditChangeAllowed) ChatWnd.SendMessage("hi");
}
}
}

RE: Help with some code by CookieRevised on 10-02-2006 at 06:38 PM

yep

(note that dead's code will work too in most cases, but it will produce errors in the (rare) cases that you can't send a message to the chatwindow; although you wont notice these errors as a user)


RE: Help with some code by Jimbo on 10-02-2006 at 06:40 PM

Thanks again everyone