Java Code Examples for org.jivesoftware.smack.Chat#sendMessage()

The following examples show how to use org.jivesoftware.smack.Chat#sendMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XHTMLExtensionTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
    * Low level API test.
    * This is a simple test to use with an XMPP client and check if the client receives the message
    * 1. User_1 will send a message with formatted text (XHTML) to user_2
    */
   public void testSendSimpleXHTMLMessage() {
// User1 creates a chat with user2
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);

// User1 creates a message to send to user2
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody("Hey John, this is my new green!!!!");
// Create a XHTMLExtension Package and add it to the message
XHTMLExtension xhtmlExtension = new XHTMLExtension();
xhtmlExtension.addBody(
"<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
msg.addExtension(xhtmlExtension);

// User1 sends the message that contains the XHTML to user2
try {
    chat1.sendMessage(msg);
    Thread.sleep(200);
}
catch (Exception e) {
    fail("An error occurred sending the message with XHTML");
}
   }
 
Example 2
Source File: XMPPService.java    From jolie with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Identifier( "sendMessage" )
@RequestResponse
public void _sendMessage( Value request )
	throws FaultException {
	Chat chat = getChat( request.getFirstChild( "to" ).strValue() );
	try {
		chat.sendMessage( request.strValue() );
	} catch( XMPPException e ) {
		throw new FaultException( e );
	}
}
 
Example 3
Source File: IMAppender.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * Send the contents of the cyclic buffer as an IM message.
 */
protected void sendBuffer() {
    try {
        final StringBuilder buf = new StringBuilder();

        final int len = cb.length();
        for (int i = 0; i < len; i++) {
            final LoggingEvent event = cb.get();
            buf.append(layout.format(event));
            // if layout doesn't handle exception, the appender has to do it
            if (layout.ignoresThrowable()) {
                final String[] s = event.getThrowableStrRep();
                if (s != null) {
                    for (int j = 0; j < s.length; j++) {
                        buf.append(Layout.LINE_SEP);
                        buf.append(s[j]);
                    }
                }
            }
        }

        if (chatroom) {
            groupchat.sendMessage(buf.toString());
        } else {
            final Iterator iter = chats.iterator();
            while (iter.hasNext()) {
                final Chat chat = (Chat) iter.next();
                chat.sendMessage(buf.toString());
            }
        }

    } catch (final Exception e) {
        errorHandler.error("Could not send message in IMAppender [" + name + "]", e, ErrorCode.GENERIC_FAILURE);
    }
}
 
Example 4
Source File: IMAppender.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * Send the contents of the cyclic buffer as an IM message.
 */
protected void sendBuffer() {
    try {
        final StringBuilder buf = new StringBuilder();

        final int len = cb.length();
        for (int i = 0; i < len; i++) {
            final LoggingEvent event = cb.get();
            buf.append(layout.format(event));
            // if layout doesn't handle exception, the appender has to do it
            if (layout.ignoresThrowable()) {
                final String[] s = event.getThrowableStrRep();
                if (s != null) {
                    for (int j = 0; j < s.length; j++) {
                        buf.append(Layout.LINE_SEP);
                        buf.append(s[j]);
                    }
                }
            }
        }

        if (chatroom) {
            groupchat.sendMessage(buf.toString());
        } else {
            final Iterator iter = chats.iterator();
            while (iter.hasNext()) {
                final Chat chat = (Chat) iter.next();
                chat.sendMessage(buf.toString());
            }
        }

    } catch (final Exception e) {
        errorHandler.error("Could not send message in IMAppender [" + name + "]", e, ErrorCode.GENERIC_FAILURE);
    }
}
 
Example 5
Source File: SmackConnection.java    From SmackAndroidDemo with Apache License 2.0 5 votes vote down vote up
private void sendMessage(String body, String toJid) {
    Log.i(TAG, "sendMessage()");
    Chat chat = ChatManager.getInstanceFor(mConnection).createChat(toJid, this);
    try {
        chat.sendMessage(body);
    } catch (SmackException.NotConnectedException | XMPPException e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: Connection.java    From HippyJava with MIT License 5 votes vote down vote up
public void sendPM(String message, String to) throws XMPPException {
    Chat c;
    if (cache.containsKey(to))
        c = cache.get(to);
    else {
        c = XMPP.getChatManager().createChat(to, this);
        cache.put(to, c);
    }
    c.sendMessage(message);
}
 
Example 7
Source File: MPAuthenticationProvider.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Read the PIN number sent by the user as the reply.
 *
 * @param connection
 * @param userName
 * @return
 */
public boolean getUserResponse(XMPPConnection connection, String userName) {

    String response = null;
    Presence presence = connection.getRoster().getPresence(userName);

    if (presence.isAvailable()) {
        try {

            ChatManager chatManager = connection.getChatManager();
            Chat chat = chatManager.createChat(userName, null);
            PacketFilter filter =
                    new AndFilter(new PacketTypeFilter(Message.class), new FromContainsFilter(userName));
            XmppResponseListener chatListener = new XmppResponseListener();
            connection.addPacketListener(chatListener, filter);

            if (isPINEnabled) {

                chat.sendMessage("Please reply with your PIN Number here.");

                if (log.isInfoEnabled()) {
                    log.info("User PIN is sent to the user and awaiting for the response.");
                }

                while (!chatListener.isResponseReceived()) {
                    Thread.sleep(100);
                }

                response = chatListener.getResponse();

                if (response != null) {
                    return userPIN.contentEquals(response.trim());
                }
            } else {
                chat.sendMessage(
                        "You are about to get authenticated for your OpenID. Do you want to continue: [Yes] or [No]");

                if (log.isInfoEnabled()) {
                    log.info("User PIN is sent to the user and awaiting for the response.");
                }

                while (!chatListener.isResponseReceived()) {
                    Thread.sleep(100);
                }

                response = chatListener.getResponse();

                if (response != null) {
                    if ("YES".equalsIgnoreCase(response.trim())) {
                        return true;
                    } else if ("NO".equalsIgnoreCase(response.trim())) {
                        return false;
                    } else {
                        pinDisabledResponse = false;
                        return false;
                    }
                }
            }

        } catch (Exception e) {
            log.error("Error while getting user response", e);
        }
    } else {
        return false;
    }
    return false;

}
 
Example 8
Source File: MessengerService.java    From KlyphMessenger with MIT License 4 votes vote down vote up
private void sendMessageTo(Bundle bundle)
{
	if (!connection.isConnected())
	{
		addPendingAction(ACTION_SEND_MSG, bundle);
	}
	else
	{
		String to = bundle.getString("to");
		String message = bundle.getString("message");

		to = "-" + to + "@chat.facebook.com";
		Log.d(TAG, "sendMessage to " + to + " " + message);
		Chat toChat = null;

		for (Chat chat : chats)
		{
			if (chat.getParticipant().equals(to))
			{
				toChat = chat;
			}
		}

		if (toChat == null)
		{
			toChat = createChat(to);
		}
		
		org.jivesoftware.smack.packet.Message msg = new org.jivesoftware.smack.packet.Message();
		msg.setBody(message);
		msg.setTo(to);
		msg.setType(org.jivesoftware.smack.packet.Message.Type.chat);
		msg.setThread(toChat.getThreadID());
		
		// Add to the message all the notifications requests (offline, delivered, displayed, composing)
	    MessageEventManager.addNotificationsRequests(msg, true, true, true, true);
	    DeliveryReceiptManager.addDeliveryReceiptRequest(msg);
	    
		try
		{
			toChat.sendMessage(msg);
		}
		catch (XMPPException e)
		{
			e.printStackTrace();
		}
	}
}