Respond to instant messages

From libopenmetaverse - libomv - Developer Wiki

Jump to: navigation, search

Contents

Prerequisites

The following prerequisites are required in order for you to create your first bot by following this tutorial.

Real World

  • This tutorial assumes that you were able to successfully build libsecondlife! If you have not compiled libsl, please follow the instructions on the Getting Started wikipage.
  • Also, if you have no experience with the C# (c-sharp) programming language, I highly recommend that you stop now and please follow the list of links to csharp tutorials located here.

The Code

Note: We are assuming that our main "GridClient" instance is named "Client." If you have a different name for this variable, please substitute the name accordingly

 
// put this somewhere when you want to start processing instant messages
Client.Self.IM += new EventHandler<InstantMessageEventArgs>(Self_IM);
 
//(...) then define the Self_IM method
 
static void Self_IM(object sender, InstantMessageEventArgs e)
{
      //there are a variety of InstantMessageDialog choices.. MessageFromObject and MessageFromAgent would be the two most common
      if (e.IM.Dialog == InstantMessageDialog.MessageFromAgent) 
      {
           Client.Self.InstantMessage(e.IM.FromAgentID, e.IM.Message, e.IM.IMSessionID); 
           //send them an instant message back (this thing will copy any message the bot recieves in an IM)
      }
}
 

You can also emulate typing through IM as such:

 
string reply = "Hello World!"; //Message to send.
byte[] bucket = new byte[1]; //I'm not sure of the actual binaryBucket, but 1 seems to work just fine.
Client.Self.InstantMessage(Client.Self.Name, e.IM.FromAgentID, "typing", e.IM.IMSessionID, InstantMessageDialog.StartTyping,
	InstantMessageOnline.Online, Client.Self.SimPosition, Client.Network.CurrentSim.RegionID, bucket); //Send the typing dialog.
//using System.Threading;
Thread.Sleep((reply.Length * 300)); //Sleep for a short amount of time, based on the length of the message. (.3 seconds per character.)
Client.Self.InstantMessage(e.IM.FromAgentID, reply, e.IM.IMSessionID); //Send the message.
 

Example of how to accept group invitations, friendship offers, teleports, and inventory offers.

 
static void Self_IM(object sender, InstantMessageEventArgs e) {
    switch (e.IM.Dialog) {
	case InstantMessageDialog.GroupInvitation:
		Client.Self.GroupInviteRespond(e.IM.FromAgentID, e.IM.IMSessionID, true);
		break;
	case InstantMessageDialog.FriendshipOffered:
		Client.Friends.AcceptFriendship(e.IM.FromAgentID, e.IM.IMSessionID);
		break;
	case InstantMessageDialog.RequestTeleport:
		Client.Self.TeleportLureRespond(e.IM.FromAgentID, e.IM.IMSessionID, true);
		break;
	case InstantMessageDialog.InventoryOffered: {
		//accepting the inventory offer
		Client.Self.InstantMessage(Client.Self.Name, e.IM.FromAgentID, String.Empty, e.IM.IMSessionID, InstantMessageDialog.InventoryAccepted,
		InstantMessageOnline.Offline, Client.Self.SimPosition, UUID.Zero, new byte[0]);
		if (e.IM.BinaryBucket.Length == 17) {
			//here we are getting the AssetType and the objectID, so this can be a bit more useful to you
			AssetType type = (AssetType)e.IM.BinaryBucket[0];
			UUID objectID = new UUID(e.IM.BinaryBucket, 1);
		}
		//declining the inventory offer
		/*Client.Self.InstantMessage(Client.Self.Name, e.IM.FromAgentID, String.Empty, e.IM.IMSessionID, InstantMessageDialog.InventoryDeclined,
		InstantMessageOnline.Offline, Client.Self.SimPosition, UUID.Zero, new byte[0]);*/
		break;
	}
	default:
		break;
	}
}
 

Important: Please notice the IMSessionID parameter of InstantMessage(). It is optional, but if you do not include it when you are replying to an instant message, it will open a new tab on the actual client instead of keeping the conversation on one tab. If you are too lazy, you can use LLUUID.Zero as the sessionID and it should work most of the time, but it is not guaranteed

See this This documentation page for a listing of InstantMessageDialog enumerations

The above link is broken, the trunk (development build) is found at: http://lib.openmetaverse.org/docs/trunk/html/T_OpenMetaverse_InstantMessageDialog.htm

Notes

  • There are many tasks you can accomplish with Instant Message processing.. in fact, "Instant Messages" are not just IMs to and from avatars, but it also handles green IM text from objects, the 'Alpha Zaius is typing...' text, Teleport Requests, Friend Requests, sending a URL open request, and much more. There are already wrapper classes for most of these functions though
  • If we didn't check for the Dialog type, it will IM the avatar "typing" whenever they type. Notice the InstantMessageDialog.StartTyping/StopTyping enumerations.