Skip to content

Instantly share code, notes, and snippets.

@devdrops
Last active December 8, 2016 03:54
Show Gist options
  • Save devdrops/474bae8a7a25f07a790c0e3d92833b1d to your computer and use it in GitHub Desktop.
Save devdrops/474bae8a7a25f07a790c0e3d92833b1d to your computer and use it in GitHub Desktop.
Telegram Update Requests

TELEGRAM BOTS

Atualizações

IDENTIFICADOR DE COMANDOS

Tanto em conversa privada como em grupo, um update sempre terá um comando quando tiver no mínimo uma entidade do tipo bot_command. Porquê no mínimo? Porque o atributo entities que existe num update com comandos é um array, o que significa que tecnicamente é possível ter mais de 1 entidade em um único update.

OBS: se não há o atributo entities na mensagem, o update deve ser ignorado pelo bot.

Logo, a ordem mais próxima do ideal é:

  1. A mensagem contém um comando?

    Para identificar isso, basta validar a presença do atributo entities.

  2. Qual é esse comando?

    Para essa parte, verificamos o tipo dessa entidade, sendo aceito somente o tipo bot_command.

Após isso, delegamos a ação do comando para sua respectiva classe, que fará a ação do comando em si. Ela precisa ter um método de evocação, cujo retorno será aquilo a passar para o Telegram numa nova request.

COMPORTAMENTOS DIFERENTES ENTRE DESKTOP E MOBILE

Notei que quando se conversa com um bot em um grupo, podemos ter no atributo text uma sutil diferença:

  • Quando executamos o comando pelo app desktop, o comando ganha a sintaxe

     /command@MyAwesomeBot
    
  • Mas quando executamos o mesmo comando por mobile, o comando fica

     /command
    

Telegram Update Requests

Em chat privado (usuário direto com o bot 1:1)

Comando não registrado no bot

{
    "update_id": 815196407,
    "message": {
        "message_id": 223,
        "from": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops"
        },
        "chat": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops",
            "type": "private"
        },
        "date": 1480884650,
        "text": "/eita Este é um comando não registrado no bot",
        "entities": [
            {
                "type": "bot_command",
                "offset": 0,
                "length": 5
            }
        ]
    }
}

Comando registrado no bot

{
    "update_id": 815196408,
    "message": {
        "message_id": 225,
        "from": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops"
        },
        "chat": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops",
            "type": "private"
        },
        "date": 1480884665,
        "text": "/wat Este é um comando registrado no bot",
        "entities": [
            {
                "type": "bot_command",
                "offset": 0,
                "length": 4
            }
        ]
    }
}

Mensagem de Texto

{
    "update_id": 815196409,
    "message": {
        "message_id": 227,
        "from": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops"
        },
        "chat": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops",
            "type": "private"
        },
        "date": 1480884675,
        "text": "E este é somente um texto"
    }
}

Emoji

{
    "update_id": 815196410,
    "message": {
        "message_id": 229,
        "from": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops"
        },
        "chat": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops",
            "type": "private"
        },
        "date": 1480885072,
        "text": "👍"
    }
}

Sticker

{
    "update_id": 815196411,
    "message": {
        "message_id": 231,
        "from": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops"
        },
        "chat": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops",
            "type": "private"
        },
        "date": 1480885159,
        "sticker": {
            "width": 482,
            "height": 512,
            "emoji": "😏",
            "thumb": {
                "file_id": "AAQEABPq5HIwAAQNLNKD7brYh4gtAAIC",
                "file_size": 3662,
                "width": 120,
                "height": 128
            },
            "file_id": "BQADBAADhAAD6MWrBd3a1lEMncqcAg",
            "file_size": 26700
        }
    }
}

Arquivo (texto)

{
    "update_id": 815196412,
    "message": {
        "message_id": 233,
        "from": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops"
        },
        "chat": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops",
            "type": "private"
        },
        "date": 1480885241,
        "document": {
            "file_name": "notas",
            "mime_type": "text/plain",
            "file_id": "BQADAQADMgADtfydBLynEUNKrhbsAg",
            "file_size": 113
        }
    }
}

