返回首页

Green-API for MAX:用户机器人和自动化

本资料描述了使用 Green-API 在 MAX 中创建用户机器人。涵盖发送消息、解析历史记录、群组管理的方法,并附代码示例和限制。实际案例:审核、邀请、报告。

Green-API 上的 MAX 用户机器人:代码和限制
Advertisement 728x90

Green-API 自动化 MAX 用户机器人:方法、限制与代码实现

MAX 中的用户机器人是一个已验证手机号码的客户端账户,通过脚本控制,没有官方 API。Green-API 提供 HTTP 接口,支持发送消息、解析聊天记录和管理群组。主要限制:每秒 50 条消息、历史记录和群组每秒 1 次请求。严格遵守速率限制,避免被封禁。

消息处理核心方法

发送支持最长 4000 字符文本、通过 URL 或上传文件、位置和 vCard 联系人。

  • SendMessage:基础文本发送,支持表情符号。
  • SendFileByUrl:外部链接媒体文件。
  • SendFileByUpload:本地文件通过 multipart/form-data 上传。
  • SendLocation:经纬度和地点名称。
  • SendContact:联系人名片。

从聊天下载文件:DownloadFile 支持图片、文档、音频和视频。

Google AdInline article slot

聊天记录解析:GetChatHistory 可拉取最多 5000 条消息(3 个月内),支持日期过滤。

POST {{apiUrl}}/waInstance{{idInstance}}/getChatHistory/{{apiTokenInstance}}

参数示例:

url = "https://3100.api.green-api.com/waInstance{id}/getChatHistory/{token}"
payload = {
    "chatId": "-730*******5943", 
    "count": 500
}
headers = {
    'Content-Type': 'application/json'
}

响应包含消息类型(收/发)、idMessage、时间戳、textMessage 和 extendedTextMessage 详情。

Google AdInline article slot
[
    {
        "type": "outgoing",
        "idMessage": "1763115112345",
        "timestamp": 1754999812,
        "typeMessage": "extendedTextMessage",
        "chatId": "10000000",
        "textMessage": "我正在使用 GREEN-API 发送这条消息!",
        "extendedTextMessage": {
            "text": "我正在使用 GREEN-API 发送这条消息!",
            "description": "",
            "title": "",
            "jpegThumbnail": "",
            "forwardingScore": 0,
            "isForwarded": false
        },
        "statusMessage": "",
        "sendByApi": true
    }
]

其他方法:GetMessage(按 ID 获取)、LastIncomingMessages、LastOutgoingMessages。

消息限制:

  • 发送消息/文件/位置/联系人:≤50/秒。
  • 文件下载、聊天记录、群组:≤1/秒。
  • 编辑:≤50/秒。

群组管理

创建群组:CreateGroup 立即添加参与者(AddGroupParticipant)并设置头像(SetGroupPicture)。

Google AdInline article slot

更新:UpdateGroupName、UpdateGroupSettings 设置权限。

{
  "chatId": "-10000000000000",
  "allowParticipantsAddMembers": true,
  "allowParticipantsEditGroupSettings": false,
  "allowParticipantsPinMessages": true
}

GetGroupData 返回:

  • chatId、群主、群名称、描述。
  • 创建时间(Unix 时间戳)。
  • 群邀请链接(如果可用)。
  • 权限:添加成员、编辑设置、置顶消息。
  • 成员数。
  • 参与者列表(管理员;成员 <100 时返回完整列表)。
{
  "chatId": "-10000000000000",
  "owner": "79001234567",
  "subject": "群组名称",
  "description": "群组描述",
  "creation": 1764637936,
  "groupInviteLink": "https://invite.link/abc123",
  "allowParticipantsAddMembers": true,
  "allowParticipantsEditGroupSettings": false,
  "allowParticipantsPinMessages": true,
  "size": 150,
  "participants": [
    {
      "chatId": "79001234567",
      "isAdmin": true,
      "isSuperAdmin": true
    }
  ]
}

成员管理:AddGroupParticipant、RemoveGroupParticipant、SetGroupAdmin、RemoveAdmin。

实用工具方法

  • GetChats:聊天列表(群组 chatId <0,phoneNumber=0)。
[
  {
    "chatId": "-10000000000000",
    "phoneNumber": 0
  },
  {
    "chatId": "79001234567",
    "phoneNumber": 79001234567
  }
]
  • CheckAccount:注册状态(exist, chatId)。严格限制,使用过多易被封禁。
  • GetContacts:手机通讯录联系人。
[
  {
    "chatId": "79001234567",
    "name": "John Doe",
    "contactName": "John",
    "type": "user",
    "phoneNumber": 79001234567
  }
]

实际应用案例

群组审核: 解析通知,检查违禁词,删除并回复。

def moderate_group_messages():
    notifications = client.receive_notification()
    
    for notification in notifications:
        if notification['type'] == 'incomingMessageReceived':
            message = notification['messageData']['textMessage']
            chat_id = notification['senderData']['chatId']
            
            if is_spam(message):
                client.delete_message(chat_id, message_id)
                client.send_message(
                    chat_id, 
                    "⚠️ 违反聊天规则"
                )

自动邀请: 按部门分发。

def add_employee_to_groups(phone, department):
    groups = get_groups_by_department(department)
    
    for group in groups:
        client.add_group_participant(
            chat_id=group['chatId'],
            participant_chat_id=phone
        )

报表生成: 从 CRM 拉取数据,推送到聊天。

def send_daily_report():
    stats = get_daily_statistics()
    
    report = f"📊 {date.today()} 日报:\n"
    report += f"新订单:{stats['orders']} 个\n"
    report += f"收入:${stats['revenue']} \n"
    
    client.send_message(report_target_chat, report)

关键要点

  • 严格遵守限制:发送 50/秒,群组/历史 1/秒,超限易被封。
  • GetChatHistory:最多 5000 条/3 个月,大群用日期过滤。
  • 群组 >100 人:API 只显示机器人角色,无法完整解析成员。
  • CheckAccount 慎用,风险 2 周封禁。
  • 替代方案 PyMax:开源、无 SLA、自托管。

— Editorial Team

Advertisement 728x90

继续阅读