dima_ua


Hi.

I'm developing Messenger bot. It will be based on .NET 2.0 and be using DotMSN library to handle messenger protocol.

I'm trying to enable activities handling in this bot. Right now I'm able to get data from activity sent using SendData method of Channel. But I need to send data to activity client from my bot.

Could anyone help on this

I've sniffed messenger packets and it seems that there is direct TCP connection for data exchange. Is there any docs on that protocol

Thanks.




Re: Developing Messenger bot

pragate


sir
i am devloping chat server application using java and j2ee ploatform .it should communicate with yahoo chat server with our yahoo login and password .it should login toyahoo messanger. it sent automatically sent messages to the yahoo chat ids provided in the database and it has to send another message from database we design.same way,the application has to popst messages on its own in chat rooms in a defined interval.please help to devolepe application.







Re: Developing Messenger bot

pragate

sir

i am devloping chat server application using java and j2ee ploatform .it should communicate with yahoo chat server with our yahoo login and password .it should login toyahoo messanger. it sent automatically sent messages to the yahoo chat ids provided in the database and it has to send another message from database we design.same way,the application has to popst messages on its own in chat rooms in a defined interval.please help to devolepe application.







Re: Developing Messenger bot

Peter - Bricksoft

Please see here: http://www.hypothetic.org/docs/msn/index.php




Re: Developing Messenger bot

J-Thread

That could have been an interesting website, but latest news from "December 19, 2003" isn't really "NEWS". Also, the MSNP10 protocol doesn't work anymore... We're currently using MSNP14.



Re: Developing Messenger bot

Dixiu

i have the same problem initiating an activity invitation from bot automatically using dotMSN 2.0.

did u figure it out could you share some sample code





Re: Developing Messenger bot

dima_ua

Hi.

Sorry for a delay with reply. Here you go. It's draft version but it works.

You can get P2PMessageSession by using this line:

P2PMessageSession session = messenger.P2PHandler.GetSession(localContact, remoteContact);

where messenger is instance of Messenger class from DotMSN.

private const string ActivityTypeGuid = "{6A13AF9C-5308-4F35-923A-67E8DDA40C2F}";

private P2PTransferSession SendActivityInternal(P2PMessageSession p2pSession, string localContact, string remoteContact, string activityNumber, string activityName)
{
MSNSLPHandler handlerObject = (MSNSLPHandler)p2pSession.GetHandler(typeof(MSNSLPHandler));

MSNObject msnObject = new MSNObject();
msnObject.Creator = localContact;

// set class variables
MSNSLPTransferProperties properties = new MSNSLPTransferProperties();
properties.SessionId = (uint)(new Random().Next(50000, int.MaxValue));

properties.LocalContact = localContact;
properties.RemoteContact = remoteContact;

properties.DataType = DataTransferType.Unknown;

MSNSLPMessage slpMessage = new MSNSLPMessage();

string activityUrl = activityNumber + ";1;" + activityName;
byte[] contextData = UnicodeEncoding.Unicode.GetBytes(activityUrl);
string base64Context = Convert.ToBase64String(contextData, 0, contextData.Length);
properties.Context = base64Context;

properties.LastBranch = Guid.NewGuid();
properties.CallId = Guid.NewGuid();

slpMessage.StartLine = "INVITE MSNMSGR:" + remoteContact + " MSNSLP/1.0";
slpMessage.To = "<msnmsgr:" + remoteContact + ">";
slpMessage.From = "<msnmsgr:" + localContact + ">";
slpMessage.Via = "MSNSLP/1.0/TLP ;branch=" + properties.LastBranch.ToString("B").ToUpper(System.Globalization.CultureInfo.InvariantCulture);
slpMessage.CSeq = 0;
slpMessage.CallId = properties.CallId.ToString("B").ToUpper(System.Globalization.CultureInfo.InvariantCulture);
slpMessage.MaxForwards = 0;
slpMessage.ContentType = "application/x-msnmsgr-sessionreqbody";

slpMessage.Body = "EUF-GUID: " + ActivityTypeGuid + "\r\nSessionID: " + properties.SessionId + "\r\nSChannelState: 0\r\nAppID: 7\r\n" +
"Context: " + base64Context + "\r\n\r\n";

if (Settings.TraceSwitch.TraceVerbose)
{
Trace.WriteLine("Sending activity ivitation with SID:" + properties.SessionId);
}

P2PMessage p2pMessage = new P2PMessage();
p2pMessage.InnerMessage = slpMessage;

// set the size, it could be more than 1202 bytes. This will make sure it is split in multiple messages by the processor.
p2pMessage.MessageSize = (uint)slpMessage.GetBytes().Length;

// store the transferproperties
handlerObject.AddTransferProperties(properties.CallId, properties);

// create a transfer session to handle the actual data transfer
P2PTransferSession session = Factory.CreateP2PTransferSession();
session.MessageSession = (P2PMessageSession)handlerObject.MessageProcessor;
session.SessionId = properties.SessionId;

handlerObject.MessageSession.AddTransferSession(session);

// set the data stream to write to
session.DataStream = msnObject.OpenStream();
session.IsSender = false;
session.CallId = properties.CallId;

handlerObject.OnTransferSessionCreated(session);

// send the invitation
handlerObject.MessageSession.SendMessage(p2pMessage);

return session;
}





