Skip to content

Commit

Permalink
初始化
Browse files Browse the repository at this point in the history
  • Loading branch information
nj committed Nov 1, 2021
0 parents commit 13274e8
Show file tree
Hide file tree
Showing 154 changed files with 11,543 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2012-2021 jt9ge

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[![Telegram Bot API PHP SDK](https://user-images.githubusercontent.com/1915268/75023827-7879f780-54be-11ea-98c1-436a14e7e633.png)][link-repo]


Telegram 机器人 API SDK(Hyperf 2.2.x版)
==========================

> Telegram Bot PHP SDK 让您可以轻松地用 PHP 开发 Telegram Bots!支持 Hyperf 2.2.x 开箱即用.
>
> [Telegram Bot API][link-telegram-bot-api] 是一个基于 HTTP 的接口,专为热衷于为 Telegram 构建机器人的开发人员而创建.
>
> 要了解有关 Telegram Bot API 的更多信息,请参阅Telegram 官方网站上的[机器人简介][link-telegram-bot-api][Bot FAQ](https://core.telegram.org/bots/faq).
>
> 要开始使用此 SDK 编写机器人,请参阅[documentation][link-docs].
> 此项目基于 [前辈telegram-bot-sdk][link-repo] 适配了GuzzleHttp协程版,本身具备handler特性.
## 使用文档说明

Documentation for the SDK can be found on the [前辈telegram-bot-sdk官方][link-docs], API方法请阅读[前辈API][link-repo-doc-api].

## 开源协议

这个项目是在 [MIT][link-license] 许可下发布的.

[link-author]: https://github.com/irazasyed
[link-repo]: https://github.com/irazasyed/telegram-bot-sdk
[link-contributors]: https://github.com/irazasyed/telegram-bot-sdk/contributors
[link-docs]: https://telegram-bot-sdk.readme.io/docs
[link-license]: https://github.com/jt9ge/hyperf-telegram-bot-sdk/blob/main/LICENSE
[link-jetbrains]: https://www.jetbrains.com/?from=telegram-bot-sdk
[link-telegram-bot-api]: https://core.telegram.org/bots
[link-awesome-telegram-bots]: https://github.com/telegram-bot-sdk/awesome-telegram-bots
[link-repo-doc-api]: https://telegram-bot-sdk.readme.io/reference
48 changes: 48 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "jt9ge/hyperf-telegram-bot-sdk",
"description": "Telegram 机器人 API SDK(Hyperf 2.2.x版)",
"keywords": ["telegram", "telegram bot", "telegram bot api", "telegram sdk", "telegram php", "hyperf"],
"type": "library",
"homepage": "https://github.com/jt9ge/hyperf-telegram-bot-sdk",
"license": "MIT",
"authors": [
{
"name": "jt9ge",
"email": "jiutong9ge@gmail.com",
"homepage": "https://github.com/jt9ge"
}
],
"require": {
"php": "^8.0",
"hyperf/guzzle": "~2.2.0",
"illuminate/support": "^5.5 || ^6.0 || ^7.0 || ^8.0",
"league/event": "^2.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
"Telegram\\Bot\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Telegram\\Bot\\Tests\\": "tests/"
}
},
"scripts": {
"test": "phpunit"
},
"extra": {
"branch-alias": {
"dev-master": "2.2-dev"
}
},
"config": {
"preferred-install": "dist",
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
43 changes: 43 additions & 0 deletions src/Actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Telegram\Bot;

/**
* Class Actions.
*
* Chat Actions let you broadcast a type of action depending on what the user is about to receive.
* The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear its typing
* status).
*/
class Actions
{
/** Sets chat status as Typing. */
const TYPING = 'typing';

/** Sets chat status as Sending Photo. */
const UPLOAD_PHOTO = 'upload_photo';

/** Sets chat status as Recording Video. */
const RECORD_VIDEO = 'record_video';

/** Sets chat status as Sending Video. */
const UPLOAD_VIDEO = 'upload_video';

/** Sets chat status as Recording Audio. */
const RECORD_AUDIO = 'record_audio';

/** Sets chat status as Sending Audio. */
const UPLOAD_AUDIO = 'upload_audio';

/** Sets chat status as Sending Document. */
const UPLOAD_DOCUMENT = 'upload_document';

/** Sets chat status as Choosing Geo. */
const FIND_LOCATION = 'find_location';

/** Sets chat status as Recording Video Note. */
const RECORD_VIDEO_NOTE = 'record_video_note';

/** Sets chat status as Sending Video Note. */
const UPLOAD_VIDEO_NOTE = 'upload_video_note';
}
68 changes: 68 additions & 0 deletions src/Answers/AnswerBus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Telegram\Bot\Answers;

use Telegram\Bot\Traits\Telegram;

/**
* Class AnswerBus.
*/
abstract class AnswerBus
{
use Telegram;

/**
* Handle calls to missing methods.
*
* @param string $method
* @param array $parameters
*
* @throws \BadMethodCallException
*
* @return mixed
*/
public function __call($method, $parameters)
{
if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], $parameters);
}

throw new \BadMethodCallException("Method [$method] does not exist.");
}

/**
* Use PHP Reflection and Laravel Container to instantiate the answer with type hinted dependencies.
*
* @param $answerClass
*
* @return object
*/
protected function buildDependencyInjectedAnswer($answerClass)
{
// check if the command has a constructor
if (! method_exists($answerClass, '__construct')) {
return new $answerClass();
}

// get constructor params
$constructorReflector = new \ReflectionMethod($answerClass, '__construct');
$params = $constructorReflector->getParameters();

// if no params are needed proceed with normal instantiation
if (empty($params)) {
return new $answerClass();
}

// otherwise fetch each dependency out of the container
$container = $this->telegram->getContainer();
$dependencies = [];
foreach ($params as $param) {
$dependencies[] = $container->make($param->getClass()->name);
}

// and instantiate the object with dependencies through ReflectionClass
$classReflector = new \ReflectionClass($answerClass);

return $classReflector->newInstanceArgs($dependencies);
}
}
69 changes: 69 additions & 0 deletions src/Answers/Answerable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Telegram\Bot\Answers;

