0

我正在尝试开发一个应用程序,我想在其中监控whatsapp群聊并限制用户,例如,如果第 6 次到来,最多 5 个用户可以加入,忽略或删除最不活动的用户并添加新用户聊天我不知道我该怎么做?它是否需要root访问权限?有没有可用的 api 来做到这一点?

4

1 回答 1

0

您可以使用Python 库Yowsup 。您需要设置一个单独的 WhatsApp 帐户,该帐户使用此库登录 WhatsApp 服务器。此帐户需要成为群组的“所有者”,并且需要管理在聊天中添加和删除用户。该代码还需要监控聊天流量以确定谁在说话。您开发的代码不需要一直运行。

您将需要为许多 WhatsApp 事件注册侦听器。您的一些初始化代码将如下所示:

class WhatsappClient:
 def __init__(self):
    atexit.register(self.exitfunction)

    connectionManager = YowsupConnectionManager()
    connectionManager.setAutoPong(True)
    self.signalsInterface = connectionManager.getSignalsInterface()
    self.methodsInterface = connectionManager.getMethodsInterface()

    self.signalsInterface.registerListener("auth_success", self.onAuthSuccess)
    self.signalsInterface.registerListener("auth_fail", self.onAuthFailed)
    self.signalsInterface.registerListener("message_received", self.onMessageReceived)
    self.signalsInterface.registerListener("receipt_messageSent", self.onMessageSent)
    self.signalsInterface.registerListener("disconnected", self.onDisconnected)
    self.signalsInterface.registerListener("group_messageReceived", self.onGroup_messageReceived)   
    self.signalsInterface.registerListener("notification_groupParticipantAdded", self.onNotification_groupParticipantAdded)
    self.signalsInterface.registerListener("notification_groupParticipantRemoved", self.onNotification_groupParticipantRemoved)
    self.signalsInterface.registerListener("group_gotInfo", self.onGroup_gotInfo)
    self.signalsInterface.registerListener("group_gotParticipants", self.onGroup_gotParticipants)
    self.signalsInterface.registerListener("group_subjectReceived", self.onGroup_subjectReceived)
    self.signalsInterface.registerListener("notification_groupPictureUpdated", self.onNotification_groupPictureUpdated)
    self.signalsInterface.registerListener("group_setSubjectSuccess", self.onGroup_setSubjectSuccess)
    self.signalsInterface.registerListener("group_createSuccess", self.onGroup_createSuccess)
    self.signalsInterface.registerListener("group_createFail", self.onGroup_createFail)
    self.signalsInterface.registerListener("group_endSuccess", self.onGroup_endSuccess)
    self.signalsInterface.registerListener("group_addParticipantsSuccess", self.onGroup_addParticipantsSuccess)
    self.signalsInterface.registerListener("group_removeParticipantsSuccess", self.onGroup_removeParticipantsSuccess)

上述方法实现的一些示例:

def onGroup_messageReceived(self, messageId, gjid, author, content, timestamp, receiptRequested, pushName):
    formattedDate = datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
    log("Group message %s - %s (%s):%s"%(gjid, pushName, author, content))
    if receiptRequested:
        self.methodsInterface.call("message_ack", (gjid, messageId))
    # do something with the message....

def onGroup_addParticipantsSuccess(self, gjid, jid):
    log ("Participant added success g=%s j=%s " % (gjid, jid))

def onGroup_removeParticipantsSuccess(self, gjid, jid):
    log ("Participant removed success g=%s j=%s " % (gjid, jid))
于 2013-09-04T09:14:58.157 回答