How do I send a message to a Whatsapp group with the venomous0x or yowsup api?

Asked

Viewed 4,948 times

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.

  • 5

    Is it in PHP or Python? What have you developed so far? Start by doing a [tour] and see [Ask].

  • 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

  • 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

  • in case I am already inserted in the group that my friend created, but I can’t send messages to the same

  • From what I understand it’s based on libraries venomous0x and yowsup to create a similar one in Java. Is this @Jacobdeoliveira? If so, the last issue completely changed the meaning of the question.

  • 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

  • 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...

  • 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

  • In this passage wa.sendMessage("5591XXXXXXXX-Mentor", msg);, Mentor is the name of the group?

  • would be the numeroccellular-name group only that does not work

  • Related: http://answall.com/a/130096/3635

  • @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.

Show 7 more comments

1 answer

-1

I don’t know if you’re doing on Android more if it is

just use the code below

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

taken from the link below https://www.whatsapp.com/faq/pt_br/android/28000012

Browser other questions tagged

You are not signed in. Login or sign up in order to post.