aliw1382/telegram-tools 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

aliw1382/telegram-tools

Composer 安装命令:

composer require aliw1382/telegram-tools

包简介

Package for tools building your telegram bot

README 文档

README

Laravel Telegram Tools

داکیومنت فارسی

About Package

This package is in its test version, it may have problems!

This package is made so that people who build Telegram bots can easily and quickly code their bots.

This package was developed and designed by Ali Shahmohammadi.

Requirement

  • guzzlehttp/guzzle ^7.*
  • illuminate/support ~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0
  • php >= 7.4

Installation ✨

To install and receive the package in your Laravel project, execute the following command in the terminal of your project path.

$ composer require aliw1382/telegram-tools

Add Providers

Open the file config/app.php and add the following values to it.

Providers:

'providers' => [

    // ......
    
    Aliw1382\TelegramTools\Providers\TelegramToolsServiceProvider::class,
    
    // ......

]

Aliases:

'aliases' => [

    // ......
    
    'Telegram' => Aliw1382\TelegramTools\Facades\Telegram::class,
    
    // ......

]

Publish Config

$ php artisan vendor:publish --tag=config-telegram-tools

Then the file config/telegram.php is created.

'telegram-bot-api' => [

    'token' => env( 'TELEGRAM_API_TOKEN' )
    
]

.Env File

Add this .env file and put the token received from Bot Father here.

TELEGRAM_API_TOKEN=

Usage

  • The first method
telegram()->sendMessage( [

    'chat_id' => 'YOUR CHAT ID',
    'text'    => 'YOUR CONTENT TEXT'
    // ....

] );

telegram( 'Your Other Token Bot' )->sendMessage( [

    'chat_id' => 'YOUR CHAT ID',
    'text'    => 'YOUR CONTENT TEXT'
    // ....

] );

If you use telegram() we use your .env token bot.

  • The second method
telegram()->sendMessage()->to( 'YOUR CHAT ID' )->content( 'YOUR CONTENT TEXT' )->send();

telegram( 'Your Other Token Bot' )->sendMessage()->to( 'YOUR CHAT ID' )->content( 'YOUR CONTENT TEXT' )->send();

For Example For SendMessage With Keyboard And Parse Mode:

// Keyboard
telegram()->sendMessage()->to( 'YOUR CHAT ID' )->content( 'YOUR CONTENT TEXT' )->parseMode( 'html' )->button( 'Button 1' )->button( 'Button 2' )->send();

// Inline Keyboard
telegram()->sendMessage()->to( 'YOUR CHAT ID' )->content( 'YOUR CONTENT TEXT' )->parseMode( 'MarkdownV2' )->button( 'Google' , 'https://google.com' )->buttonWithCallback( 'Button 1' , 'Your Callback Data' )->send();
  • The third method
\Telegram::sendMessage( 'YOUR CHAT ID', 'YOUR CONTENT TEXT' );

or

use Aliw1382\TelegramTools\Facades\Telegram;

Telegram::sendMessage( 'YOUR CHAT ID', 'YOUR CONTENT TEXT' );

For Example For SendMessage With Keyboard And Parse Mode:

use Aliw1382\TelegramTools\Facades\Telegram;

Telegram::sendMessage( 'YOUR CHAT ID', 'YOUR CONTENT TEXT', Telegram::buildKeyBoard( [
    [
        Telegram::buildKeyboardButton( 'Button 1' ),
        Telegram::buildKeyboardButton( 'Button 2' , true ), // for request contact
    ]
] ) , 'html' );

Telegram::sendMessage( 'YOUR CHAT ID', 'YOUR CONTENT TEXT', Telegram::buildInlineKeyBoard( [
    [
        Telegram::buildInlineKeyboardButton( 'Google' ,'https://google.com'),
        Telegram::buildInlineKeyboardButton( 'Button 1' , '','Your CALLBACK DATA' ),
        // or
        Telegram::buildInlineKeyboardButton( text: 'Button 2' ,callback_data: 'Your CALLBACK DATA' ),
    ]
] ) , 'html' );

✨ I hope you enjoy this package ✨

Source Code Programming

This feature is activated in version 2.0.0 !

Okay, now it's time to handle the /start command in the robot.

First you need to create a class to code your bot. You can use the following command to create a class for the messages that are sent to the bot.

$ php artisan telegram:command StartMessage --type=Message

After you create your command, you need to introduce it to the program, so for this, enter the file config/telegram.php and add the address of the created class to it.

'commands' => [
    
    App\Telegram\StartMessage::class,
    // Commands Class

]

This class instance is made to handle the bot /start message and respond to it.

<?php

namespace App\Telegram;

use Aliw1382\TelegramTools\Attribute\TelegramAttribute;
use Aliw1382\TelegramTools\Contracts\Abstract\AbstractTelegramMessage;
use Aliw1382\TelegramTools\Vendor\TelegramUpdate;


class StartMessage extends AbstractTelegramMessage
{

    #[TelegramAttribute( '/start' )]
    public function myMethod( TelegramUpdate $update )
    {

        telegram()->sendMessage( [

            'chat_id' => $update->ChatID(),
            'text'    => 'Hello Welcome To Bot!'

        ] );

    }

}

For example, if you want to handle an unknown word, you can use * instead.

for example: We want to handle the word /help, but the user might send it /hel

<?php

namespace App\Telegram;

use Aliw1382\TelegramTools\Attribute\TelegramAttribute;
use Aliw1382\TelegramTools\Contracts\Abstract\AbstractTelegramMessage;
use Aliw1382\TelegramTools\Vendor\TelegramUpdate;


class StartMessage extends AbstractTelegramMessage
{

    #[TelegramAttribute( '/hel*' )]
    public function myMethod( TelegramUpdate $update )
    {

        telegram()->sendMessage( [

            'chat_id' => $update->ChatID(),
            'text'    => 'Help Message ...'

        ] );

    }

}

An example for Callback Query

$ php artisan telegram:command CallBackQueryHandler --type=CallbackQuery

add new class to config/telegram.php

<?php

namespace App\Telegram;

use Aliw1382\TelegramTools\Attribute\TelegramAttribute;
use Aliw1382\TelegramTools\Contracts\Abstract\AbstractTelegramCallbackQuery;
use Aliw1382\TelegramTools\Vendor\TelegramUpdate;


class CallBackQueryHandler extends AbstractTelegramCallbackQuery
{

    #[TelegramAttribute( 'mention-*' )]
    public function exampleMethod( TelegramUpdate $update )
    {

        telegram()->editMessageText( [
            
            'chat_id'    => $update->ChatID(),
            'message_id' => $update->MessageID(),
            'text'       => 'You Mention The User <a href="tg://user=' . $update->CallbackDataArray()[ 1 ] . '">' . $update->CallbackDataArray()[ 1 ] . '</a>'
            
        ] );

    }

}

if we send data mention-123456 so we mentioned the user 123456

History

Please see History for more information on what has been changed recently.

Security

If you discover any security related issues, please email aliw1382@gmail.com instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

aliw1382/telegram-tools 适用场景与选型建议

aliw1382/telegram-tools 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 08 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 aliw1382/telegram-tools 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-31