承接 shamanhead/telbot 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

shamanhead/telbot

Composer 安装命令:

composer require shamanhead/telbot

包简介

A simple library to create message bot in telegram

README 文档

README

Contents

Introducion

If you want to create your bot, for the first you need to register him.You can do it with @BotFather. Open the dialog with him and write /newbot , like as in this image:

Снимок

Снимок6

After that you need to set webhook on your bot.Webhook its a system, who sending queries to your server, if telegram gets one.

Before start creating a webhook you need bot api token and server.If you dont have a server, you can use heroku to create one.

You can find your bot api token by writing /mybots, then select your bot, and then select button "API Token".Example:

Снимок2

Снимок 3PNG

Снимок3

When you finish all this actions, you can set webhook to your bot.For this you need to use bot api method setWebhook:

Снимок8

Then, if you done all right, you will see this answer:

Снимок4

After that you can start working with your bot.But how I can work?You can ask me.Lets see, how.

Creating you bot

How to create bot?Very easy!Just create a new bot class:

	use \Telbot\Bot as Bot;

	$bot = new Bot('BOT_API_KEY HERE');

So, lets create a script, who will send a text message as test.But how?The Inquiry class will help us with it:

	use \Telbot\Bot as Bot;
	use \Telbot\Inquiry as Inquiry;
	use \Telbot\InputHandle as InputHandle;

	$bot = new Bot('API_TOKEN');
	$InputHandle = new InputHandle();

	Inquiry::send($bot
			,'sendMessage',
		[
			'chat_id' => $InputHandle->getChatId(),
			'text' => 'Testing your bot.'
		]
	);

Снимок

Utils

Supporting class for ease of work with telegram bot api types.

Creating keyboard

You can create keyboards easy using this way:

	use \Telbot\Utils\ as Utils;
	use \Telbot\Bot as Bot;

	Utils::buildInlineKeyboard([[['one', 'text']], [['two', 'text']], [['three', 'test']]])

	Utils::buildKeyboard([[['third'], ['second'], ['first']]])

Examples:

	use \Telbot\Utils as Utils;
	use \Telbot\Bot as Bot;
	use \Telbot\InputHandle as InputHandle;
	use \Telbot\Inquiry as Inquiry;

	$bot = new Bot('API_TOKEN');
	$InputHandle = new InputHandle();

	Inquiry::send($bot, 'sendMessage', [
		'chat_id' => $InputHandle->getChatId(),
		'text' => 'Simple text.',
		'reply_markup' => Utils::buildInlineKeyboard([[['one', 'callback']], [['two', 'callback']], [['three', 'callback']]])
	]);

Снимок3

Encoding files

If you want to send video or photo to user, you need to encode them to CURl format.For this use this method:

	Utils::encodeFile($filePath) //return encoded CURlfile object.

Parameter $filePath need to indicate path to file you want to send.

Building inline query result

If you want to send an answer to inline query, you need to build answer object.For this use this method:

	Utils::buildInlineQueryResult($resultType ,$data) //returns json encoded array of $data with type of result $resultType

You can check example here

Mysql features

To start work with mysql, first you need to do is enable sql connection in your bot object:

	$bot->enableSql();

You can also disable sql by using similar method:

	$bot->disableSql();

Warning: if your sql connection doesnt exist, you can not use this modules:User, Chat. In this case Context instead of writing context values ​​to the database would be create new file with context(one to user).

Later you need to specify sql credentials:

	$bot->sqlCredentials(
		[
			'database_server' => '',
			'database_name' => '',
			'username' => '',
			'password' => ''
		]
		);

Or you can indicate your external pdo connection as sql credentials:

	$bot->externalPDO($PDO_CONNECTION);

After all this actions, you can start to work with database.

Context

Using class Context you can create context dependence:

	use \Telbot\Context as Context; //We include new class Context
	use \Telbot\Bot as Bot;
	use \Telbot\Inquiry as Inquiry;
	use \Telbot\InputHandle as InputHandle;
	
	$InputHandle = new InputHandle();
	$bot = new Bot('API_TOKEN');
	$DBH = new PDO();
	$bot->externalPDO($DBH);
	$bot->enableSql();

	if(!Context::read($bot, $InputHandle->getChatId(), $InputHandle->getUserId())){ //reading context
		Inquiry::send($bot
				,'sendMessage',
			[
				'chat_id' => $InputHandle->getChatId(),
				'text' => 'Write smth'
			]
		);
		Context::write($bot, $InputHandle->getChatId(), $InputHandle->getUserId(), 'smth'); //creating new context
	}else{
		Inquiry::send($bot
			,'sendMessage',
			[
			'chat_id' => $InputHandle->getChatId(),
			'text' => 'Okay, you writed!'
			]
		);
		Context::delete($bot, $InputHandle->getChatId(), $InputHandle->getUserId()); //delete context
	}

Снимок2

Working with chats in database

All the same, but a bit different:

To add chats to database, you need to use this function:

	Chat::add($bot, $chatId);

To delete chats:

	Chat::delete($bot, $chatId);

