8
I would like to know, with an example if possible, how to send a Whatsapp message to a group created by another person 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");
}
Already 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 your help so far.
Is it in PHP or Python? What have you developed so far? Start by doing a [tour] and see [Ask].
– gmsantos
look I’m doing in java, code I picked up on the internet it is based venomous0x which is in php, and wanted in java I’ll show the code pera there
– Jacob de Oliveira
is so to send individually I know already, what I wanted is to send to a group that my friend created on his cell phone, he already added me in the group in the case how to send to the group created by him
– Jacob de Oliveira
in case I am already inserted in the group that my friend created, but I can’t send messages to the same
– Jacob de Oliveira
From what I understand it’s based on libraries
venomous0x
andyowsup
to create a similar one in Java. Is this @Jacobdeoliveira? If so, the last issue completely changed the meaning of the question.– Renan Gomes
is that I didn’t express it well, but that’s it, what I could find was from this site here: https://github.com/sumppen/WhatsApi4J
– Jacob de Oliveira
In this site has the implementation of venomous0x in java only that bugged why when having to send a message with accent it arrives with interrogation on the mobile...
– Jacob de Oliveira
the accent words in the case. What happens I was looking on the net for some solution in java but I could not from there saw these alternatives only it is very complicated to move to java because you have to understand the language and do something similar in java. if you can
– Jacob de Oliveira
In this passage
wa.sendMessage("5591XXXXXXXX-Mentor", msg);
,Mentor
is the name of the group?– Lucio Rubens
would be the numeroccellular-name group only that does not work
– Jacob de Oliveira
Related: http://answall.com/a/130096/3635
– Guilherme Nascimento
@Stormwind It happens that the other one is closed as not being clear enough, and this one is. I disagree to be duplicate. Also, in the other question the OP is wondering if there is any API in C#. This one is about programming with a certain API. You may even say that this question is obsolete and that the other is related, but not that it is duplicate.
– Victor Stafusa