How does one send chat message from chat bot to clients? - java

I have made a bot which can reply when the client sends a message. I want to send a message to the client without them sending me a message. I don't want to code in each and every message. Can someone please suggest a method for this? Thank you.
XMPPService xmpps = XMPPServiceFactory.getXMPPService();
Message msg = xmpps.parseMessage(req);
.....
......
msg = new MessageBuilder()
.withRecipientJids(jid)
.withBody(respMsg)
.build();
xmpps.sendMessage(msg);
what the above code does is, it sends the message after it receives a message from the client. my bot basically informs the clients about important announcements. so suppose i want to tell them something important without them asking. i just want to send a message and all my clients should receive it. do i need the JID of my clients? if so, then how do i get them? i am new to xmpp please help me.
Thank you.

It is not possible to broadcast messages to all jou xmpp contacts at once. You have to adress them one by one. If you look deepher in the xmpp code you will see that status updates will be broadcasted to all contacts that are not offline. Maybe you can use that to your advantage.

Related

Getting reply to a particular sms sent pragmatically from app

I'm working on an app that requires to read the reply of a particular sms message sent and update the Database, i know i can use SmsManager (Pending Intent) class to know the status of the message (Like SENT or DELIVERED) and also broadcast receiver for receiving sms but i want to be able to read the user reply to that sms.
There can be multiple message sent at a particular time which i want to be able to differentiate between response for each sms.
Is there a way to attach something like an id to sms and get it back when i receive the reply or how can i achieve this? most of the tutorials online only cover how to listen to the status and how to receive new sms.

Mqtt-client does not receive repeating message or payload when using qos2

I have a problem with my client , I dont know where to look or pinpoint the problem but as far as I know im using qos 2 and my broker is mosquitto. Does anyone have any problem with messages that are not received but delivered?
My process is like these
ClientServer(acts as a bridge to the database) subscribed to "topic1"
Client publishes a payload to "topic1"
Something went wrong then ClientServer then send back to Client that it has not been saved.
Client receives the message and send the message with correct payload again.
ClientServer doesn't receive anymore (Mostly 2 - multiple times publish)
Then i use another client to send some mqtt-client statistics to send a payload message to the ClientServer and in the ClientServer publish tokens most ImqttDeliveryToken data is pending. I dont know why is it because of QOS 2?
So is there a problem with my current pseudo-code when using qos 2 with Client(having the same unique client-id) and ClientServer(having the same unique-client-id)?
PS: What i meant about same unique client-id is that since runtime my clients dont use generated client-id to allow qos 2 to work.
I think i found my answer.
Seems like in order to get over from this problem is to enter higher number of
max_inflight_messages
(in mosquitto.conf )
to N number of messages that makes the ClientServer accommodate, it was its default was 10 that i think thats why 100+ records sent asynchonously will be pending or i don't know what happened but it stopped processing incoming messages.
As for my testing I set it temporarily to 1000.
Hope some people might enlighten me for additional information about this inflight messages?

Smack send and receive message without chat

I would like to send a simple message from one client to another one not opening a chat because there will never be a response and all messages fire the same event.
In the smack (4.1.7) documentation I found out that it is possible to do so but so far I did not find a way how to do it.
Do you have any ideas how to do it?
Would it be better (especially acording to performance: runtime and memory) to use the chat?
For receiving you'd probably want to use a synchronous stanza listener with a suitable filter.For example, if you want to receive messages with a body from user#example.org, then you could
XMPPConnection connection = …;
connection.addSyncStanzaListener(new StanzaListener() {
#Override
void process(Stanza stanza) {
Message message = (Message) stanza;
// Received new message with body from user#example.org
}, new AndFilter(MessageWithBodiesFilter.INSTANCE,
FromMatchesFilter.create("user#example.org")));
Sending messages is even easier
Message message = new Message("user#example.org", "Hi, how are you?");
XMPPConnection connection = …;
connection.sendStanza(message);
Hint: Reading the source code of Smack is a great way to learn about such stuff. If you look at the source of ChatManager, you will find what I've just written above.

Java sockets, server send message to multiple clients

I am one server and multiple clients via threads. The client(s) send their message to the server. I have worked out how to make the server send the message back to client like a echo system. If I have two clients, I want them to send their message to the server and the server should send it to the client that did not send the message i.e. the other client. How would I go about send the message back to all the clients apart from the one that send the message?
When the message comes in, determine what the userID / other identifying id the incoming message is associated with. Then re-broadcast to all other sockets, but exclude the Socket associated with the ID that sent the message
Make at the server side a list with all the clients...
every time a new msg is received, then iterate the list and send the msg using the port of the socket as id...
I recently wrote a Chat program too. What I did was, I had a class ClientHandler that handles the connection for each individual client.
Inside ClientHandler I had a HashMap. I added each client that had connected to the HashMap, with the Key being the client id. I used a UUID rather than int for the client id.
Inside this handler class, I had a sendMessage(String str) method. Within this method, a for-each loop that loops through each ClientHandler object, checking the values inside the HashMap. Inside this for-each loop, I have an if statement that checks whether you are writing to the ClientHandler object with this id. If the check returns false, you go ahead and write the message on the PrintWriter and the message won't be sent to the client writing the message.
This worked for me. Might not work for you.

SMSLib sending message, using multiple gateways

I'm using SMSLib for sending and receiving messages. Everything's working great, but now I'd like to plug more than one modem. I want to receive messages by all of my modems and do something with them (I can do that, I think). I also want to send messages, but only through the selected modem (there's my problem). Until I had one gateway i did sending like this:
OutboundMessage msg = new OutboundMessage(recipientNumber, text);
Service.getInstance().sendMessage(msg);
But now, how can I select the one specific gateway, that I want to use to send my message?
I found a topic with a problem a bit like mine, but not exactly:
Use multiple gateway with SMSLIB
Each modem is a AGatway object in SMSLib so you need to set it up first:
SerialModemGateway modemGateway = new SerialModemGateway("FirstGateway", "/dev/ttyM0", "9600", "WAVECOM", "Fastrack");
Service.getInstance().addGateway(modemGateway);
Where FirstGateway is ID of your modem which is called gatewayId in SMSLib. All you have to do now is pass your gatewayId to sendMessage method or queueMessage (if you send messages asynchronously):
OutboundMessage msg = new OutboundMessage(recipientNumber, text);
Service.getInstance().sendMessage(msg, "FirstGateway");
or:
OutboundMessage msg = new OutboundMessage(recipientNumber, text);
msg.setGatewayId("FirstGateway");
Service.getInstance().sendMessage(msg);
I didnt notice that there is such a method sendMessage() which takes gatewayId as a second agrument. If so, there will be perfect. I'll check that tomorrow, are you sure about that? I'm using SmsLib 3.x
EDIT:
It's exactly as you said. I just put gatewayId as a second argument and it's working. Another options is that you can set gatewayId of created OutboundMessage:
OutboundMessage msg = new OutboundMessage(recipientNumber, text);
msg.setGatewayId("FirstGateway");
Service.getInstance().sendMessage(msg);
So easy.. Thanks!
I would't use sendMessage method with multiple gateways, use queueMessage it adds your msg to SMSLib service queue and sends it asynchronously.
Also , if you start your application with:
-Dsmslib.queuedir=yourQueuedMessagesDirectory
you will be able to store all unsent messages on hard drive and give SMSLib service facility to send them after application restart.

Categories

Resources