Skip to content

Files

Latest commit

5bfdda1 · Jan 12, 2025

History

History
213 lines (185 loc) · 5.44 KB

methods.md

File metadata and controls

213 lines (185 loc) · 5.44 KB

Methods and Listener

Listeners

  • on(array|string $message, callable $action)
  • onText(array|string $message, callable $action)
  • onCommand(array|string $command, callable $action)
  • onAnimation(callable $action, array|string $file_id = null)
  • onAudio(callable $action, array|string $file_id = null)
  • onDocument(callable $action, array|string $file_id = null)
  • onPhoto(callable $action, array|string $file_id = null)
  • onSticker(callable $action, array|string $file_id = null)
  • onVideo(callable $action, array|string $file_id = null)
  • onVideoNote(callable $action, array|string $file_id = null)
  • onVoice(callable $action, array|string $file_id = null)
  • onContact(callable $action)
  • onDice(callable $action, string|null $emoji = null, string|int|null $value = null)
  • onGame(callable $action)
  • onPoll(callable $action)
  • onVenue(callable $action)
  • onLocation(callable $action)
  • onNewChatMembers(callable $action)
  • onLeftChatMember(callable $action)
  • onNewChatTitle(callable $action)
  • onNewChatPhoto(callable $action)
  • onDeleteChatPhoto(callable $action)
  • onGroupChatCreated(callable $action)
  • onSuperGroupChatCreated(callable $action)
  • onMessageAutoDeleteTimerChanged(callable $action)
  • onMigrateToChatId(callable $action)
  • onMigrateFromChatId(callable $action)
  • onPinnedMessage(callable $action)
  • onInvoice(callable $action)
  • onSuccessfulPayment(callable $action)
  • onConnectedWebsite(callable $action)
  • onPassportData(callable $action)
  • onProximityAlertTriggered(callable $action)
  • onForumTopicCreated(callable $action)
  • onForumTopicEdited(callable $action)
  • onForumTopicClosed(callable $action)
  • onForumTopicReopened(callable $action)
  • onVideoChatScheduled(callable $action)
  • onVideoChatStarted(callable $action)
  • onVideoChatEnded(callable $action)
  • onVideoChatParticipantsInvited(callable $action)
  • onWebAppData(callable $action)
  • onMessage(callable $action);
  • onMessageType(string|array $type, callable $action);
  • onEditedMessage(callable $action);
  • onChannelPost(callable $action);
  • onEditedChannelPost(callable $action);
  • onInlineQuery(callable $action);
  • onChosenInlineResult(callable $action);
  • onCallbackQuery(callable $action);
  • onCallbackQueryData(string|array $pattern, callable $action);
  • onShippingQuery(callable $action);
  • onPreCheckoutQuery(callable $action);
  • onPollAnswer(callable $action);
  • onMyChatMember(callable $action);
  • onChatMember(callable $action);
  • onChatJoinRequest(callable $action);
  • onReferral(callable $action);
  • onAny(callable $action);

Methods

  • The methods are exactly the same as the telegram api methods.

⚙️ Usage

  1. make a resource and open it --- Path: App/Resources
  • Create Resource
php laragram make:resource my-resource
  1. Start Coding
  • Simple use
Bot::onText('hello', function(Request $request){
  $request->sendMessage($request->message->chat->id, 'hi');
});
  • Use Variable
Bot::onText('say {text}', function(Request $request, $text){
  $request->sendMessage($request->message->chat->id, $text);
});
Bot::onText('say {text?}', function(Request $request, $text = 'hi'){
  $request->sendMessage($request->message->chat->id, $text);
});
  • Pass Multiple Pattern
Bot::onText(['hello', 'hay'], function(Request $request){
  $request->sendMessage($request->message->chat->id, 'hi');
});
Bot::onText('hello|hay', function(Request $request){
  $request->sendMessage($request->message->chat->id, 'hi');
});

Change Request Method

Constant
Type Name Int
CURL (Default) CURL 32
Non-Response CURL NO_RESPONSE_CURL 64
Bot::onText(['hello', 'hay'], function(Request $request){
  $request->mode(Mode::NO_RESPONSE_CURL)->sendMessage($request->message->chat->id, $count);
});

Controller

# app/Resources/bot.php

Bot::onText('hello', "App/Controller/HelloController@sayHello");
Bot::onText('hello', [App/Controller/HelloController::class ,"sayHello"]);
# app/Controllers/HelloController.php

use LaraGram\Request\Request;

class HelloController 
{
  public function sayHello(Request $request)
  {
    $request->sendMessage(chat()->id, "hello");
  }
}

Group Listeners:

Use one Controller for many listeners:

Bot::controller("App/Controller/GreetingController")->group(function(){
  Bot::onText('hello', "sayHello");
  Bot::onText('hello', "sayBye");
  // ...
});

Conditions:

  • Access Controll:
Bot::can("administrator")->group(function(){
  // ...
})
Bot::canNot("administrator")->group(function(){
  // ...
})
  • Scope Controll:
Bot::scope("private")->group(function(){
  // ...
})
Bot::outOfScope("private")->group(function(){
  // ...
})
  • Is Replied?
Bot::hasReply()->group(function(){
  // ...
})
Bot::hasNotReply()->group(function(){
  // ...
})
  • Multiple Conditions:
Bot::group(["can" => "creator", "scope" => "private"], function(){
  // ...
})
Bot::canNot("administrator")->hasReply()->group(function(){
  // ...
})