Code Monkey home page Code Monkey logo

serogaq / laravel-tgbotapi Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 448 KB

This package provides methods for working with the telegram bot api, and helpers for receiving updates via webhooks or the long polling method

License: BSD 3-Clause "New" or "Revised" License

PHP 100.00%
composer-package laravel-package laravel-telegram laravel-telegram-bot-sdk php laravel laravel-8-package laravel-9-package telegram-bot-api telegram-bot-framework

laravel-tgbotapi's Introduction

Laravel TgBotApi

Latest Stable Version License

This package provides methods for working with the telegram bot api, and helpers for receiving updates via webhooks or the polling method
Requires PHP >= 8.0.2 and Laravel >= 8

Installation

Require the serogaq/laravel-tgbotapi package in your composer.json and update your dependencies:

composer require serogaq/laravel-tgbotapi

Then run the command:

php artisan tgbotapi:install

Usage

Set up your bot in config/tgbotapi.php

Add a listener for the new update event:

// app/Providers/EventServiceProvider.php
use App\Listeners\HandleNewUpdate;
use Serogaq\TgBotApi\Events\NewUpdateEvent;
class EventServiceProvider extends ServiceProvider {
	protected $listen = [
		NewUpdateEvent::class => [
			HandleNewUpdate::class,
		],
	];
}

To receive updates via webhooks, use the command:

php artisan tgbotapi:setwebhook

To receive updates via a long polling, create a background task:

// app/Console/Kernel.php
class Kernel extends ConsoleKernel {
	protected function schedule(Schedule $schedule) {
		$schedule->command('tgbotapi:getupdates', ['bot_username', '--until-complete'])->everyMinute()->withoutOverlapping()->runInBackground();
	}
}

Events will be sent to app/Listeners/HandleNewUpdate.php where you can process them.

Package provides the logic for handling updates in controllers:

// app/Listeners/HandleNewUpdate.php
use Serogaq\TgBotApi\Facades\BotManager;
use Serogaq\TgBotApi\Events\NewUpdateEvent;
use Serogaq\TgBotApi\Traits\ProcessingInControllers;
class HandleNewUpdate {
	use ProcessingInControllers;
}

Next, create controller for the update:

$ php artisan make:tgbotapi:controller CommandUpdate

All updates with type CommandUpdate, i.e. commands, will be processed in the controller:

// app/TgBotApi/Updates/CommandUpdate.php
use Serogaq\TgBotApi\Events\NewUpdateEvent;
use Serogaq\TgBotApi\Facades\BotManager;
use Serogaq\TgBotApi\Interfaces\UpdateController;
class CommandUpdate implements UpdateController {
	public function __construct(protected NewUpdateEvent $event) {}
	public function handle(): void {
		BotManager::bot('username_bot')?->sendMessage([
			'text' => $this->event->update['message']['text'],
			'chat_id' => $this->event->update['message']['chat']['id']
		])->send();
	}
}

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Troubleshooting

If you like living on the edge, please report any bugs you find on the issues page.

License

The BSD-3-Clause. Please see License File for more information.

laravel-tgbotapi's People

Contributors

actions-user avatar dependabot[bot] avatar serogaq avatar

Stargazers

 avatar

Watchers

 avatar

laravel-tgbotapi's Issues

Fix commandMatch function

Commands with @ do not return true in this function. When the bot has anonymous mode, the command from the telegram list will be sent with @

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.