To get chats:

	Chat::get($bot, $chatId);

This function returns array with row information of this chat(row id, chat id, bot token)

To get all chats:

	Chat::getAll($bot);

Inquiry

Its the main class in this library.This paragraph shows all capabilities of this class.

This class has only one method - Inquiry::send().With this class you can send both simple text messages and complex answers.

	Inquiry::send($bot, $method, $data);

Supported methods

All method supported during version of API 4.7

Sending simple answer

	Inquiry::send($bot, 'sendMessage', [
		'chat_id' => $InputHandle->getChatId(),
		'text' => 'This is a testing message'
	
	]);

Sending callback query answer

	Inquiry::send($bot, 'answerCallbackQuery', [
		'callback_query_id' => $InputHandle->getCallbackQueryId(),
		'text' => 'This is a callback answer, who lool like common notification'
	]);

Sending Inline query answer

	Inquiry::answerInlineQuery($bot, [
		'inline_query_id' => $InputHandle->getInlineQueryId(),
		'results' => Utils::buildInlineQueryResult('article', [
		'title' => 'test',
		'input_message_content' => [
			'message_text' => 'Yes, its just test'
		]])
	]);

You can send any of telegram methods with this method send.All of this supported.

Sending files

	//sending photo
	use \Telbot\Bot as Bot;
	use \Telbot\InputHandle as InputHandle;
	use \Telbot\Inquiry as Inquiry;

	$bot = new Bot('927942575:AAHZpZoG2pBRw25Lw-pPaw8FU15t00Lsf3A');
	$InputHandle = new InputHandle();

	Inquiry::send($bot ,'sendPhoto', [
		'chat_id' => $InputHandle->getChatId(),
		'photo' => 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Telegram_2019_Logo.svg/1200px-Telegram_2019_Logo.svg.png',
		'caption' => 'This is an image!So beautiful'
	]);

To send files from your server, you need to encode file to CURl format.For this, you need to use method Utils::encodeFile.

	use \Telbot\Utils as Utils;
	use \Telbot\Bot as Bot;
	use \Telbot\InputHandle as InputHandle;
	use \Telbot\Inquiry as Inquiry;

	$bot = new Bot('927942575:AAHZpZoG2pBRw25Lw-pPaw8FU15t00Lsf3A');
	$InputHandle = new InputHandle();

	Inquiry::send($bot ,'sendPhoto', [
		'chat_id' => $InputHandle->getChatId(),
		'photo' => Utils::encodeFile('app/something/telegram.png'),
		'caption' => 'This is an image!So beautiful'
	]);

Input Handle

This class needs for comfortable work with telegram answer query.

Creating a new InputHandle object

	$InputHanle = new InputHandle();

Working with data

	$InputHandle->getUpdateId() // returns an update id of telegram answer query.

	$InputHandle->getQueryType() // returns a query type of telegram answer query(callback_query,inline_query,message).

	$InputHandle->getInstance() // returns an array of telegram answer.

	$InputHandle->getCallbackData() //  returns a callback data from telegram answer query.

	$InputHandle->getCallBackQueryId() // returns a callback query id from telegram answer query.

	$InputHandle->getUserId() // returns user id.

	$InputHandle->userIsBot() // return true if user who send quiry is bot.

	$InputHandle->getUserName() // returns name of user, who sends query.

	$InputHandle->getMessageId() // returns user's message id.

	$InputHandle->getUserFirstName() // returns user's first name.

	$InputHandle->getLanguageCode() // returns user's language code.

	$InputHandle->getChatType() // returns chat type.

	$InputHandle->getChat() // returns chat array from telegram answer query.

	$InputHandle->newChatMember() // returns true when new member comes to telegram chat.

	$InputHandle->getChatId() // returns a chat id, where the message come.

	$InputHandle->getDate() // returns date when telegram answer query was send.

	$InputHandle->getEntities() // returns message entities from telegram answer query.

	$InputHandle->getChatTitle() // returns title of chat where bot gets query.

	$InputHandle->getInlineQueryText() // returns query data from inline query.

	$InputHandle->getInlineQueryOffset() // returns query offset from inline query.

	$InputHandle->getInlineQueryId() // returns an inline query in from telegram answer query.

Privelege

You can give a user a specific privilege in the chat(or all chats). This can be used to give access to certain commands to that particular user.

	Privilege::setToChat($bot, $value, $userId, $chatId); //for one chat
	Privilege::setToAllChats($bot, $value, $userId); //for all chats
	Privilege::get($bot, $userId); //getting a privelege

Examples

See examples at 'examples' folder.

License

Please see the LICENSE included in this repository for a full copy of the MIT license, which this project is licensed under.

shamanhead/telbot 适用场景与选型建议

shamanhead/telbot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 293 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 06 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「package」 「php」 「bot」 「telegram」 「TelegramAPI」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 shamanhead/telbot 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 shamanhead/telbot 我们能提供哪些服务?
定制开发 / 二次开发

基于 shamanhead/telbot 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 293
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-06-11