Programmatically send Sametime IM

Hi all,

I’ve had a look on this forum and the web and haven’t heard any mention of programmatically sending Instant Messages from within databases. I’m looking for the Instant messaging @MailSend() equivalent. I’ve read a bit about bots but am hoping that there’s a more straightforward way of sending a user an IM using formula or LotusScript when a button is pressed.

Any advice would be greatly appreciated,

Regards,

Niall

Subject: Try this

I had to piece this together from other posts/examples but now I am able to send IM messages from lotusScript. The first bit of code is a simple lotusScript Agent that uses the STAgent.java class i included below.

// Paste this into a Notes Agent

Option Public

Use “STAgent”

Uselsx “*javacon”

Sub Initialize

Set JavaSession = New JavaSession

Dim STAgent As JavaClass



Dim mySTAgent As JavaObject

Set STAgent = JavaSession.GetClass("STAgent")

Set mySTAgent = STAgent.CreateObject



Call mySTAgent.stLogin("sametime.xx.com", "username", "password")

Call mySTAgent.stSendMessage("bettita", "The current time is " & Now)

Call mySTAgent.stLogout()

End Sub

// End Notes Agent

//

//Paste this into a Java Script Library

//Don’t forget to import STComm.jar - Edit Project button in Java Script Library

/*

  • STAgent.java

  • Created on July 3, 2006, 10:31 AM

  • To change this template, choose Tools | Template Manager

  • and open the template in the editor.

*/

//package userstatus;

//import lotus.domino.*;

import com.lotus.sametime.announcement.*;

import com.lotus.sametime.awareness.*;

import com.lotus.sametime.community.*;

import com.lotus.sametime.core.comparch.*;

import com.lotus.sametime.core.constants.*;

import com.lotus.sametime.core.types.*;

import com.lotus.sametime.lookup.*;

/**

  • @author BETTITA

*/

