{"id":13742547,"url":"https://github.com/kaiheila/php-bot","last_synced_at":"2026-01-13T22:46:53.874Z","repository":{"id":45257929,"uuid":"322195238","full_name":"kaiheila/php-bot","owner":"kaiheila","description":"开黑啦机器人的php版本","archived":false,"fork":false,"pushed_at":"2025-03-31T08:22:28.000Z","size":40,"stargazers_count":25,"open_issues_count":0,"forks_count":9,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-12-27T19:51:26.071Z","etag":null,"topics":["bot","kaiheila","php","sdk"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kaiheila.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-17T05:53:03.000Z","updated_at":"2025-03-31T08:22:32.000Z","dependencies_parsed_at":"2023-02-08T05:30:53.575Z","dependency_job_id":null,"html_url":"https://github.com/kaiheila/php-bot","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/kaiheila/php-bot","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaiheila%2Fphp-bot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaiheila%2Fphp-bot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaiheila%2Fphp-bot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaiheila%2Fphp-bot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kaiheila","download_url":"https://codeload.github.com/kaiheila/php-bot/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaiheila%2Fphp-bot/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28403707,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T14:36:09.778Z","status":"ssl_error","status_checked_at":"2026-01-13T14:35:19.697Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["bot","kaiheila","php","sdk"],"created_at":"2024-08-03T05:00:33.509Z","updated_at":"2026-01-13T22:46:53.848Z","avatar_url":"https://github.com/kaiheila.png","language":"PHP","funding_links":[],"categories":["SDK"],"sub_categories":["官方服务器"],"readme":"# Bot\n本代码是[kaiheila](http://developer.kaiheila.cn/doc)机器人的php示例sdk， 用户可以直接使用该代码或者参照该代码来构建自己的机器人。\n\n 本代码既可以当成一个composer包，来供系统调用。也可以当成一个独立的机器人来运行。在本代码中有一个重要的概念叫session。当websocket/webhook在线时，我们认为它和服务器保持了一个session,然后我们可以通过session来处理数据了。\n\n## 代码说明\n该包依赖了swoole，请确定你安装了[swoole扩展](https://wiki.swoole.com/#/environment?id=%e5%ae%89%e8%a3%85swoole)。\n\n### docker \n如果你觉得安装php相关环境比较麻烦，我们也提供了docker来方便你的使用[php-bot](https://hub.docker.com/r/kaiheila/php-bot):\n\n```bash\ndocker pull kaiheila/php-bot\n```\n\n### 代码使用\n\n```php \n//$session = new WebsocketSession();\n$session = new WebhookSession();\n\n// 注册接收frame事件回调，当session收到了正确的frame数据时，就会调用此方法\n$session-\u003eon($session::EVENT_RECEIVE_FRAME, function($frame){\n    \n});\n\n// 事件名默认为 channel_type + _ + type组成， 如下代表侦听群聊的文字消息\n$session-\u003eon(\"GROUP_1\", function($frame){\n    \n});\n\n// 事件名支持通配符匹配，如下代表侦听群聊的所有消息\n$session-\u003eon(\"GROUP*\", function($frame){\n     \n});\n\n// 事件默认是采用了swoole的协程进行异步回调，如果需要同步回调，如下所示\n$session-\u003eon(\"GROUP*\", function(){}, ['async' =\u003e false]);\n\n// 通过webhook/websocket收到消息后，把数据传给session处理即可，session就会自动按上面注册的事件进行处理。\n$session-\u003ereceiveData($data);\n\n```\n\n在回调中，我们通常会跟据服务端返回的消息，来做一些动作，我们统一封装了ApiClient:\n```\n$client = new \\kaiheila\\api\\helpers\\ApiHelper($path);\n// post json示例\n$client-\u003esetBody([\"foo\" =\u003e \"bar\"])-\u003esend(\\kaiheila\\api\\helpers\\ApiHelper::POST);\n// get示例\n$client-\u003esetQuery([\"foo\" =\u003e \"bar\"])-\u003esend(\\kaiheila\\api\\helpers\\ApiHelper::GET);\n\n```\n\n## kaiheila/api 作为composer集成至其它服务内\n\n```\ncomposer require  kaiheila/api dev-main\n````\n参数上文或example, 直接使用`session-\u003ereceiveData($data)`来处理数据即可。\n\n**注意：** 在php-fpm中，正常是不支持async-io的，可能会报`async-io must be used in PHP CLI mode`的错误，因此在php-fpm模式下时，需要同步处理事件，示例如下：\n```php\n$session-\u003eon($eventName, function(){},  ['async' =\u003e false]);\n```\n\n\n## 独立机器人\n\n本代码也可以作为一个独立的机器人来运行。\n\n1. git clone git@github.com:kaiheila/php-bot.git\n2. 进入代码目录`cd php-bot`，运行`composer update`\n3. 打开[开发者中心](https://developer.kaiheila.cn/bot), 创建机器人，并更改为webhook/websocket模式。\n4. 更改配置，将开发者中心的配置填入config.php文件中。\n\n```bash\ncp examples/config.php.sample examples/config.php\n# 按照参数说明，修改config.php的配置, vim examples/config.php\n# 运行webhook机器人\nphp examples/webhook.php\n# 运行websocket机器人\n````\n4. 在开发者后台，把机器人的地址填入后台。\n\n在做好上述配置后，你也可以试试我们做的一个小机器人【强尼机器人】, 注意它是webhook模式，需要配置ip及端口。\n```bash\nphp examples/cyberpunk.php\n````\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaiheila%2Fphp-bot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaiheila%2Fphp-bot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaiheila%2Fphp-bot/lists"}