infinite4evr/php-tamtam-bot-api

dev-master 2019-11-29 14:00 UTC

This package is auto-updated.

Last update: 2024-09-29 05:22:08 UTC


README

CodeFactor PHP CURL

一个非常简单的 PHP TamTam Bot API
符合 2019 年 9 月 tamtam Bot API 更新。

一个可工作的示例机器人可供测试,请访问 @ExampleBot,代码在 /exampleBot/exampleBot.php 中可获取。

要求

  • PHP >= 5.3
  • PHP5 必须启用 Curl 扩展。
  • tamtam API 密钥,您可以通过创建机器人后使用简单命令在 @primeBot 获取。

对于 WebHook

  • 据我所知,TamTam 不需要 ssl,因此只需将库放在服务器上,并使用 setWebhook() 命令。

下载

使用 Composer

composer require infinite4evr/php-tamtam-bot-api

使用 Git

从项目目录中,运行

git clone https://github.com/infinite4evr/php-tamtam-bot-api.git

安装

通过 tamtamBotPHP 类

将 tamtam.php 复制到您的服务器,并在您的机器人脚本中包含它

require_once('tamtam.php');

$tamtam = new tamtam('YOUR bot TOKEN HERE');

注意:要启用错误日志文件,请将 TamTamErrorLogger.php 也复制到 TamTam.php 文件的同目录中。

配置(WebHook)

使用 tamtam 类的 setWebhook('url') 方法

示例

$tamtam = new tamtam('YOUR tamtam TOKEN HERE');
$tamtam->setWebhook('https://mywebhook.com/mywebhook.php');
//to delete webhook 
$tamtam->deleteWebhook('https://mywebhook.com/mywebhook.php');
//to check subscriptions 
$tamtam->getSubscriptions());
$tamtam = new tamtam('YOUR tamtam TOKEN HERE');

$chatId = $tamtam->getRecipientChatId();
$content = array('chat_id' => $chatId, 'text' => 'Test');
$tamtam->sendMessage($content);

如果您想从 tamtam 响应中获取一些特定参数

$tamtam = new tamtam('YOUR tamtam TOKEN HERE');

$result = $tamtam->getData();
$text = $result['message']['body']['text'];
$userId = $result['message'] ['sender']['user_id'];
$content = array('user_id' => $userId, 'text' => 'Test');  // it can be any of user_id or chat_id
$tamtam->sendMessage($content);

发送照片

// Load a local file to upload. If is already on tamtam's Servers just pass the resource id
$img = '/path/to/file' ;  // relative path, if you're passing abolsolute path then set $absolutePath parameter = true;
$content = array('chat_id' => $chat_id, 'photo' => $img );
$tamtam->sendPhoto($content);

请参阅 exampleBot.php 或更新 exampleBot.php 以获取完整示例。如果您想查看 exampleBot 的运行情况,请添加它。

如果您想使用 getUpdates 而不是 WebHook,则需要在一个循环中调用 getUpdates() 函数。

$tamtam = new tamtam('YOUR tamtam TOKEN HERE');

$req = $tamtam->getUpdates();

for ($i = 0; $i < $tamtam-> UpdateCount(); $i++) {
	// You NEED to call serveUpdate before accessing the values of message in tamtam Class
    $data = $req[$i];
    $tamtam->setData($data);
	$text = $tamtam->getText();
	$chat_id = $tamtam->getChatID();

	if ($text == '/start') {
		$reply = 'Working';
		$content = array('chat_id' => $chat_id, 'text' => $reply);
		$tamtam->sendMessage($content);
	}
	// DO OTHER STUFF
}

函数

要获取完整和最新的函数文档,请查看 http://infinite4evr.github.io/php-tamtam-bot-api/

构建键盘

tamtam 的机器人只能有一种键盘类型:inline_keyboard。InlineKeyboard 与特定消息相关联

ReplyKeabordExample 使用此代码

    // 4 kinds of buttons possible, please refer to documentation
    $callbackButton = $bot->buildCallbackButton('I am Callback Button', 'callback_data', 'positive');
    $linkButton =  $bot->buildLinkButton('I am link', 'https://infinite4evr.com');
    $contactButton = $bot->buildRequestContactButton('I am requesting Contact');
    $geoLocation = $bot->buildRequestGeoLocationButton('I am geo Request');
    $keyboard = [
                  [$callbackButton],         // each row, array of buttons
                  [$linkButton],
                  [$contactButton,$geoLocation]  // two buttons in same row
    ];
    $inlineKeyboard = $bot->buildInlineKeyboard($keyboard);  // making the final keyboard
    $content = ['user_id' => $user_id,'text' => 'All Inline Buttons', 'attachments' => [$inlineKeyboard]];
    $bot->sendMessage($content);

更多示例即将推出,请查看 /exampleBot/exampleBot.php 以获取示例。

许可

此开源软件根据 MIT 许可证分发。请参阅 LICENSE.md。

贡献

欢迎所有类型的贡献 - 代码、测试、文档、错误报告、新功能等...

  • 发送反馈。
  • 提交错误报告。
  • 编写/编辑文档。
  • 修复错误或添加新功能。

联系我

您可以通过 tamtam 联系我,但如果您有问题,请 打开 一个。