Arquivo (imagem)

{
    "update_id": 815196413,
    "message": {
        "message_id": 235,
        "from": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops"
        },
        "chat": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops",
            "type": "private"
        },
        "date": 1480885305,
        "photo": [
            {
                "file_id": "AgADAQADVKgxG7X8nQQ9Qzm4pf0gdBiD5y8ABDHBE_yx88gTtDsCAAEC",
                "file_size": 1241,
                "width": 90,
                "height": 69
            },
            {
                "file_id": "AgADAQADVKgxG7X8nQQ9Qzm4pf0gdBiD5y8ABLZai-speml5tTsCAAEC",
                "file_size": 11509,
                "width": 312,
                "height": 240
            }
        ],
        "caption": "Olha que imagem bonita de se ver!"
    }
}

Em grupo (vários usuários além do bot)

Comando não registrado no bot

{
    "update_id": 815196419,
    "message": {
        "message_id": 246,
        "from": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops"
        },
        "chat": {
            "id": -127375057,
            "title": "Teste do bot",
            "type": "group",
            "all_members_are_administrators": true
        },
        "date": 1480885960,
        "text": "/potato@SatanGossBot",
        "entities": [
            {
                "type": "bot_command",
                "offset": 0,
                "length": 20
            }
        ]
    }
}

Comando registrado no bot (Desktop app)

{
    "update_id": 815196418,
    "message": {
        "message_id": 244,
        "from": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops"
        },
        "chat": {
            "id": -127375057,
            "title": "Teste do bot",
            "type": "group",
            "all_members_are_administrators": true
        },
        "date": 1480885905,
        "text": "/tweet@SatanGossBot",
        "entities": [
            {
                "type": "bot_command",
                "offset": 0,
                "length": 19
            }
        ]
    }
}

Comando registrado no bot (mobile app)

{
    "update_id": 815196430,
    "message": {
        "message_id": 262,
        "from": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops"
        },
        "chat": {
            "id": -127375057,
            "title": "Teste do bot",
            "type": "group",
            "all_members_are_administrators": true
        },
        "date": 1481082081,
        "text": "/wat",
        "entities": [
            {
                "type": "bot_command",
                "offset": 0,
                "length": 4
            }
        ]
    }
}

Mensagem de Texto

{
    "update_id": 815196420,
    "message": {
        "message_id": 248,
        "from": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops"
        },
        "chat": {
            "id": -127375057,
            "title": "Teste do bot",
            "type": "group",
            "all_members_are_administrators": true
        },
        "date": 1480886684,
        "text": "/wat@SatanGossBot EITA PREULA",
        "entities": [
            {
                "type": "bot_command",
                "offset": 0,
                "length": 17
            }
        ]
    }
}

Emoji

{
    "update_id": 815196421,
    "message": {
        "message_id": 250,
        "from": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops"
        },
        "chat": {
            "id": -127375057,
            "title": "Teste do bot",
            "type": "group",
            "all_members_are_administrators": true
        },
        "date": 1480886742,
        "text": "/wat@SatanGossBot 😄",
        "entities": [
            {
                "type": "bot_command",
                "offset": 0,
                "length": 17
            }
        ]
    }
}

Notificação do chat

{
    "update_id": 815196414,
    "message": {
        "message_id": 237,
        "from": {
            "id": 77462709,
            "first_name": "Davi",
            "last_name": "Marcondes Moreira",
            "username": "Devdrops"
        },
        "chat": {
            "id": -127375057,
            "title": "Teste do bot",
            "type": "group",
            "all_members_are_administrators": true
        },
        "date": 1480885609,
        "new_chat_participant": {
            "id": 291444591,
            "first_name": "SatanGossBot",
            "username": "SatanGossBot"
        },
        "new_chat_member": {
            "id": 291444591,
            "first_name": "SatanGossBot",
            "username": "SatanGossBot"
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment