{"id":22850124,"url":"https://github.com/phpzm/telegrambot","last_synced_at":"2025-03-31T06:28:16.455Z","repository":{"id":136239065,"uuid":"118333948","full_name":"phpzm/telegrambot","owner":"phpzm","description":null,"archived":false,"fork":false,"pushed_at":"2018-01-22T02:41:43.000Z","size":511,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-06T10:53:20.358Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phpzm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-21T12:45:54.000Z","updated_at":"2018-02-23T07:00:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"d265f423-ade3-4766-bc5e-96741b0f20fd","html_url":"https://github.com/phpzm/telegrambot","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpzm%2Ftelegrambot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpzm%2Ftelegrambot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpzm%2Ftelegrambot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpzm%2Ftelegrambot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phpzm","download_url":"https://codeload.github.com/phpzm/telegrambot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246427338,"owners_count":20775558,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-13T05:06:34.696Z","updated_at":"2025-03-31T06:28:16.445Z","avatar_url":"https://github.com/phpzm.png","language":"PHP","readme":"# Telegram Bot API\n\nEste projeto visa criar uma estrutura de recursos para utilizar a API de bots do Telegram.\n\n## Como usar\n\nPara entregar uma API simplificada o SDK possui uma classe que agrega as features para o bot.\n```php\nuse App\\Telegram\\Bot;\n\n$telegram = new Bot(env('BOT_TOKEN'));\n```\nCom estas instruções acima temos uma instância do Bot disponível para adicionar os comandos.\nO exemplo abaixo mostra como podemos adicionar um comando para o Bot para ao receber uma mensagem que comece com `Hello` ou `Hi` ele responda `Nice to meet you`.\n```php\n$bot-\u003eadd('^Hello|^Hi', function($bot, $match) {\n    $message = $match-\u003eget('$message');\n    $chatId = $message['chat']['id'];\n    /** @var Bot $bot */\n    return $bot-\u003eapiRequest(\n        'sendMessage', ['chat_id' =\u003e $chatId, \"text\" =\u003e 'Nice to meet you']\n    );\n});\n```\n\nO bot conta com métodos para lidar com os tipos de mensagem de forma separada.\n```php\n$body = json_decode(file_get_contents(\"php://input\"), true);\n\n// add actions to bot\n$bot-\u003eadd(/* regex on message */, /* callable */);\n$bot-\u003eadd(/* regex on message */, /* callable */);\n\n// apply the actions\n$telegram-\u003etext($body);\n```\n\n### Exemplos mais elaborados\n\nO método add suporta qualquer callable (php.net/callable) facilitando a criação dos comandos e permitindo uma organização das instruções.\n\n```php\n$bot-\u003eadd('/start', Start::class);\n\nclass Start\n{\n    /**\n     * @param Bot $bot\n     * @param array $match\n     * @throws Exception\n     */\n    function __invoke($bot, $match)\n    {\n        $message = $match-\u003eget('$message');\n        $chatId = $message['chat']['id'];\n        $parameters = [\n            'chat_id' =\u003e $chatId,\n            \"text\" =\u003e 'Hello',\n            'reply_markup' =\u003e [\n                'keyboard' =\u003e [['Hello', 'Hi']], 'one_time_keyboard' =\u003e true, 'resize_keyboard' =\u003e true\n            ]\n        ];\n        $bot-\u003eapiRequestJson('sendMessage', $parameters);\n    }\n}\n```\n\nTambém é possível adicionar um arquivo que contenha uma função para adicionar as ações\n```php\n$telegram-\u003eactions(dirname(__DIR__) . '/actions/text.php');\n```\nOnde o arquivo `actions/text.php` é algo como\n```php\n\u003c?php\n\nuse App\\Actions\\OtherWise;\nuse App\\Telegram\\Bot;\n\nreturn function (Bot $bot) {\n    $bot-\u003eadd('.*', OtherWise::class);\n};\n```\n\n### API simplificada\n\nA classe Bot possui alguns métodos simplificadas como o reply ou replyTo.\n```php\n$bot-\u003eadd('^Hello|^Hi', function($bot) {\n    return $bot-\u003ereply('Nice to meet you');\n});\n```\n\n### Interagindo com a mensagem\n\nCriando grupos de expressão regular é possível interagir com a mensagem de forma simples e rápida\n```php\n$bot-\u003eadd('How much is (.*) \\+ (.*)\\?', function ($bot, $match) {\n    /** @var Bot $bot */\n    /** @var Match $match */\n    $parameters = $match-\u003eget('$parameters');\n    if (count($parameters) !== 2) {\n        return $bot-\u003ereply('Can`t resolve this math');\n    }\n    $sum = $parameters[0] + $parameters[1];\n    return $bot-\u003ereply(\"That is easy, the answer is `{$sum}`\");\n});\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpzm%2Ftelegrambot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphpzm%2Ftelegrambot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpzm%2Ftelegrambot/lists"}