public class STAgent implements LoginListener , StatusListener, ResolveListener {

private STSession m_session;

private CommunityService m_communityService;

private LookupService m_lookupService;

private AnnouncementService m_announcementService;

private AwarenessService m_awarenessService;

private Resolver m_resolver;

private WatchList m_watchList;

private STObject m_requestedName;



private String m_strUserName;

private String m_strAnnouncement;

private short m_shAnnouncementStatus;

private boolean m_bSetup;

private boolean m_bResolved;

private boolean m_bFinished;



private String servername;

private String userid;

private String password;





/** Creates a new instance of STAgentNew */

public STAgent() {

}



public short stSendMessage(String User, String Announcement) {

    m_strUserName = User;

    m_strAnnouncement = Announcement;

    

    stResolve();

    return m_shAnnouncementStatus;

}



public void stLogout() {

    

    try {

        Thread.sleep(2000);

    } catch (InterruptedException ie) {

        ie.printStackTrace();

    }

    

    m_watchList.removeStatusListener(this);

    m_watchList.close();

    

    m_communityService.logout();

    boolean logoutExpired = false;

    int logoutAttempts = 0;

    

    do

    {

        if(++logoutAttempts > 1000) {

            logoutExpired = true;

        }

        

        try {

            Thread.sleep(5);

        } catch (InterruptedException ie) {

            ie.printStackTrace();

        }

    }

    while (isLoggedIn() == true && logoutExpired == false);

    

    m_session.stop();

    m_session.unloadSession();

}



public short stLogin(String servername, String userid, String password) {

// m_strUserName = User;

// m_strAnnouncement = Announcement;

    this.servername = servername;

    this.userid = userid;

    this.password = password;

    

    m_bSetup = false;

    

    try {

        m_session = new STSession("AnouncementSender" + this);

    } catch (DuplicateObjectException doe) {

        doe.printStackTrace();

        return -1;

    }

    

    m_session.loadSemanticComponents();

    m_session.start();

    

    m_communityService = (CommunityService) m_session.getCompApi(CommunityService.COMP_NAME);

    m_lookupService = (LookupService) m_session.getCompApi(LookupService.COMP_NAME);

    login();

    

    int setupAttempts = 0;

    do

    {

        if(++setupAttempts > 5000) {

            System.err.println("Setup attempt timed out");

            m_bSetup = true;

        }

        

        try {

            Thread.sleep(5);

        } catch (InterruptedException ie) {

            ie.printStackTrace();

        }

    }

    while (m_bSetup == false);

    

    if(isLoggedIn() == false) {

        System.err.println("Error: cannot login to Sametime server");

        return -1;

    }

    

    //     resolve();

    

    return m_shAnnouncementStatus;

}



private void login() {

    m_communityService.addLoginListener(this);

    m_communityService.loginByPassword(servername, userid, password);

    

    int loginAttempts = 0;

    boolean loginExpired = false;

    do

    {

        if (++loginAttempts > 10000) {

            loginExpired = true;

        }

        

        try {

            Thread.sleep(5);

        } catch (InterruptedException ie) {

            ie.printStackTrace();

        }

    }

    while (isLoggedIn() == false && loginExpired == false);

    

    if(isLoggedIn() == false) {

        m_session.stop();

        m_session.unloadSession();

        m_bSetup = true;

    }

}



private boolean isLoggedIn() {

    if(m_communityService == null) {

        return false;

    }

    return m_communityService.isLoggedIn();

}



public void loggedIn(LoginEvent event) {

    //System.out.println("loggedIn()");

    m_resolver = m_lookupService.createResolver(false, false, true, true);

    m_awarenessService = (AwarenessService) m_session.getCompApi(AwarenessService.COMP_NAME);

    m_watchList = m_awarenessService.createWatchList();

    m_watchList.addStatusListener(this);

    m_announcementService = (AnnouncementService) m_session.getCompApi(AnnouncementService.COMP_NAME);

    m_bSetup = true;

}



public void loggedOut(LoginEvent event) {

    //System.err.println("UserStatus loggedOut: " + event.getReason());

}



private void stResolve() {

    m_bResolved = false;

    m_bFinished = false;

    m_resolver.addResolveListener(this);

    m_resolver.resolve(m_strUserName);

    

    int resolveAttempts = 0;

    do

    {

        if(++resolveAttempts > 1000) {

            m_bResolved = true;

        }

        

        try {

            Thread.sleep(5);

        } catch (InterruptedException ie) {

            ie.printStackTrace();

        }

    }

    while (m_bResolved == false);

    

    m_resolver.removeResolveListener(this);

    

    

    int finishAttempts = 0;

    

    do

    {

        if(++finishAttempts > 1000) {

            m_bFinished = true;

        }

        

        try {

            Thread.sleep(5);

        } catch (InterruptedException ie) {

            ie.printStackTrace();

        }

    }

    while (m_bFinished == false);

}



public void resolved(ResolveEvent event) {

    System.out.println("resolved()");

    if(event.getName().compareTo(m_strUserName) == 0) {

        m_bResolved = true;

        m_requestedName = event.getResolved();

        STObject stObject[] = {m_requestedName};

        m_announcementService.sendAnnouncement(stObject, false, m_strAnnouncement);

        m_shAnnouncementStatus = 0;

        m_bFinished = true;

        

        

    }

}



public void resolveConflict(ResolveEvent event) {

    System.out.println("resolveConflict()");

    if(event.getName().compareTo(m_strUserName) == 0) {

        m_bResolved = true;

        m_shAnnouncementStatus = -1;

        m_bFinished = true;

    }

}



public void resolveFailed(ResolveEvent event) {

    System.out.println("resolveFailed()");

    String failedNames[] = event.getFailedNames();

    System.out.println(event.getReason());

    if(failedNames[0].compareTo(m_strUserName) == 0) {

        m_bResolved = true;

        m_shAnnouncementStatus = -1;

        m_bFinished = true;

    }

}



public void groupCleared(StatusEvent event) {

}



public void userStatusChanged(StatusEvent event) {

    System.out.println("userStatusChanged()");

// STWatchedUser watchedUser = event.getWatchedUsers();

// short userStatus = watchedUser[0].getStatus().getStatusType();

// if(userStatus == 32) {

    STObject stObject[] = {m_requestedName};

    m_announcementService.sendAnnouncement(stObject, false, m_strAnnouncement);

    m_shAnnouncementStatus = 0;

    m_bFinished = true;

    //      } else {

    //        m_shAnnouncementStatus = -1;

    //m_bFinished = true;

    //  }

}

}

Subject: Programmatically send Sametime IM

Hi Niall,to get Sametime “under control” you need to use the Sametime toolkit(s). A good introduction can be found here:

http://www-10.lotus.com/ldd/today.nsf/Lookup/ST_toolkits

You also might ask the IM guru Cark Tyler: http://www.instant-tech.com/blogs/ctyler.nsf and check out the Instant Tech agent framework.

Hth

:wink: stw

Subject: RE: Programmatically send Sametime IM

Stephan,

Thanks for your prompt response. I had a look at the site about Sametime Toolkits but amn’t sure that the solution to my problem lies therein. I’ll continue reading.

Has anybody else tried to programmatically send Instant messages from databases in the same way as the @MailSend() sends a mail? It seems like a very obvious and powerful application of Instant Messaging.

Thanks in advance,

Niall

Subject: RE: Programmatically send Sametime IM

FORMULA LANGUAGE

SendInstantMessage @Command

Starts a chat with one or more users.

Note This @command is new with Release 6.5.

Syntax

@Command( [SendInstantMessage] ; names )

Parameters

names

Text list. Optional. The name or names to start chatting with. If this parameter is omitted, the user is prompted.

See Also

@GetIMContactListGroupNames

AddToIMContactList @Command

ShowHideIMContactList @Command

Subject: Programmatically send Sametime IM

You can do this using solution from Botstation.Using their script library you can send Sametime messages from both LotusScript and Java agents. To send from Formula code, you can trigger a LotusScript agent.

simple example:

result = STAgent.login(“stserver.company.com”, “Notification Agent”, “password”)

result = STAgent.sendMessage(“John Doe”, “test message”)

result= STAgent.logout()