2015-08-30 18:08:41 +00:00
|
|
|
from yowsup import env
|
|
|
|
from yowsup.stacks import YowStack
|
2015-08-30 18:51:44 +00:00
|
|
|
from yowsup.common import YowConstants
|
|
|
|
from yowsup.layers import YowLayerEvent, YowParallelLayer
|
2015-08-30 20:18:03 +00:00
|
|
|
from yowsup.layers.auth import AuthError
|
2015-08-30 18:08:41 +00:00
|
|
|
|
|
|
|
# Layers
|
2015-08-30 20:18:03 +00:00
|
|
|
from yowsup.layers.axolotl import YowAxolotlLayer
|
2015-08-30 18:08:41 +00:00
|
|
|
from yowsup.layers.auth import YowCryptLayer, YowAuthenticationProtocolLayer
|
|
|
|
from yowsup.layers.coder import YowCoderLayer
|
|
|
|
from yowsup.layers.logger import YowLoggerLayer
|
|
|
|
from yowsup.layers.network import YowNetworkLayer
|
|
|
|
from yowsup.layers.protocol_messages import YowMessagesProtocolLayer
|
|
|
|
from yowsup.layers.stanzaregulator import YowStanzaRegulator
|
|
|
|
from yowsup.layers.protocol_media import YowMediaProtocolLayer
|
|
|
|
from yowsup.layers.protocol_acks import YowAckProtocolLayer
|
|
|
|
from yowsup.layers.protocol_receipts import YowReceiptProtocolLayer
|
|
|
|
from yowsup.layers.protocol_groups import YowGroupsProtocolLayer
|
|
|
|
from yowsup.layers.protocol_presence import YowPresenceProtocolLayer
|
|
|
|
from yowsup.layers.protocol_ib import YowIbProtocolLayer
|
|
|
|
from yowsup.layers.protocol_notifications import YowNotificationsProtocolLayer
|
|
|
|
from yowsup.layers.protocol_iq import YowIqProtocolLayer
|
|
|
|
from yowsup.layers.protocol_contacts import YowContactsIqProtocolLayer
|
|
|
|
from yowsup.layers.protocol_chatstate import YowChatstateProtocolLayer
|
|
|
|
from yowsup.layers.protocol_privacy import YowPrivacyProtocolLayer
|
|
|
|
from yowsup.layers.protocol_profiles import YowProfilesProtocolLayer
|
|
|
|
from yowsup.layers.protocol_calls import YowCallsProtocolLayer
|
|
|
|
|
2015-08-30 18:51:44 +00:00
|
|
|
# ProtocolEntities
|
|
|
|
|
2015-09-06 12:06:16 +00:00
|
|
|
from yowsup.layers.protocol_acks.protocolentities import *
|
2015-08-30 18:51:44 +00:00
|
|
|
from yowsup.layers.protocol_chatstate.protocolentities import *
|
2015-09-06 12:06:16 +00:00
|
|
|
from yowsup.layers.protocol_groups.protocolentities import *
|
|
|
|
from yowsup.layers.protocol_media.protocolentities import *
|
|
|
|
from yowsup.layers.protocol_messages.protocolentities import *
|
|
|
|
from yowsup.layers.protocol_presence.protocolentities import *
|
2015-09-05 20:22:30 +00:00
|
|
|
from yowsup.layers.protocol_profiles.protocolentities import *
|
2015-09-06 12:06:16 +00:00
|
|
|
from yowsup.layers.protocol_receipts.protocolentities import *
|
|
|
|
|
|
|
|
from functools import partial
|
2015-08-30 18:51:44 +00:00
|
|
|
|
2015-08-30 19:03:11 +00:00
|
|
|
class YowsupApp(object):
|
2015-08-30 18:08:41 +00:00
|
|
|
def __init__(self):
|
|
|
|
env.CURRENT_ENV = env.S40YowsupEnv()
|
|
|
|
|
2015-08-30 20:18:03 +00:00
|
|
|
layers = (YowsupAppLayer,
|
2015-08-30 18:08:41 +00:00
|
|
|
YowParallelLayer((YowAuthenticationProtocolLayer,
|
|
|
|
YowMessagesProtocolLayer,
|
|
|
|
YowReceiptProtocolLayer,
|
|
|
|
YowAckProtocolLayer,
|
|
|
|
YowMediaProtocolLayer,
|
|
|
|
YowIbProtocolLayer,
|
|
|
|
YowIqProtocolLayer,
|
|
|
|
YowNotificationsProtocolLayer,
|
|
|
|
YowContactsIqProtocolLayer,
|
2015-09-03 18:04:29 +00:00
|
|
|
YowChatstateProtocolLayer,
|
2015-08-30 18:08:41 +00:00
|
|
|
YowCallsProtocolLayer,
|
|
|
|
YowMediaProtocolLayer,
|
|
|
|
YowPrivacyProtocolLayer,
|
|
|
|
YowProfilesProtocolLayer,
|
|
|
|
YowGroupsProtocolLayer,
|
|
|
|
YowPresenceProtocolLayer)),
|
|
|
|
YowAxolotlLayer,
|
|
|
|
YowCoderLayer,
|
|
|
|
YowCryptLayer,
|
|
|
|
YowStanzaRegulator,
|
|
|
|
YowNetworkLayer
|
|
|
|
)
|
|
|
|
self.stack = YowStack(layers)
|
|
|
|
self.stack.broadcastEvent(
|
|
|
|
YowLayerEvent(YowsupAppLayer.EVENT_START, caller = self)
|
|
|
|
)
|
|
|
|
|
2015-08-30 18:51:44 +00:00
|
|
|
def login(self, username, password):
|
|
|
|
"""Login to yowsup
|
|
|
|
|
|
|
|
Should result in onAuthSuccess or onAuthFailure to be called.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- username: (str) username in the form of 1239482382 (country code
|
|
|
|
and cellphone number)
|
|
|
|
|
|
|
|
- password: (str) base64 encoded password
|
|
|
|
"""
|
|
|
|
self.stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS,
|
|
|
|
(username, password))
|
|
|
|
self.stack.setProp(YowNetworkLayer.PROP_ENDPOINT,
|
|
|
|
YowConstants.ENDPOINTS[0])
|
|
|
|
self.stack.setProp(YowCoderLayer.PROP_DOMAIN,
|
|
|
|
YowConstants.DOMAIN)
|
|
|
|
self.stack.setProp(YowCoderLayer.PROP_RESOURCE,
|
|
|
|
env.CURRENT_ENV.getResource())
|
|
|
|
# self.stack.setProp(YowIqProtocolLayer.PROP_PING_INTERVAL, 5)
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.stack.broadcastEvent(
|
|
|
|
YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT))
|
|
|
|
except TypeError as e: # Occurs when password is not correctly formated
|
|
|
|
self.onAuthFailure('password not base64 encoded')
|
2015-09-02 15:04:49 +00:00
|
|
|
# try:
|
|
|
|
# self.stack.loop(timeout=0.5, discrete=0.5)
|
|
|
|
# except AuthError as e: # For some reason Yowsup throws an exception
|
|
|
|
# self.onAuthFailure("%s" % e)
|
2015-08-30 18:51:44 +00:00
|
|
|
|
|
|
|
def logout(self):
|
|
|
|
"""
|
|
|
|
Logout from whatsapp
|
|
|
|
"""
|
|
|
|
self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_DISCONNECT))
|
|
|
|
|
|
|
|
def sendReceipt(self, _id, _from, read, participant):
|
|
|
|
"""
|
|
|
|
Send a receipt (delivered: double-tick, read: blue-ticks)
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- _id: id of message received
|
2015-09-05 20:05:34 +00:00
|
|
|
- _from: jid of person who sent the message
|
|
|
|
- read: ('read' or None) None is just delivered, 'read' is read
|
2015-08-30 18:51:44 +00:00
|
|
|
- participant
|
|
|
|
"""
|
|
|
|
receipt = OutgoingReceiptProtocolEntity(_id, _from, read, participant)
|
2015-09-02 15:04:49 +00:00
|
|
|
self.sendEntity(receipt)
|
2015-08-30 18:51:44 +00:00
|
|
|
|
|
|
|
def sendTextMessage(self, to, message):
|
|
|
|
"""
|
|
|
|
Sends a text message
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- to: (xxxxxxxxxx@s.whatsapp.net) who to send the message to
|
|
|
|
- message: (str) the body of the message
|
|
|
|
"""
|
|
|
|
messageEntity = TextMessageProtocolEntity(message, to = to)
|
2015-08-30 20:18:03 +00:00
|
|
|
self.sendEntity(messageEntity)
|
2015-08-30 18:51:44 +00:00
|
|
|
|
2015-09-02 15:04:49 +00:00
|
|
|
def sendPresence(self, available):
|
|
|
|
"""
|
|
|
|
Send presence to whatsapp
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- available: (boolean) True if available false otherwise
|
|
|
|
"""
|
|
|
|
if available:
|
|
|
|
self.sendEntity(AvailablePresenceProtocolEntity())
|
|
|
|
else:
|
|
|
|
self.sendEntity(UnavailablePresenceProtocolEntity())
|
2015-09-03 18:04:29 +00:00
|
|
|
|
|
|
|
def subscribePresence(self, phone_number):
|
|
|
|
"""
|
|
|
|
Subscribe to presence updates from phone_number
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- phone_number: (str) The cellphone number of the person to
|
|
|
|
subscribe to
|
|
|
|
"""
|
|
|
|
jid = phone_number + '@s.whatsapp.net'
|
|
|
|
entity = SubscribePresenceProtocolEntity(jid)
|
|
|
|
self.sendEntity(entity)
|
|
|
|
|
|
|
|
def unsubscribePresence(self, phone_number):
|
|
|
|
"""
|
|
|
|
Unsubscribe to presence updates from phone_number
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- phone_number: (str) The cellphone number of the person to
|
|
|
|
unsubscribe from
|
|
|
|
"""
|
|
|
|
jid = phone_number + '@s.whatsapp.net'
|
|
|
|
entity = UnsubscribePresenceProtocolEntity(jid)
|
|
|
|
self.sendEntity(entity)
|
2015-09-02 15:04:49 +00:00
|
|
|
|
|
|
|
def setStatus(self, statusText):
|
|
|
|
"""
|
|
|
|
Send status to whatsapp
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- statusTest: (str) Your whatsapp status
|
|
|
|
"""
|
2015-09-05 20:22:30 +00:00
|
|
|
iq = SetStatusIqProtocolEntity(statusText)
|
|
|
|
self.sendIq(iq)
|
2015-09-03 18:04:29 +00:00
|
|
|
|
|
|
|
def sendTyping(self, phoneNumber, typing):
|
|
|
|
"""
|
|
|
|
Notify buddy using phoneNumber that you are typing to him
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- phoneNumber: (str) cellphone number of the buddy you are typing to.
|
|
|
|
- typing: (bool) True if you are typing, False if you are not
|
|
|
|
"""
|
|
|
|
jid = phoneNumber + '@s.whatsapp.net'
|
|
|
|
if typing:
|
|
|
|
state = OutgoingChatstateProtocolEntity(
|
|
|
|
ChatstateProtocolEntity.STATE_TYPING, jid
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
state = OutgoingChatstateProtocolEntity(
|
|
|
|
ChatstateProtocolEntity.STATE_PAUSED, jid
|
|
|
|
)
|
|
|
|
self.sendEntity(state)
|
|
|
|
|
|
|
|
def requestLastSeen(self, phoneNumber, success = None, failure = None):
|
|
|
|
"""
|
|
|
|
Requests when user was last seen.
|
|
|
|
Args:
|
|
|
|
- phone_number: (str) the phone number of the user
|
|
|
|
- success: (func) called when request is successfully processed.
|
|
|
|
The first argument is the number, second argument is the seconds
|
|
|
|
since last seen.
|
|
|
|
- failure: (func) called when request has failed
|
|
|
|
"""
|
|
|
|
iq = LastseenIqProtocolEntity(phoneNumber + '@s.whatsapp.net')
|
2015-09-06 12:06:16 +00:00
|
|
|
self.sendIq(iq, onSuccess = partial(self._lastSeenSuccess, success),
|
|
|
|
onError = failure)
|
2015-09-05 20:22:30 +00:00
|
|
|
|
2015-09-06 12:06:16 +00:00
|
|
|
def _lastSeenSuccess(self, success, response, request):
|
|
|
|
success(response._from.split('@')[0], response.seconds)
|
2015-09-02 15:04:49 +00:00
|
|
|
|
2015-09-05 22:14:12 +00:00
|
|
|
def requestProfilePicture(self, phoneNumber, onSuccess = None, onFailure = None):
|
|
|
|
"""
|
|
|
|
Requests profile picture of whatsapp user
|
|
|
|
Args:
|
|
|
|
- phoneNumber: (str) the phone number of the user
|
|
|
|
- success: (func) called when request is successfully processed.
|
|
|
|
- failure: (func) called when request has failed
|
|
|
|
"""
|
|
|
|
iq = GetPictureIqProtocolEntity(phoneNumber + '@s.whatsapp.net')
|
|
|
|
self.sendIq(iq, onSuccess = onSuccess, onError = onFailure)
|
2015-09-06 12:06:16 +00:00
|
|
|
|
|
|
|
def requestGroupsList(self, onSuccess = None, onFailure = None):
|
|
|
|
iq = ListGroupsIqProtocolEntity()
|
|
|
|
self.sendIq(iq, onSuccess = onSuccess, onError = onFailure)
|
2015-09-05 22:14:12 +00:00
|
|
|
|
2015-08-30 18:08:41 +00:00
|
|
|
def onAuthSuccess(self, status, kind, creation, expiration, props, nonce, t):
|
|
|
|
"""
|
|
|
|
Called when login is successful.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- status
|
|
|
|
- kind
|
|
|
|
- creation
|
|
|
|
- expiration
|
|
|
|
- props
|
|
|
|
- nonce
|
|
|
|
- t
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onAuthFailure(self, reason):
|
|
|
|
"""
|
|
|
|
Called when login is a failure
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- reason: (str) Reason for the login failure
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onReceipt(self, _id, _from, timestamp, type, participant, offline, items):
|
|
|
|
"""
|
|
|
|
Called when a receipt is received (double tick or blue tick)
|
|
|
|
|
|
|
|
Args
|
|
|
|
- _id
|
|
|
|
- _from
|
|
|
|
- timestamp
|
|
|
|
- type: Is 'read' for blue ticks and None for double-ticks
|
|
|
|
- participant: (dxxxxxxxxxx@s.whatsapp.net) delivered to or
|
|
|
|
read by this participant in group
|
|
|
|
- offline: (True, False or None)
|
|
|
|
- items
|
|
|
|
"""
|
2015-09-02 15:04:49 +00:00
|
|
|
pass
|
2015-08-30 18:08:41 +00:00
|
|
|
|
|
|
|
def onAck(self, _id,_class, _from, timestamp):
|
|
|
|
"""
|
|
|
|
Called when Ack is received
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- _id
|
|
|
|
- _class: ('message', 'receipt' or something else?)
|
|
|
|
- _from
|
|
|
|
- timestamp
|
|
|
|
"""
|
|
|
|
pass
|
2015-09-03 18:04:29 +00:00
|
|
|
|
|
|
|
def onPresenceReceived(self, _type, name, _from, last):
|
|
|
|
"""
|
|
|
|
Called when presence (e.g. available, unavailable) is received
|
|
|
|
from whatsapp
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- _type: (str) 'available' or 'unavailable'
|
|
|
|
- _name
|
|
|
|
- _from
|
|
|
|
- _last
|
|
|
|
"""
|
|
|
|
pass
|
2015-08-30 18:08:41 +00:00
|
|
|
|
2015-08-30 18:51:44 +00:00
|
|
|
def onDisconnect(self):
|
|
|
|
"""
|
|
|
|
Called when disconnected from whatsapp
|
|
|
|
"""
|
2015-08-30 20:18:03 +00:00
|
|
|
|
2015-09-03 18:04:29 +00:00
|
|
|
def onContactTyping(self, number):
|
|
|
|
"""
|
|
|
|
Called when contact starts to type
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- number: (str) cellphone number of contact
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onContactPaused(self, number):
|
|
|
|
"""
|
|
|
|
Called when contact stops typing
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- number: (str) cellphone number of contact
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2015-09-05 20:05:34 +00:00
|
|
|
def onTextMessage(self, _id, _from, to, notify, timestamp, participant, offline, retry, body):
|
|
|
|
"""
|
|
|
|
Called when text message is received
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- _id:
|
|
|
|
- _from: (str) jid of of sender
|
|
|
|
- to:
|
|
|
|
- notify: (str) human readable name of _from (e.g. John Smith)
|
|
|
|
- timestamp:
|
2015-09-06 12:06:16 +00:00
|
|
|
- participant: (str) jid of user who sent the message in a groupchat
|
2015-09-05 20:05:34 +00:00
|
|
|
- offline:
|
|
|
|
- retry:
|
|
|
|
- body: The content of the message
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onImage(self, entity):
|
|
|
|
"""
|
|
|
|
Called when image message is received
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- entity: ImageDownloadableMediaMessageProtocolEntity
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onAudio(self, entity):
|
|
|
|
"""
|
|
|
|
Called when audio message is received
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- entity: AudioDownloadableMediaMessageProtocolEntity
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def onVideo(self, entity):
|
|
|
|
"""
|
|
|
|
Called when video message is received
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- entity: VideoDownloadableMediaMessageProtocolEntity
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onVCard(self, _id, _from, name, card_data, to, notify, timestamp, participant):
|
|
|
|
"""
|
|
|
|
Called when VCard message is received
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- _id: (str) id of entity
|
|
|
|
- _from:
|
|
|
|
- name:
|
|
|
|
- card_data:
|
|
|
|
- to:
|
|
|
|
- notify:
|
|
|
|
- timestamp:
|
|
|
|
- participant:
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2015-08-30 20:18:03 +00:00
|
|
|
def sendEntity(self, entity):
|
|
|
|
"""Sends an entity down the stack (as if YowsupAppLayer called toLower)"""
|
|
|
|
self.stack.broadcastEvent(YowLayerEvent(YowsupAppLayer.TO_LOWER_EVENT,
|
|
|
|
entity = entity
|
|
|
|
))
|
2015-09-05 20:22:30 +00:00
|
|
|
|
|
|
|
def sendIq(self, iq, onSuccess = None, onError = None):
|
|
|
|
self.stack.broadcastEvent(
|
|
|
|
YowLayerEvent(
|
|
|
|
YowsupAppLayer.SEND_IQ,
|
|
|
|
iq = iq,
|
|
|
|
success = onSuccess,
|
|
|
|
failure = onError,
|
|
|
|
)
|
|
|
|
)
|
2015-08-30 18:51:44 +00:00
|
|
|
|
2015-08-30 18:08:41 +00:00
|
|
|
from yowsup.layers.interface import YowInterfaceLayer, ProtocolEntityCallback
|
|
|
|
|
|
|
|
class YowsupAppLayer(YowInterfaceLayer):
|
2015-08-30 20:18:03 +00:00
|
|
|
EVENT_START = 'transwhat.event.YowsupAppLayer.start'
|
2015-09-03 18:04:29 +00:00
|
|
|
TO_LOWER_EVENT = 'transwhat.event.YowsupAppLayer.toLower'
|
|
|
|
SEND_IQ = 'transwhat.event.YowsupAppLayer.sendIq'
|
2015-08-30 18:08:41 +00:00
|
|
|
|
|
|
|
def onEvent(self, layerEvent):
|
|
|
|
# We cannot pass instance varaibles in through init, so we use an event
|
|
|
|
# instead
|
|
|
|
# Return False if you want the event to propogate down the stack
|
|
|
|
# return True otherwise
|
|
|
|
if layerEvent.getName() == YowsupAppLayer.EVENT_START:
|
|
|
|
self.caller = layerEvent.getArg('caller')
|
2015-08-30 18:51:44 +00:00
|
|
|
return True
|
|
|
|
elif layerEvent.getName() == YowNetworkLayer.EVENT_STATE_DISCONNECTED:
|
|
|
|
self.caller.onDisconnect()
|
|
|
|
return True
|
2015-08-30 20:18:03 +00:00
|
|
|
elif layerEvent.getName() == YowsupAppLayer.TO_LOWER_EVENT:
|
|
|
|
self.toLower(layerEvent.getArg('entity'))
|
|
|
|
return True
|
2015-09-03 18:04:29 +00:00
|
|
|
elif layerEvent.getName() == YowsupAppLayer.SEND_IQ:
|
|
|
|
iq = layerEvent.getArg('iq')
|
|
|
|
success = layerEvent.getArg('success')
|
|
|
|
failure = layerEvent.getArg('failure')
|
|
|
|
self._sendIq(iq, success, failure)
|
|
|
|
return True
|
|
|
|
return False
|
2015-08-30 18:08:41 +00:00
|
|
|
|
|
|
|
@ProtocolEntityCallback('success')
|
|
|
|
def onAuthSuccess(self, entity):
|
|
|
|
# entity is SuccessProtocolEntity
|
2015-08-30 18:51:44 +00:00
|
|
|
status = entity.status
|
|
|
|
kind = entity.kind
|
|
|
|
creation = entity.creation
|
|
|
|
expiration = entity.expiration
|
|
|
|
props = entity.props
|
|
|
|
nonce = entity.nonce
|
|
|
|
t = entity.t # I don't know what this is
|
2015-08-30 18:08:41 +00:00
|
|
|
self.caller.onAuthSuccess(status, kind, creation, expiration, props, nonce, t)
|
|
|
|
|
|
|
|
@ProtocolEntityCallback('failure')
|
|
|
|
def onAuthFailure(self, entity):
|
|
|
|
# entity is FailureProtocolEntity
|
|
|
|
reason = entity.reason
|
|
|
|
self.caller.onAuthFailure(reason)
|
|
|
|
|
|
|
|
@ProtocolEntityCallback('receipt')
|
|
|
|
def onReceipt(self, entity):
|
|
|
|
"""Sends ack automatically"""
|
|
|
|
# entity is IncomingReceiptProtocolEntity
|
|
|
|
ack = OutgoingAckProtocolEntity(entity.getId(),
|
|
|
|
'receipt', entity.getType(), entity.getFrom())
|
|
|
|
self.toLower(ack)
|
|
|
|
_id = entity._id
|
|
|
|
_from = entity._from
|
|
|
|
timestamp = entity.timestamp
|
2015-08-30 18:51:44 +00:00
|
|
|
type = entity.type
|
|
|
|
participant = entity.participant
|
2015-08-30 18:08:41 +00:00
|
|
|
offline = entity.offline
|
|
|
|
items = entity.items
|
|
|
|
self.caller.onReceipt(_id, _from, timestamp, type, participant, offline, items)
|
|
|
|
|
|
|
|
@ProtocolEntityCallback('ack')
|
2015-08-30 19:03:11 +00:00
|
|
|
def onAck(self, entity):
|
2015-08-30 18:08:41 +00:00
|
|
|
# entity is IncomingAckProtocolEntity
|
|
|
|
self.caller.onAck(
|
|
|
|
entity._id,
|
|
|
|
entity._class,
|
|
|
|
entity._from,
|
|
|
|
entity.timestamp
|
|
|
|
)
|
|
|
|
|
|
|
|
@ProtocolEntityCallback('notification')
|
|
|
|
def onNotification(self, entity):
|
|
|
|
"""
|
|
|
|
Sends ack automatically
|
|
|
|
"""
|
2015-09-03 18:04:29 +00:00
|
|
|
self.toLower(entity.ack())
|
2015-08-30 18:08:41 +00:00
|
|
|
|
2015-09-03 18:04:29 +00:00
|
|
|
@ProtocolEntityCallback('message')
|
2015-08-30 18:08:41 +00:00
|
|
|
def onMessageReceived(self, entity):
|
2015-09-05 20:05:34 +00:00
|
|
|
if entity.getType() == MessageProtocolEntity.MESSAGE_TYPE_TEXT:
|
|
|
|
self.caller.onTextMessage(
|
|
|
|
entity._id,
|
|
|
|
entity._from,
|
|
|
|
entity.to,
|
|
|
|
entity.notify,
|
|
|
|
entity.timestamp,
|
|
|
|
entity.participant,
|
|
|
|
entity.offline,
|
|
|
|
entity.retry,
|
|
|
|
entity.body
|
|
|
|
)
|
|
|
|
elif entity.getType() == MessageProtocolEntity.MESSAGE_TYPE_MEDIA:
|
|
|
|
if isinstance(entity, ImageDownloadableMediaMessageProtocolEntity):
|
|
|
|
# There is just way too many fields to pass them into the
|
|
|
|
# function
|
|
|
|
self.caller.onImage(entity)
|
|
|
|
elif isinstance(entity, AudioDownloadableMediaMessageProtocolEntity):
|
|
|
|
self.caller.onAudio(entity)
|
|
|
|
elif isinstance(entity, VideoDownloadableMediaMessageProtocolEntity):
|
|
|
|
self.caller.onVideo(entity)
|
|
|
|
elif isinstance(entity, VCardMediaMessageProtocolEntity):
|
|
|
|
self.caller.onVCard(
|
|
|
|
entity._id,
|
|
|
|
entity._from,
|
|
|
|
entity.name,
|
|
|
|
entity.card_data,
|
|
|
|
entity.to,
|
|
|
|
entity.notify,
|
|
|
|
entity.timestamp,
|
|
|
|
entity.participant
|
|
|
|
)
|
2015-09-03 18:04:29 +00:00
|
|
|
|
|
|
|
@ProtocolEntityCallback('presence')
|
|
|
|
def onPresenceReceived(self, presence):
|
|
|
|
_type = presence.getType()
|
|
|
|
name = presence.getName()
|
|
|
|
_from = presence.getFrom()
|
|
|
|
last = presence.getLast()
|
|
|
|
self.caller.onPresenceReceived(_type, name, _from, last)
|
|
|
|
|
|
|
|
@ProtocolEntityCallback('chatstate')
|
|
|
|
def onChatstate(self, chatstate):
|
|
|
|
number = chatstate._from.split('@')[0]
|
|
|
|
if chatstate.getState() == ChatstateProtocolEntity.STATE_TYPING:
|
|
|
|
self.caller.onContactTyping(number)
|
|
|
|
else:
|
|
|
|
self.caller.onContactPaused(number)
|