Giter Club home page Giter Club logo

Comments (7)

pooriamo avatar pooriamo commented on June 23, 2024 4

@ranrinc
Hi

1- After setting up the package, create a controller, for example TelegramController.php in you app/Http/Controllers directory and put this in it:

<?php
namespace App\Http\Controllers;

use PhpTelegramBot\Laravel\PhpTelegramBotContract;

class TelegramController extends Controller {
    public function set(PhpTelegramBotContract $telegram_bot) {
        return $telegram_bot->setWebhook(url(config('phptelegrambot.bot.api_key') . '/hook'));
    }

    public function hook(PhpTelegramBotContract $telegram_bot) {
        $telegram_bot->handle();
    }
}

2- Now you need to write routes for these actions. so open your web.php and write:

$router->group(['prefix' => '[YOUR BOT API KEY]', function() use ($router) {
    $router->get('set', 'TelegramController@set');
    $router->post('hook', 'TelegramController@hook');
});

replace [YOUR BOT API KEY] with the API key that BotFather has given you. This is to make secure the hook url to ensure the incoming requests are from no one but Telegram.

3- Go to https://yoursite.com/[YOUR BOT API KEY]/set
You should see the success message.

4- Create a directory for your commands. It can be anywhere, for example app/Telegram/Commands/[CommandNameCommand].php would be nice.

This is an example StartCommand.php:

<?php
namespace Longman\TelegramBot\Commands\SystemCommands;

use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Request;

class StartCommand extends SystemCommand {
	protected $name = 'start';
	protected $usage = '/start';

	public function execute()
	{
		$message = $this->getMessage();

        	$chat_id = $message->getChat()->getId();
        	$text    = 'Hi! Welcome to my bot!';

        	$data = [
            		'chat_id' => $chat_id,
            		'text'    => $text,
        	];

        	return Request::sendMessage($data);
	}
}

5- The last thing you need to do is to add your custom commands directory to the bot's config. so open config/phptelegrambot.php and add the commands directory to the array:

...
'commands' => [
        'before'  => true,
        'paths'   => [
            base_path('/app/Telegram/Commands')
        ],
        'configs' => [
            // Custom commands configs
        ],
],
...

That's it all.

from laravel.

ranrinc avatar ranrinc commented on June 23, 2024

@pooriamo yeah... thank you so much.. finally someone explain.

from laravel.

akalongman avatar akalongman commented on June 23, 2024

@pooriamo thank you for the given example. Could you please send PR and add some examples in EXAMPLES.md or somewhere?

from laravel.

akalongman avatar akalongman commented on June 23, 2024

@pooriamo

from laravel.

BigXia avatar BigXia commented on June 23, 2024

@ranrinc
Hi

1- After setting up the package, create a controller, for example TelegramController.php in you app/Http/Controllers directory and put this in it:

<?php
namespace App\Http\Controllers;

use PhpTelegramBot\Laravel\PhpTelegramBotContract;

class TelegramController extends Controller {
    public function set(PhpTelegramBotContract $telegram_bot) {
        return $telegram_bot->setWebhook(url(config('phptelegrambot.bot.api_key') . '/hook'));
    }

    public function hook(PhpTelegramBotContract $telegram_bot) {
        $telegram_bot->handle();
    }
}

2- Now you need to write routes for these actions. so open your web.php and write:

$router->group(['prefix' => '[YOUR BOT API KEY]', function() use ($router) {
    $router->get('set', 'TelegramController@set');
    $router->post('hook', 'TelegramController@hook');
});

replace [YOUR BOT API KEY] with the API key that BotFather has given you. This is to make secure the hook url to ensure the incoming requests are from no one but Telegram.

3- Go to https://yoursite.com/[YOUR BOT API KEY]/set
You should see the success message.

4- Create a directory for your commands. It can be anywhere, for example app/Telegram/Commands/[CommandNameCommand].php would be nice.

This is an example StartCommand.php:

<?php
namespace Longman\TelegramBot\Commands\SystemCommands;

use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Request;

class StartCommand extends SystemCommand {
	protected $name = 'start';
	protected $usage = '/start';

	public function execute()
	{
		$message = $this->getMessage();

        	$chat_id = $message->getChat()->getId();
        	$text    = 'Hi! Welcome to my bot!';

        	$data = [
            		'chat_id' => $chat_id,
            		'text'    => $text,
        	];

        	return Request::sendMessage($data);
	}
}

5- The last thing you need to do is to add your custom commands directory to the bot's config. so open config/phptelegrambot.php and add the commands directory to the array:

...
'commands' => [
        'before'  => true,
        'paths'   => [
            base_path('/app/Telegram/Commands')
        ],
        'configs' => [
            // Custom commands configs
        ],
],
...

That's it all.

There is a fault, It should be add "/":
base_path('/app/Telegram/Commands/')
or will do not work.

from laravel.

pooriamo avatar pooriamo commented on June 23, 2024

@akalongman I'm sorry I just saw your message. I just created a PR.

from laravel.

noplanman avatar noplanman commented on June 23, 2024

I'll close off here, as there is a wonderful example available now!

from laravel.

Related Issues (20)

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.