Re: Developing Messenger bot

Yuanjie Liu

So when these codes are run over, what will happen

Does the remote client will be received an "active window invitation" or The remote client will continue to send a P2PMessage and I have to reply it

I'm not very familiar with DOTMSN but I have read materials about P2Pmessage. p2pmessage includes binary header ,message,binary footer.

I want to know what should be filled in in the 9 fields of binary header





Re: Developing Messenger bot

dima_ua

Hi.

This code will send "invitation" to start an activity to remote client so remote client can accept it or decline.

You should not do anything because activity is a HTML page, unless you want to receive data from remote client that has started activity. There are some JS objects available to send data from activity. But you'll have to extend DOTMSN a bit to be able to receive this data.





Re: Developing Messenger bot

motion

Hi , thanks for your piece of code . It works perfect for inviting.
I am also developing an msn bot using dotMSN 2.0 library and trying to send url(which can change based on input bot received) as a parameter to activity which will open link in activity window. Any hints on this






Re: Developing Messenger bot

dima_ua

This is not very easy task. I will try to post here code snippets that we use. But you may need additional changes\debugging.

1. When we start activity we use this code:

ActivityHelper helper = new ActivityHelper(session.Messenger);

P2PTransferSession p2psession = helper.SendActivity(MsnLogin, session.Email, ActivityNumber, ActivityName);

if (p2psession != null)

{

P2PCustomHandler customHandler = new P2PCustomHandler();

customHandler.SendDataReceived +=

new P2PCustomHandler.P2PSendDataEventHandler(msnHandler.SendDataReceived);

customHandler.MessageProcessor = p2psession.MessageProcessor;

p2psession.RegisterHandler(customHandler);

}

SendActivity method is simmilar to code I've posted already.

2. Here is P2PCustomHandler:

/// <summary>

/// Custom handler to catch SendData messages.

/// </summary>

public class P2PCustomHandler : IMessageHandler

{

#region IMessageHandler Members

private IMessageProcessor messageProcessor;

/// <summary>

/// The message processor to send outgoing p2p messages to.

/// </summary>

public IMessageProcessor MessageProcessor

{

get

{

return messageProcessor;

}

set

{

messageProcessor = value;

}

}

/// <summary>

/// The message session to send message to. This is simply the MessageProcessor property, but explicitly casted as a P2PMessageSession.

/// </summary>

private P2PMessageSession MessageSession

{

get { return (P2PMessageSession)MessageProcessor; }

}

/// <summary>

/// Handles message.

/// </summary>

public void HandleMessage(IMessageProcessor sender, NetworkMessage message)

{

P2PMessage p2pMessage = message as P2PMessage;

if (p2pMessage == null)

{

return;

}

MSNSLPMessage slpMessage = new MSNSLPMessage();

slpMessage.CreateFromMessage(message);

if(Settings.TraceSwitch.TraceVerbose)

{

Trace.WriteLine("P2PCustomHandler: " + p2pMessage.ToDebugString());

}

if (string.IsNullOrEmpty(slpMessage.ContentType) &&

message.InnerBody != null && message.InnerBody.Length > 10)

{

if (Settings.TraceSwitch.TraceVerbose)

{

Trace.WriteLine("P2PCustomHandler. Correct message type.");

Trace.WriteLine("P2PCustomHandler. Body Bytes: " + GetDumpBytesString(message.InnerBody));

}

int size = BitConverter.ToInt32(message.InnerBody, 6);

//Trim end 0 char

size -= 2;

byte[] sourceData = new byte[size];

Array.Copy(message.InnerBody, 10, sourceData, 0, size);

string s = UnicodeEncoding.Unicode.GetString(sourceData);

if (Settings.TraceSwitch.TraceVerbose)

{

Trace.WriteLine("P2PCustomHandler. Decoded Data: " + s);

Trace.WriteLine("P2PCustomHandler. Re-encoded Data: " + GetDumpBytesString(UnicodeEncoding.Unicode.GetBytes(s)));

}

OnSendDataReceived(s, MessageSession.RemoteContact, ((P2PMessage)message).SessionId, ((P2PMessage)message).AckSessionId);

if (Settings.TraceSwitch.TraceVerbose)

{

Trace.WriteLine("P2PCustomHandler. Event raised.");

}

}

}

#endregion

/// <summary>

/// SendData received.

/// </summary>

public virtual void OnSendDataReceived(string data, string contact, uint sessionId, uint ackSessionId)

{

if (SendDataReceived != null)

{

SendDataReceived(this, new P2PSendDataEventArgs(data, contact, sessionId, ackSessionId));

}

}

/// <summary>

/// Event rised when SendData is received.

/// </summary>

public event P2PSendDataEventHandler SendDataReceived;

/// <summary>

/// Event handler for <see cref="SendDataReceived"/>.

/// </summary>

public delegate void P2PSendDataEventHandler(object sender, P2PSendDataEventArgs e);

/// <summary>

/// Returns string representation of byte array.

/// </summary>

private string GetDumpBytesString(byte[] data)

{

StringBuilder sb = new StringBuilder();

sb.Append("{");

for (int i = 0; i < data.Length; i++)

{

sb.Append(dataIdea);

if (i < data.Length - 1)

{

sb.Append(", ");

}

}

sb.Append("}");

return sb.ToString();

}

}