use Illuminate\Support\Str;
use Telegram\Bot\Objects\Update;
use Telegram\Bot\Traits\Telegram;

/**
* Class Answerable.
*
* @method mixed replyWithMessage($use_sendMessage_parameters) Reply Chat with a message. You can use all the sendMessage() parameters except chat_id.
* @method mixed replyWithPhoto($use_sendPhoto_parameters) Reply Chat with a Photo. You can use all the sendPhoto() parameters except chat_id.
* @method mixed replyWithAudio($use_sendAudio_parameters) Reply Chat with an Audio message. You can use all the sendAudio() parameters except chat_id.
* @method mixed replyWithVideo($use_sendVideo_parameters) Reply Chat with a Video. You can use all the sendVideo() parameters except chat_id.
* @method mixed replyWithVoice($use_sendVoice_parameters) Reply Chat with a Voice message. You can use all the sendVoice() parameters except chat_id.
* @method mixed replyWithDocument($use_sendDocument_parameters) Reply Chat with a Document. You can use all the sendDocument() parameters except chat_id.
* @method mixed replyWithSticker($use_sendSticker_parameters) Reply Chat with a Sticker. You can use all the sendSticker() parameters except chat_id.
* @method mixed replyWithLocation($use_sendLocation_parameters) Reply Chat with a Location. You can use all the sendLocation() parameters except chat_id.
* @method mixed replyWithChatAction($use_sendChatAction_parameters) Reply Chat with a Chat Action. You can use all the sendChatAction() parameters except chat_id.
*/
trait Answerable
{
use Telegram;

/**
* @var Update Holds an Update object.
*/
protected $update;

/**
* Magic Method to handle all ReplyWith Methods.
*
* @param $method
* @param $arguments
*
* @return mixed|string
*/
public function __call($method, $arguments)
{
if (! Str::startsWith($method, 'replyWith')) {
throw new \BadMethodCallException("Method [$method] does not exist.");
}
$reply_name = Str::studly(substr($method, 9));
$methodName = 'send'.$reply_name;

if (! method_exists($this->telegram, $methodName)) {
throw new \BadMethodCallException("Method [$method] does not exist.");
}

if (! $this->update->getChat()->has('id')) {
throw new \BadMethodCallException("No chat available for reply with [$method].");
}

$params = array_merge(['chat_id' => $this->update->getChat()->id], $arguments[0]);

return call_user_func([$this->telegram, $methodName], $params);
}

/**
* Returns Update object.
*
* @return Update
*/
public function getUpdate(): Update
{
return $this->update;
}
}
Loading

0 comments on commit 13274e8

Please sign in to comment.