Sending message to server using python SocketIO - python-2.7

I have a situation where I have a python client try to send a message to a server using SocketIO. I'm using sock.emit to send a json message as shown below
sock = SocketIO(url, None, ...)
message = {'ob': 'house',
'ids': ['54fjadb70f9756','39f1ax451f6567']}
self.sock.emit(message)
server expects the follow:
{"ob":"house","ids":['54fjadb70f9756','39f1ax451f6567']}
the server is current receive the following message:
{"args": [], "name": "{\"ids\": [\"54fjadb70f9756\", \"39f1ax451f6567\"], \"ob\": \"house\"}"}
then complains about an invalid message.
How would I send the message so that the server receives it as expected i.e which the original message structure I specified? it seems emit is changing the structure of the message when I send it, is it possible to override and change emit to that it retain the original message structure? is so how would I do that?

Socket.io .emit() takes two arguments. The first argument is a message name which should be a plain string. The second argument is optional and can be data which will be JSON encoded data.
So, you are trying to send your data as the message name. That will not work. You need to create a message name and send the data as the argument for that message name.
sock = SocketIO(url, None, ...)
message = {'ob': 'house',
'ids': ['54fjadb70f9756','39f1ax451f6567']}
sock.emit("someMessageName", message)
Then, on the server, you need to have a listener for that specific message name and then you can receive the data when that message listener is invoked.

Related

update() got an unexpected keyword argument 'status' Deleting Whatsapp Message using Flask twilio

I created a WhatsApp Chatbot using Flask and Twilio. Then I wanted to be able to delete older messages that were already sent and received. I save message IDs in the table so that I should delete them later. On Twilio documentation delete-a-message-resource
They say it is possible to delete them but I am getting the above error
below is the code
select_cur = mysql.connection.cursor()
select_cur.execute(""" select * from message; """)
for row in select_cur.fetchall():
date_diff = datetime.datetime.now()-row[4]
date_diff_minutes = date_diff.total_seconds() / 60
print(date_diff_minutes)
if date_diff_minutes>30:
try:
#client.messages(row[1]).update(body="")
client.messages(row[1]).update(status='canceled')
except Exception as e:
print(e)
select_cur.close()
I tried to update the message ID using an empty body but it doesn't do anything to those older messages.

Akka TcpPipeLine: how can I send message to a client/server without receiving a init.Event first?

folks!
I'm using akka 2.2.3 and developing simple tcp server application.
The work flow is:
1. client connects to server
2. server accepts connection,
3. server sends to client message "Hello!"
On page http://doc.akka.io/docs/akka/2.2.3/scala/io-tcp.html I can see how I can send response message to request. But, how can I send message before some data was received?
How can I send message to a client without receiving a init.Event first?
Code from documentation page:
class AkkaSslHandler(init: Init[WithinActorContext, String, String])
extends Actor with ActorLogging {
def receive = {
case init.Event(data) ⇒
val input = data.dropRight(1)
log.debug("akka-io Server received {} from {}", input, sender)
val response = serverResponse(input)
sender ! init.Command(response)
log.debug("akka-io Server sent: {}", response.dropRight(1))
case _: Tcp.ConnectionClosed ⇒ context.stop(self)
}
}
You use the init for creating the TcpPipelineHandler as well, and you can of course always send commands to that actor. For this you will need to pass its ActorRef to your handler actor besides the Init.

Capture when an email is actually sent

I'm working on an application that captures some Outlook events. Is it possible to detect the event when an email is sent. I don't mean when the user clicks on "send" button but when the email is actually sent and is no more in the outbox. Thanks in advance.
UPDATE
I managed to capture the Items.ItemAdd event on the sent mails folder. In order to check whether the last email added to the sent items folder is the one I'm interestd in I tried this :
STDMETHODIMP CItemsEventListener::HandleItemAdd(CString p_date, CString p_time)
{
CComPtr<Outlook::_Application> spApplication;
CComPtr<Outlook::_NameSpace> spSession;
CComPtr<Outlook::MAPIFolder> spSentMailsFolder;
CComPtr<Outlook::_Items> pSentboxItems;
spSentMailsFolder->get_Items(&pSentboxItems);
CComPtr<Outlook::_MailItem> pSentMail;
pSentMail = pSentboxItems->GetLast();
//do staff
return S_OK;
}`
I'm getting compiler error :
error C2660: 'Outlook::_Items::GetLast' : the function doesn't take 0 arguments
Which argument should I pass to the function?
MailItem.Send and Application.ItemSend events occur before the message is actually sent.
To capture when a message is actually sent, use the Items.ItemAdd event on the Sent Items folder.

how to send json header in websocket tornado python?

def on_message(self, message):
for client in ChatWebSocket.clients:
print(client)
t=json.loads(message)
client.write_message(json.dumps('Content-type:application/json\n'))
client.write_message(json.dumps({'a':t['b']}))
print(t['b'])
the problem is client is accepting it like a normal string and not a header
please help
From Tornado's documentation:
WebSocketHandler.write_message(message, binary=False)
Sends the given message to the client of this Web Socket.
The message may be either a string or a dict (which will be encoded as json). If the binary argument is false, the message will be sent as utf8; in binary mode any byte string is allowed.
So you don't need to dump anything. Just send the dict as is and Tornado will encode it as JSON anyways:
false, the message will be sent as utf8; in binary mode any byte string is allowed.
So you don't need to dump anything. Just send the dict as is and Tornado will
def on_message(self, message):
for client in ChatWebSocket.clients:
print(client)
t = json.loads(message)
client.write_message({'a': t['b']})
print(t['b'])

How to implement proxy/broker for (X)PUB/(X)SUB messaging in ZMQ?

So I was reading this article on how to create proxy/broker for (X)PUB/(X)SUB messaging in ZMQ. There is this nice picture of what shall architecture look like :
But when I look at XSUB socket description I do not get how to forward all subscriptions via it due to the fact that its Outgoing routing strategy is N/A
So how one shall implement (un)subscription forwarding in ZeroMQ, what is minimal user code for such forwarding application (one that can be inserted between simple Publisher and Subscriber samples)?
XPUB does receive messages - the only messages it receives are subscriptions from connected subscribers, and these messages should be forwarded upstream as-is via XSUB.
The very simplest way to relay messages is with zmq_proxy:
xpub = ctx.socket(zmq.XPUB)
xpub.bind(xpub_url)
xsub = ctx.socket(zmq.XSUB)
xsub.bind(xsub_url)
pub = ctx.socket(zmq.PUB)
pub.bind(pub_url)
zmq.proxy(xpub, xsub, pub)
which will relay messages to/from xpub and xsub. Optionally, you can add a PUB socket to monitor the traffic that passes through in either direction.
If you want user code in the middle to implement extra routing logic, you would do something like this,
which re-implements the inner loop of zmq_proxy:
def broker(ctx):
xpub = ctx.socket(zmq.XPUB)
xpub.bind(xpub_url)
xsub = ctx.socket(zmq.XSUB)
xsub.bind(xsub_url)
poller = zmq.Poller()
poller.register(xpub, zmq.POLLIN)
poller.register(xsub, zmq.POLLIN)
while True:
events = dict(poller.poll(1000))
if xpub in events:
message = xpub.recv_multipart()
print "[BROKER] subscription message: %r" % message[0]
xsub.send_multipart(message)
if xsub in events:
message = xsub.recv_multipart()
# print "publishing message: %r" % message
xpub.send_multipart(message)
# insert user code here
full working (Python) example