3. Here is P2PSendDataEventArgs:

/// <summary>

/// Event args for DataSend receive event.

/// </summary>

public class P2PSendDataEventArgs : EventArgs

{

private string data = string.Empty;

private string contact;

private uint sessionId;

private uint ackSessionId;

/// <summary>

/// AckSessionId.

/// </summary>

public uint AckSessionId

{

get { return ackSessionId; }

set { ackSessionId = value; }

}

/// <summary>

/// SessionId.

/// </summary>

public uint SessionId

{

get { return sessionId; }

set { sessionId = value; }

}

/// <summary>

/// Contact e-mail who sent data.

/// </summary>

public string Contact

{

get { return contact; }

set { contact = value; }

}

/// <summary>

/// Sent data.

/// </summary>

public string Data

{

get { return data; }

set { data = value; }

}

/// <summary>

/// Default constructor.

/// </summary>

public P2PSendDataEventArgs(string data, string contact, uint sessionId, uint ackSessionId)

{

this.data = data;

this.contact = contact;

this.sessionId = sessionId;

this.ackSessionId = ackSessionId;

}

}

4. Here is class that use to send data back to MSN client:

/// <summary>

/// Helper to perform DataSend into activity.

/// </summary>

public class DataSendHelper

{

private Messenger messenger;

/// <summary>

/// Default constructor.

/// </summary>

/// <param name="messenger">Current <see cref="Messenger"/></param>

public DataSendHelper(Messenger messenger)

{

this.messenger = messenger;

}

/// <summary>

/// Performs SendData.

/// </summary>

/// <param name="remoteContact">Remote receiver of data.</param>

/// <param name="sessionId">SessionId.</param>

/// <param name="data">Data being sent.</param>

public void SendData(string remoteContact, uint sessionId, string data)

{

P2PMessageSession session = null;

lock (messenger.P2PHandler.MessageSessions)

{

foreach (P2PMessageSession tmpSession in messenger.P2PHandler.MessageSessions)

{

if (tmpSession.RemoteContact == remoteContact)

{

session = tmpSession;

break;

}

}

}

if (session != null)

{

P2PMessage msg = new P2PMessage();

msg.SessionId = sessionId;

msg.Flags = 0x0;

msg.Footer = 0x7;

DataSendMessage dataMsg = new DataSendMessage(data);

msg.InnerMessage = dataMsg;

session.SendMessage(msg);

}

}

}

Hope you will figure out how to use it.





Re: Developing Messenger bot

dima_ua

Sorry, lost one file.

Here we go. DataSendMessage:

internal class DataSendMessage : P2PMessage

{

string data;

public DataSendMessage(string data)

: base()

{

this.data = data;

}

public override byte[] GetBytes()

{

List<byte> bytes = new List<byte>();

//Data Send flags

bytes.AddRange(new byte[] { 128, 0, 0, 0, 8, 0 });

byte[] enc = UnicodeEncoding.Unicode.GetBytes(data);

bytes.AddRange(BitConverter.GetBytes(enc.Length + 2));

bytes.AddRange(enc);

bytes.AddRange(new byte[] { 0, 0 });

return bytes.ToArray();

}

}





Re: Developing Messenger bot

Yuanjie Liu

Hi. now I can send a invitation.

But I don't know how to set the activitiy number and URL

for example, if I want to let the active window display www.google.com , what the activity number and URL should be filled in





Re: Developing Messenger bot

J-Thread

You should make your own Activity or you could use an existing activity to do it. I know there's one for the BuddyScript SDK that you could use (search the forums for that), or there's also one for the incesoft SDK but I can't find it right now.



Re: Developing Messenger bot

Yuanjie Liu

Hello.

I've viewed the MSN MESSENGER SDK. It seems that it only have a chm file for telling you how to develop the activity( without need of installation of any package )

In the help file, it seems that if I write my own HTML file and put it on a webserver, and then change the configuration file(MSNSG**.XML or something like this) , I can test my activity. Am I understanding correctly