private void WhatsEventHandlerOnMessageRecievedEvent(FMessage mess) { if (mess == null || mess.key.remote_jid == null || mess.key.remote_jid.Length == 0) return; if(!this.messageHistory.ContainsKey(mess.key.remote_jid)) this.messageHistory.Add(mess.key.remote_jid, new List<FMessage>()); this.messageHistory[mess.key.remote_jid].Add(mess); this.CheckIfUserRegisteredAndCreate(mess); }
private void CheckIfUserRegisteredAndCreate(FMessage mess) { if (this.messageHistory.ContainsKey(mess.key.remote_jid)) return; var jidSplit = mess.key.remote_jid.Split('@'); WhatsUser tmpWhatsUser = new WhatsUser(jidSplit[0], jidSplit[1], mess.key.serverNickname); User tmpUser = new User(jidSplit[0], jidSplit[1]); tmpUser.SetUser(tmpWhatsUser); this.messageHistory.Add(mess.key.remote_jid, new List<FMessage>()); this.messageHistory[mess.key.remote_jid].Add(mess); }
/* * No need to add documentation here * User will only handle the delagates and events * */ internal static void OnMessageRecievedEventHandler(FMessage mess) { var h = MessageRecievedEvent; if (h == null) return; foreach (var tmpSingleCast in h.GetInvocationList()) { var tmpSyncInvoke = tmpSingleCast.Target as ISynchronizeInvoke; if (tmpSyncInvoke != null && tmpSyncInvoke.InvokeRequired) { tmpSyncInvoke.BeginInvoke(tmpSingleCast, new object[] {mess}); continue; } h.BeginInvoke(mess, null, null); } }
protected void SendMessageWithBody(FMessage message, bool hidden = false) { var child = new ProtocolTreeNode("body", null, null, WhatsApp.SYSEncoding.GetBytes(message.data)); this.SendNode(GetMessageNode(message, child, hidden)); }
protected FMessage getFmessageImage(string to, byte[] ImageData, ImageType imgtype) { string type = string.Empty; string extension = string.Empty; switch (imgtype) { case ImageType.PNG: type = "image/png"; extension = "png"; break; case ImageType.GIF: type = "image/gif"; extension = "gif"; break; default: type = "image/jpeg"; extension = "jpg"; break; } //create hash string filehash = string.Empty; using(HashAlgorithm sha = HashAlgorithm.Create("sha256")) { byte[] raw = sha.ComputeHash(ImageData); filehash = Convert.ToBase64String(raw); } //request upload WaUploadResponse response = this.UploadFile(filehash, "image", ImageData.Length, ImageData, to, type, extension); if (response != null && !String.IsNullOrEmpty(response.url)) { //send message FMessage msg = new FMessage(to, true) { media_wa_type = FMessage.Type.Image, media_mime_type = response.mimetype, media_name = response.url.Split('/').Last(), media_size = response.size, media_url = response.url, binary_data = this.CreateThumbnail(ImageData) }; return msg; } return null; }
protected static ProtocolTreeNode GetMessageNode(FMessage message, ProtocolTreeNode pNode, bool hidden = false) { return new ProtocolTreeNode("message", new[] { new KeyValue("to", message.identifier_key.remote_jid), new KeyValue("type", message.media_wa_type == FMessage.Type.Undefined?"text":"media"), new KeyValue("id", message.identifier_key.id) }, new ProtocolTreeNode[] { new ProtocolTreeNode("x", new KeyValue[] { new KeyValue("xmlns", "jabber:x:event") }, new ProtocolTreeNode("server", null)), pNode, new ProtocolTreeNode("offline", null) }); }
public void SendMessageVcard(string to, string name, string vcard_data) { var tmpMessage = new FMessage(GetJID(to), true) { data = vcard_data, media_wa_type = FMessage.Type.Contact, media_name = name }; this.SendMessage(tmpMessage, this.hidden); }
public void SendMessage(FMessage message, bool hidden = false) { if (message.media_wa_type != FMessage.Type.Undefined) { this.SendMessageWithMedia(message); } else { this.SendMessageWithBody(message, hidden); } }
protected void SendMessageReceived(FMessage message, string response) { ProtocolTreeNode node = new ProtocolTreeNode("receipt", new[] { new KeyValue("to", message.identifier_key.remote_jid), new KeyValue("id", message.identifier_key.id) }); this.SendNode(node); }
private void WhatsEventHandlerOnMessageRecievedEvent(FMessage mess) { var tmpMes = mess.data; this.AddNewText(this.user.UserName, tmpMes); }
/// <summary> /// Get the message node /// </summary> /// <param name="message">the message</param> /// <param name="pNode">The protocol tree node</param> /// <returns>An instance of the ProtocolTreeNode class.</returns> internal static ProtocolTreeNode GetMessageNode(FMessage message, ProtocolTreeNode pNode, bool hidden = false) { return new ProtocolTreeNode("message", new[] { new KeyValue("to", message.identifier_key.remote_jid), new KeyValue("type", "chat"), new KeyValue("id", message.identifier_key.id) }, new ProtocolTreeNode[] { new ProtocolTreeNode("x", new KeyValue[] { new KeyValue("xmlns", "jabber:x:event") }, new ProtocolTreeNode("server", null)), pNode, new ProtocolTreeNode("offline", null) }); }
/// <summary> /// Tell the server the message has been recieved. /// </summary> /// <param name="message">An instance of the FMessage class.</param> public void SendMessageReceived(FMessage message, string response) { var child = new ProtocolTreeNode(response, new[] { new KeyValue("xmlns", "urn:xmpp:receipts") }); var node = new ProtocolTreeNode("message", new[] { new KeyValue("to", message.identifier_key.remote_jid), new KeyValue("type", "chat"), new KeyValue("id", message.identifier_key.id) }, child); this.whatsNetwork.SendData(this._binWriter.Write(node)); }
protected void SendMessageReceived(FMessage message, string type = "read") { KeyValue toAttrib = new KeyValue("to", message.identifier_key.remote_jid); KeyValue idAttrib = new KeyValue("id", message.identifier_key.id); var attribs = new List<KeyValue>(); attribs.Add(toAttrib); attribs.Add(idAttrib); if (type.Equals("read")) { KeyValue typeAttrib = new KeyValue("type", type); attribs.Add(typeAttrib); } ProtocolTreeNode node = new ProtocolTreeNode("receipt", attribs.ToArray()); this.SendNode(node); }
/// <summary> /// Send an image to a person /// </summary> /// <param name="msgid">The id of the message</param> /// <param name="to">the reciepient</param> /// <param name="url">The url to the image</param> /// <param name="file">Filename</param> /// <param name="size">The size of the image in string format</param> /// <param name="icon">Icon</param> public void MessageImage(string to, string filepath) { to = this.GetJID(to); FileInfo finfo = new FileInfo(filepath); string type = string.Empty; switch (finfo.Extension) { case ".png": type = "image/png"; break; case ".gif": type = "image/gif"; break; default: type = "image/jpeg"; break; } //create hash string filehash = string.Empty; using(FileStream fs = File.OpenRead(filepath)) { using(BufferedStream bs = new BufferedStream(fs)) { using(HashAlgorithm sha = HashAlgorithm.Create("sha256")) { byte[] raw = sha.ComputeHash(bs); filehash = Convert.ToBase64String(raw); } } } //request upload UploadResponse response = this.UploadFile(filehash, "image", finfo.Length, filepath, to, type); if (response != null && !String.IsNullOrEmpty(response.url)) { //send message FMessage msg = new FMessage(to, true) { identifier_key = { id = TicketManager.GenerateId() }, media_wa_type = FMessage.Type.Image, media_mime_type = response.mimetype, media_name = response.url.Split('/').Last(), media_size = response.size, media_url = response.url, binary_data = this.CreateThumbnail(filepath) }; this.WhatsSendHandler.SendMessage(msg); } }
/// <summary> /// Send a message to a person /// </summary> /// <param name="to">The phone number to send</param> /// <param name="txt">The text that needs to be send</param> public void Message(string to, string txt) { var tmpMessage = new FMessage(this.GetJID(to), true) { identifier_key = { id = TicketManager.GenerateId() }, data = txt }; this.WhatsParser.WhatsSendHandler.SendMessage(tmpMessage); }
/// <summary> /// Notify typing chat /// </summary> /// <param name="messageNode"></param> /// <param name="tmpAttrFrom"></param> /// <param name="tmpAttrbId"></param> /// <param name="builder"></param> /// <param name="tmpAttrFromJid"></param> private void TypeChat(ProtocolTreeNode messageNode, string tmpAttrFrom, string tmpAttrbId, FMessage.Builder builder, string tmpAttrFromJid) { foreach (ProtocolTreeNode itemNode in (messageNode.GetAllChildren() ?? new ProtocolTreeNode[0])) { if (ProtocolTreeNode.TagEquals(itemNode, "composing")) { WhatsEventHandler.OnIsTypingEventHandler(tmpAttrFrom, true); } else if (ProtocolTreeNode.TagEquals(itemNode, "paused")) { WhatsEventHandler.OnIsTypingEventHandler(tmpAttrFrom, false); } else if (ProtocolTreeNode.TagEquals(itemNode, "body") && (tmpAttrbId != null)) { string dataString = WhatsApp.SYSEncoding.GetString(itemNode.GetData()); var tmpMessKey = new FMessage.Key(tmpAttrFrom, false, tmpAttrbId); builder.Key(tmpMessKey).Remote_resource(tmpAttrFromJid).NewIncomingInstance().Data(dataString); } else if (ProtocolTreeNode.TagEquals(itemNode, "media") && (tmpAttrbId != null)) { long tmpMediaSize; int tmpMediaDuration; builder.Media_wa_type(FMessage.GetMessage_WA_Type(itemNode.GetAttribute("type"))).Media_url( itemNode.GetAttribute("url")).Media_name(itemNode.GetAttribute("file")); if (long.TryParse(itemNode.GetAttribute("size"), WhatsConstants.WhatsAppNumberStyle, CultureInfo.InvariantCulture, out tmpMediaSize)) { builder.Media_size(tmpMediaSize); } string tmpAttrSeconds = itemNode.GetAttribute("seconds"); if ((tmpAttrSeconds != null) && int.TryParse(tmpAttrSeconds, WhatsConstants.WhatsAppNumberStyle, CultureInfo.InvariantCulture, out tmpMediaDuration)) { builder.Media_duration_seconds(tmpMediaDuration); } if (builder.Media_wa_type().HasValue && (builder.Media_wa_type().Value == FMessage.Type.Location)) { double tmpLatitude = 0; double tmpLongitude = 0; string tmpAttrLatitude = itemNode.GetAttribute("latitude"); string tmpAttrLongitude = itemNode.GetAttribute("longitude"); if ((tmpAttrLatitude == null) || (tmpAttrLongitude == null)) { tmpAttrLatitude = "0"; tmpAttrLongitude = "0"; } else if (!double.TryParse(tmpAttrLatitude, WhatsConstants.WhatsAppNumberStyle, CultureInfo.InvariantCulture, out tmpLatitude) || !double.TryParse(tmpAttrLongitude, WhatsConstants.WhatsAppNumberStyle, CultureInfo.InvariantCulture, out tmpLongitude)) { throw new CorruptStreamException("location message exception parsing lat or long attribute: " + tmpAttrLatitude + " " + tmpAttrLongitude); } builder.Latitude(tmpLatitude).Longitude(tmpLongitude); string tmpAttrName = itemNode.GetAttribute("name"); string tmpAttrUrl = itemNode.GetAttribute("url"); if (tmpAttrName != null) { builder.Location_details(tmpAttrName); } if (tmpAttrUrl != null) { builder.Location_url(tmpAttrUrl); } } if (builder.Media_wa_type().HasValue && (builder.Media_wa_type().Value) == FMessage.Type.Contact) { ProtocolTreeNode tmpChildMedia = itemNode.GetChild("media"); if (tmpChildMedia != null) { builder.Media_name(tmpChildMedia.GetAttribute("name")).Data(WhatsApp.SYSEncoding.GetString(tmpChildMedia.GetData())); } } else { string tmpAttrEncoding = itemNode.GetAttribute("encoding") ?? "text"; if (tmpAttrEncoding == "text") { builder.Data(WhatsApp.SYSEncoding.GetString(itemNode.GetData())); } } var tmpMessageKey = new FMessage.Key(tmpAttrFrom, false, tmpAttrbId); builder.Key(tmpMessageKey).Remote_resource(tmpAttrFromJid).NewIncomingInstance(); } else if (ProtocolTreeNode.TagEquals(itemNode, "request")) { builder.Wants_receipt(true); } else if (ProtocolTreeNode.TagEquals(itemNode, "x")) { string str16 = itemNode.GetAttribute("xmlns"); if ("jabber:x:event".Equals(str16) && (tmpAttrbId != null)) { var tmpMessageKey = new FMessage.Key(tmpAttrFrom, true, tmpAttrbId); } } else if (ProtocolTreeNode.TagEquals(itemNode, "received")) { if (tmpAttrbId != null) { var tmpMessageKey = new FMessage.Key(tmpAttrFrom, true, tmpAttrbId); if (true) { string tmpAttrType = itemNode.GetAttribute("type"); if ((tmpAttrType != null) && !tmpAttrType.Equals("delivered")) { if (tmpAttrType.Equals("visible")) { this.sendHandler.SendVisibleReceiptAck(tmpAttrFrom, tmpAttrbId); } } else { this.sendHandler.SendDeliveredReceiptAck(tmpAttrFrom, tmpAttrbId); } } } } else if (ProtocolTreeNode.TagEquals(itemNode, "offline")) { builder.Offline(true); } else if (ProtocolTreeNode.TagEquals(itemNode, "notify")) { var tmpAttrName = itemNode.GetAttribute("name"); if (tmpAttrName != null) { builder.from_me = false; builder.id = tmpAttrbId; builder.remote_jid = tmpAttrFromJid; builder.Key().serverNickname = tmpAttrName; } } } if (!builder.Timestamp().HasValue) { builder.Timestamp(new DateTime?(DateTime.Now)); } FMessage message = builder.Build(); if (message != null) { WhatsEventHandler.OnMessageRecievedEventHandler(message); } }
protected void sendMessageReceived(ProtocolTreeNode msg, string response = "received") { FMessage tmpMessage = new FMessage(new FMessage.FMessageIdentifierKey(msg.GetAttribute("from"), true, msg.GetAttribute("id"))); this.SendMessageReceived(tmpMessage, response); }
public string Message(string to, string txt) { //var bodyNode = new ProtocolTreeNode("body", null, txt); //var tmpMessage = new FMessage(to, true) { key = { id = TicketCounter.MakeId("mSend_") }, data = txt }; var tmpMessage = new FMessage(to, true) { key = { id = TicketManager.GenerateId() }, data = txt }; this.WhatsParser.WhatsSendHandler.SendMessage(tmpMessage); return tmpMessage.key.id; }
public string SendMessage(string to, string txt) { var tmpMessage = new FMessage(GetJID(to), true) { data = txt }; this.SendMessage(tmpMessage, this.hidden); return tmpMessage.identifier_key.ToString(); }
public string MessageImage(string to, string url, long size, string mime_type, string binary_data, string mid) { //var mediaAttribs = new KeyValue[] // { // new KeyValue("xmlns", "urn:xmpp:whatsapp:mms"), // new KeyValue("type", "image"), // new KeyValue("url", url), // new KeyValue("file", file), // new KeyValue("size", size) // }; //var mediaNode = new ProtocolTreeNode("media", mediaAttribs, icon); //this.SendMessageNode(msgid, to, mediaNode); if (string.IsNullOrEmpty(mid)) { mid = TicketManager.GenerateId(); } var tmpMessage = new FMessage(to, true) { key = { id = mid } }; tmpMessage.binary_data = Convert.FromBase64String(binary_data); tmpMessage.data = ""; tmpMessage.media_mime_type = mime_type; tmpMessage.media_name = ""; tmpMessage.media_size = size; tmpMessage.media_url = url; tmpMessage.media_wa_type = FMessage.Type.Image; this.WhatsParser.WhatsSendHandler.SendMessageWithMedia(tmpMessage); return tmpMessage.key.id; }
public void SendMessageBroadcast(string[] to, FMessage message) { if (to != null && to.Length > 0 && message != null && !string.IsNullOrEmpty(message.data)) { ProtocolTreeNode child; if (message.media_wa_type == FMessage.Type.Undefined) { //text broadcast child = new ProtocolTreeNode("body", null, null, WhatsApp.SYSEncoding.GetBytes(message.data)); } else { throw new NotImplementedException(); } //compose broadcast envelope ProtocolTreeNode xnode = new ProtocolTreeNode("x", new KeyValue[] { new KeyValue("xmlns", "jabber:x:event") }, new ProtocolTreeNode("server", null)); List<ProtocolTreeNode> toNodes = new List<ProtocolTreeNode>(); foreach (string target in to) { toNodes.Add(new ProtocolTreeNode("to", new KeyValue[] { new KeyValue("jid", WhatsAppApi.WhatsApp.GetJID(target)) })); } ProtocolTreeNode broadcastNode = new ProtocolTreeNode("broadcast", null, toNodes); ProtocolTreeNode messageNode = new ProtocolTreeNode("message", new KeyValue[] { new KeyValue("to", "broadcast"), new KeyValue("type", message.media_wa_type == FMessage.Type.Undefined?"text":"media"), new KeyValue("id", message.identifier_key.id) }, new ProtocolTreeNode[] { broadcastNode, xnode, child }); this.SendNode(messageNode); } }
protected void sendMessageReceived(ProtocolTreeNode msg) { //this.WhatsParser.WhatsSendHandler.SendMessageReceived(); ProtocolTreeNode requestNode = msg.GetChild("request"); if (requestNode == null || !requestNode.GetAttribute("xmlns").Equals("urn:xmpp:receipts", StringComparison.OrdinalIgnoreCase)) return; FMessage tmpMessage = new FMessage(new FMessage.Key(msg.GetAttribute("from"), true, msg.GetAttribute("id"))); this.WhatsParser.WhatsSendHandler.SendMessageReceived(tmpMessage); //var receivedNode = new ProtocolTreeNode("received", // new[] {new KeyValue("xmlns", "urn:xmpp:receipts")}); //var messageNode = new ProtocolTreeNode("message", // new[] // { // new KeyValue("to", msg.GetAttribute("from")), // new KeyValue("type", "chat"), // new KeyValue("id", msg.GetAttribute("id")) // }, // new[] {receivedNode}); //this.whatsNetwork.SendNode(messageNode); }
public void SendStatusUpdate(string status) { string id = TicketManager.GenerateId(); FMessage message = new FMessage(new FMessage.FMessageIdentifierKey("s.us", true, id)); var messageNode = GetMessageNode(message, new ProtocolTreeNode("body", null, WhatsApp.SYSEncoding.GetBytes(status))); this.SendNode(messageNode); }
//overload public void SendMessageBroadcast(string to, FMessage message) { this.SendMessageBroadcast(new string[] { to }, message); }
protected FMessage getFmessageAudio(string to, byte[] audioData, AudioType audtype) { to = GetJID(to); string type = string.Empty; string extension = string.Empty; switch (audtype) { case AudioType.WAV: type = "audio/wav"; extension = "wav"; break; case AudioType.OGG: type = "audio/ogg"; extension = "ogg"; break; default: type = "audio/mpeg"; extension = "mp3"; break; } //create hash string filehash = string.Empty; using (HashAlgorithm sha = HashAlgorithm.Create("sha256")) { byte[] raw = sha.ComputeHash(audioData); filehash = Convert.ToBase64String(raw); } //request upload WaUploadResponse response = this.UploadFile(filehash, "audio", audioData.Length, audioData, to, type, extension); if (response != null && !String.IsNullOrEmpty(response.url)) { //send message FMessage msg = new FMessage(to, true) { media_wa_type = FMessage.Type.Audio, media_mime_type = response.mimetype, media_name = response.url.Split('/').Last(), media_size = response.size, media_url = response.url, media_duration_seconds = response.duration }; return msg; } return null; }
/// <summary> /// Tell the server the message has been recieved. /// </summary> /// <param name="message">An instance of the FMessage class.</param> public void SendMessageReceived(FMessage message, string response) { ProtocolTreeNode node = new ProtocolTreeNode("receipt", new[] { new KeyValue("to", message.identifier_key.remote_jid), new KeyValue("id", message.identifier_key.id) }); this.whatsNetwork.SendData(this.BinWriter.Write(node)); }
protected FMessage getFmessageVideo(string to, byte[] videoData, VideoType vidtype) { to = GetJID(to); string type = string.Empty; string extension = string.Empty; switch (vidtype) { case VideoType.MOV: type = "video/quicktime"; extension = "mov"; break; case VideoType.AVI: type = "video/x-msvideo"; extension = "avi"; break; default: type = "video/mp4"; extension = "mp4"; break; } //create hash string filehash = string.Empty; using (HashAlgorithm sha = HashAlgorithm.Create("sha256")) { byte[] raw = sha.ComputeHash(videoData); filehash = Convert.ToBase64String(raw); } //request upload WaUploadResponse response = this.UploadFile(filehash, "video", videoData.Length, videoData, to, type, extension); if (response != null && !String.IsNullOrEmpty(response.url)) { //send message FMessage msg = new FMessage(to, true) { media_wa_type = FMessage.Type.Video, media_mime_type = response.mimetype, media_name = response.url.Split('/').Last(), media_size = response.size, media_url = response.url, media_duration_seconds = response.duration }; return msg; } return null; }
/// <summary> /// Send a status update /// </summary> /// <param name="status">The status</param> /// <param name="onSuccess">Action to execute when the request was successful</param> /// <param name="onError">Action to execute when the request failed</param> public void SendStatusUpdate(string status, Action onComplete, Action<int> onError) { string id = TicketManager.GenerateId(); FMessage message = new FMessage(new FMessage.FMessageIdentifierKey("s.us", true, id)); var messageNode = GetMessageNode(message, new ProtocolTreeNode("body", null, WhatsApp.SYSEncoding.GetBytes(status))); this.whatsNetwork.SendData(this.BinWriter.Write(messageNode)); }
protected void SendMessageWithMedia(FMessage message) { ProtocolTreeNode node; if (FMessage.Type.System == message.media_wa_type) { throw new SystemException("Cannot send system message over the network"); } List<KeyValue> list = new List<KeyValue>(new KeyValue[] { new KeyValue("xmlns", "urn:xmpp:whatsapp:mms"), new KeyValue("type", FMessage.GetMessage_WA_Type_StrValue(message.media_wa_type)) }); if (FMessage.Type.Location == message.media_wa_type) { list.AddRange(new KeyValue[] { new KeyValue("latitude", message.latitude.ToString(CultureInfo.InvariantCulture)), new KeyValue("longitude", message.longitude.ToString(CultureInfo.InvariantCulture)) }); if (message.location_details != null) { list.Add(new KeyValue("name", message.location_details)); } if (message.location_url != null) { list.Add(new KeyValue("url", message.location_url)); } } else if (((FMessage.Type.Contact != message.media_wa_type) && (message.media_name != null)) && ((message.media_url != null) && (message.media_size > 0L))) { list.AddRange(new KeyValue[] { new KeyValue("file", message.media_name), new KeyValue("size", message.media_size.ToString(CultureInfo.InvariantCulture)), new KeyValue("url", message.media_url) }); if (message.media_duration_seconds > 0) { list.Add(new KeyValue("seconds", message.media_duration_seconds.ToString(CultureInfo.InvariantCulture))); } } if ((FMessage.Type.Contact == message.media_wa_type) && (message.media_name != null)) { node = new ProtocolTreeNode("media", list.ToArray(), new ProtocolTreeNode("vcard", new KeyValue[] { new KeyValue("name", message.media_name) }, WhatsApp.SYSEncoding.GetBytes(message.data))); } else { byte[] data = message.binary_data; if ((data == null) && !string.IsNullOrEmpty(message.data)) { try { data = Convert.FromBase64String(message.data); } catch (Exception) { } } if (data != null) { list.Add(new KeyValue("encoding", "raw")); } node = new ProtocolTreeNode("media", list.ToArray(), null, data); } this.SendNode(GetMessageNode(message, node)); }
/// <summary> /// Send a message with a body (Plain text); /// </summary> /// <param name="message">An instance of the FMessage class.</param> internal void SendMessageWithBody(FMessage message, bool hidden = false) { var child = new ProtocolTreeNode("body", null, null, WhatsApp.SYSEncoding.GetBytes(message.data)); this.whatsNetwork.SendData(this.BinWriter.Write(GetMessageNode(message, child, hidden))); }