I would like to know, with an example if possible, how do I send a WhatsApp message to a group created by someone else using venomous0x or yowsup?
The shipping code is this:
public String sendMessage(String to, String message) throws WhatsAppException {
return sendMessage(to, message, null);
}
/**
* Send a text message to the user/group.
*
* @param String to The recipient.
* @param String message The text message.
* @param String id
*
* @return String
*/
public String sendMessage(String to, String message, String id) throws WhatsAppException {
message = parseMessageForEmojis(message);
ProtocolNode bodyNode = new ProtocolNode("body", null, null, message.getBytes());
try {
return sendMessageNode(to, bodyNode, id);
} catch (Exception e) {
throw new WhatsAppException("Failed to send message", e);
}
}
/**
* Send node to the servers.
*
* @param to The recipient to send.
* @param node The node that contains the message.
* @return message id
* @throws IOException
* @throws InvalidTokenException
* @throws InvalidMessageException
* @throws IncompleteMessageException
* @throws WhatsAppException
* @throws JSONException
* @throws NoSuchAlgorithmException
*/
private String sendMessageNode(String to, ProtocolNode node, String id) throws IOException, IncompleteMessageException, InvalidMessageException, InvalidTokenException, WhatsAppException, JSONException, NoSuchAlgorithmException {
ProtocolNode serverNode = new ProtocolNode("server", null, null, null);
List<ProtocolNode> list = new LinkedList<ProtocolNode>();
list.add(serverNode);
Map<String, String> xHash = new LinkedHashMap<String, String>();
xHash.put("xmlns", "jabber:x:event");
ProtocolNode xNode = new ProtocolNode("x", xHash, list, null);
Map<String, String> notify = new LinkedHashMap<String, String>();
notify.put("xmlns", "urn:xmpp:whatsapp");
notify.put("name", name);
ProtocolNode notnode = new ProtocolNode("notify", notify, null, null);
Map<String, String> request = new LinkedHashMap<String, String>();
request.put("xmlns", "urn:xmpp:receipts");
ProtocolNode reqnode = new ProtocolNode("request", request, null, null);
Map<String, String> messageHash = new LinkedHashMap<String, String>();
messageHash.put("to", getJID(to));
messageHash.put("type", "chat");
messageHash.put("id", (id == null ? createMsgId("message") : id));
messageHash.put("t", time());
list = new LinkedList<ProtocolNode>();
list.add(xNode);
list.add(notnode);
list.add(reqnode);
list.add(node);
ProtocolNode messageNode = new ProtocolNode("message", messageHash, list, null);
if (lastId == null) {
lastId = messageHash.get("id");
sendNode(messageNode);
//listen for response
waitForServer(messageHash.get("id"));
} else {
outQueue.add(messageNode);
}
eventManager().fireSendMessage(
phoneNumber,
getJID(to),
messageHash.get("id"),
node
);
return messageHash.get("id");
}
The test code is this:
public class TestSendMessageWhatsapp {
public TestSendMessageWhatsapp() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test
public void hello() throws NoSuchAlgorithmException, WhatsAppException, IOException {
String username = "5591XXXXXXXX", password = "/vuL76lOtEFfE5PE5Pw3Z3o6cXs=",
identity = "mentorid", nickname = "mentor";
boolean running = true;
boolean loggedIn = false;
WhatsApi wa = null;
try {
wa = new WhatsApi(username, identity, nickname);
EventManager eventManager = new ExampleEventManager();
wa.setEventManager(eventManager);
MessageProcessor mp = new ExampleMessageProcessor();
wa.setNewMessageBind(mp);
if (!wa.connect()) {
System.out.println("Failed to connect to WhatsApp");
System.exit(1);
}
if (password != null) {
wa.loginWithPassword(password);
loggedIn = true;
}
ExampleMessagePoller poller = new ExampleMessagePoller(wa);
poller.start();
String msg = "send message by java...";
//send message group
wa.sendMessage("5591XXXXXXXX-Mentor", msg);
System.out.print("$ ");
poller.setRunning(false);
System.out.println("Done! Logging out");
wa.disconnect();
} catch (Exception e) {
System.out.println("Caught exception: " + e.getMessage());
e.printStackTrace();
if (wa != null) {
wa.disconnect();
}
System.exit(1);
}
}
}
And the result of the execution is this:
tx: <stream:features>
<receipt_acks></receipt_acks><status></status>
</stream:features>
tx: <auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="WAUTH-1" user="559181196092"></auth>
tx: <response xmlns="urn:ietf:params:xml:ns:xmpp-sasl">a26d55bd313697d6de333cb89b3a91089d419d686e1cb98856032ca454b8386ac87f07174881a0e5c2c6f08dc25f</response>
tx: <presence type="available" name="mentor"></presence>
tx: <message to="[email protected]" type="chat" id="message-1420815830-1" t="1420815830">
<x xmlns="jabber:x:event">
<server></server>
</x><notify xmlns="urn:xmpp:whatsapp" name="mentor"></notify><request xmlns="urn:xmpp:receipts"></request><body>73656e64206d657373616765206279206a6176612e2e2e</body>
</message>
Event PRESENCE: phoneNumber=5591XXXXXXXX,[email protected],type=available
Event message_received_server: time=1420815892,phoneNumber=5591XXXXXXXX,msgId=message-1420815830-1,[email protected],type=chat
Event send_message: phoneNumber=5591XXXXXXXX
Thanks for the help right away.