PageSourceSearch

https://nutgram.dev/assets/js/93765f2b.32f1383e.js

js nutgram.dev collected 2026-10-04 17:40:10 UTC 8,652 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunknutgram_docs=self.webpackChunknutgram_docs||[]).push([[3307],{85533:function(e,n,t){t.r(n),t.d(n,{assets:function(){return s},contentTitle:function(){return i},default:function(){return m},frontMatter:function(){return o},metadata:function(){return l},toc:function(){return u}});var a=t(83117),r=(t(67294),t(3905));t(56022);const o={sidebar_position:3},i="Laravel Integration",l={unversionedId:"configuration/laravel",id:"version-2.x/configuration/laravel",title:"Laravel Integration",description:"If you are using the Laravel framework, much of the setup is handled automatically for you. First, you should install",source:"@site/versioned_docs/version-2.x/configuration/laravel.md",sourceDirName:"configuration",slug:"/configuration/laravel",permalink:"/docs/2.x/configuration/laravel",draft:!1,editUrl:"https://github.com/nutgram/docs/tree/master/versioned_docs/version-2.x/configuration/laravel.md",tags:[],version:"2.x",sidebarPosition:3,frontMatter:{sidebar_position:3},sidebar:"docs",previous:{title:"Cache",permalink:"/docs/2.x/configuration/cache"},next:{title:"Usage",permalink:"/docs/2.x/category/usage"}},s={},u=[{value:"Configuration",id:"configuration",level:2},{value:"Commands",id:"commands",level:2},{value:"Handlers definition",id:"handlers-definition",level:2},{value:"Webhook updates",id:"webhook-updates",level:2},{value:"Testing",id:"testing",level:2}],p={toc:u};
1function m(e){let{components:n,...t}=e;return(0,r.kt)("wrapper",(0,a.Z)({},p,t,{components:n,mdxType:"MDXLayout"}),(0,r.kt)("h1",{id:"laravel-integration"},"Laravel Integration"),(0,r.kt)("p",null,"If you are using the Laravel framework, much of the setup is handled automatically for you. First, you should install\nthe package via composer as usual (see ",(0,r.kt)("a",{parentName:"p",href:"/docs/2.x/configuration/installation#composer"},"the installation page"),")"),(0,r.kt)("p",null,"In you ",(0,r.kt)("inlineCode",{parentName:"p"},".env")," file, you should only define the ",(0,r.kt)("inlineCode",{parentName:"p"},"TELEGRAM_TOKEN")," var, that's it!"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-bash"},'TELEGRAM_TOKEN="api-telegram-token"\n')),(0,r.kt)("p",null,"The framework instance, is available anywhere via the DI container, for example:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-php"},"<?php\n\nnamespace App\\Http\\Controllers;\n\nuse App\\Http\\Controllers\\Controller;\nuse SergiX44\\Nutgram\\Nutgram;\n\nclass TelegramController extends Controller\n{\n    /**\n     * Handle the request.\n     */\n    public function handle(Nutgram $bot)\n    {\n        //\n    }\n}\n")),(0,r.kt)("h2",{id:"configuration"},"Configuration"),(0,r.kt)("p",null,"To expose the undelying configuration, you need to publish the configuration file:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-bash"},'php artisan vendor:publish --provider="SergiX44\\Nutgram\\NutgramServiceProvider" --tag="nutgram"\n')),(0,r.kt)("p",null,"In the ",(0,r.kt)("inlineCode",{parentName:"p"},"config/nutgram.php")," file, you will find something like that:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-php"},"    // The Telegram BOT api token\n    'token' => env('TELEGRAM_TOKEN'),\n\n    // if the webhook mode must validate the incoming IP range is from a telegram server\n    'safe_mode' => env('APP_ENV', 'local') === 'production',\n\n    // Extra or specific configurations\n    'config' => [],\n\n    // Set if the service provider should automatically load\n    // handlers from /routes/telegram.php\n    'routes' => true,\n")),(0,r.kt)("p",null,"The second ",(0,r.kt)("inlineCode",{parentName:"p"},"config")," array, is basically any configuration option, already\nexplained ",(0,r.kt)("a",{parentName:"p",href:"/docs/2.x/configuration/installation#configuration"},"here"),"."),(0,r.kt)("p",null,"The third ",(0,r.kt)("inlineCode",{parentName:"p"},"routes"),", set if the service provider should load the handlers form the folder ",(0,r.kt)("inlineCode",{parentName:"p"},"routes/telegram.php"),", by\ndefault is ",(0,r.kt)("inlineCode",{parentName:"p"},"true"),"."),(0,r.kt)("h2",{id:"commands"},"Commands"),(0,r.kt)("p",null,"The framework automatically register some useful commands in your Laravel application:"),(0,r.kt)("ul",null,(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("inlineCode",{parentName:"li"},"nutgram:list"),(0,r.kt)("ul",{parentName:"li"},(0,r.kt)("li",{parentName:"ul"},"List all registered handlers"))),(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("inlineCode",{parentName:"li"},"nutgram:hook:info"),(0,r.kt)("ul",{parentName:"li"},(0,r.kt)("li",{parentName:"ul"},"Get current webhook status"))),(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("inlineCode",{parentName:"li"},"nutgram:hook:remove {--d|drop-pending-updates}"),(0,r.kt)("ul",{parentName:"li"},(0,r.kt)("li",{parentName:"ul"},"Remove the bot webhook"))),(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("inlineCode",{parentName:"li"},"nutgram:hook:set {url}"),(0,r.kt)("ul",{parentName:"li"},(0,r.kt)("li",{parentName:"ul"},"Set the bot webhook"))),(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("inlineCode",{parentName:"li"},"nutgram:register-commands"),(0,r.kt)("ul",{parentName:"li"},(0,r.kt)("li",{parentName:"ul"},"Register the bot commands, see ",(0,r.kt)("a",{parentName:"li",href:"/docs/2.x/usage/handlers#automatically-register-bot-commands"},"automatically-register-bot-commands")))),(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("inlineCode",{parentName:"li"},"nutgram:run"),(0,r.kt)("ul",{parentName:"li"},(0,r.kt)("li",{parentName:"ul"},"Start the bot in long polling mode. Useful in development mode.")))),(0,r.kt)("h2",{id:"handlers-definition"},"Handlers definition"),(0,r.kt)("p",null,"The ",(0,r.kt)("inlineCode",{parentName:"p"},"routes/telegram.php")," should be something like this:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-php"},"<?php\n/** @var SergiX44\\Nutgram\\Nutgram $bot */\n\nuse SergiX44\\Nutgram\\Nutgram;\n\n/*\n|--------------------------------------------------------------------------\n| Nutgram Handlers\n|--------------------------------------------------------------------------\n|\n| Here is where you can register telegram handlers for Nutgram. These\n| handlers are loaded by the NutgramServiceProvider. Enjoy!\n|\n*/\n\n$bot->onCommand('start', function (Nutgram $bot) {\n    return $bot->sendMessage('Hello, world!');\n})->description('The start command!');\n")),(0,r.kt)("p",null,"This file is automatically loaded by the framework, so here is where you should define middleware, handlers and\nconversations."),(0,r.kt)("h2",{id:"webhook-updates"},"Webhook updates"),(0,r.kt)("p",null,"For production mode, the webhook mode is recommended. Run the bot in that way is really simple, you should just create a\nnew controller ",(0,r.kt)("inlineCode",{parentName:"p"},"php artisan make:controller FrontController"),", and call the ",(0,r.kt)("inlineCode",{parentName:"p"},"run")," method on the bot instance:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-php"},"class FrontController extends Controller\n{\n    /**\n     * Handle the telegram webhook request.\n     */\n    public function __invoke(Nutgram $bot)\n    {\n        $bot->run();\n    }\n}\n")),(0,r.kt)("admonition",{type:"tip"},(0,r.kt)("p",{parentName:"admonition"},"When calling the ",(0,r.kt)("inlineCode",{parentName:"p"},"run()")," method on the bot instance, it automatically recognize if use the ",(0,r.kt)("inlineCode",{parentName:"p"},"Polling")," method to retrieve updates,\nor ",(0,r.kt)("inlineCode",{parentName:"p"},"Webhook"),", based on whether the current instance is running in a cli process, or is serving a web request.")),(0,r.kt)("p",null,"and remember to register it on you http routes:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-php"}
1,"// routes/api.php\n\nRoute::post('/webhook', 'FrontController');\n")),(0,r.kt)("h2",{id:"testing"},"Testing"),(0,r.kt)("p",null,"Inside unit tests, you can automatically retrieve the ",(0,r.kt)("inlineCode",{parentName:"p"},"fake")," instance with all your handlers and middleware loaded,\nsimply by resolving it via DI:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-php"},"namespace Tests\\Feature;\n\nuse SergiX44\\Nutgram\\Nutgram;\nuse Tests\\TestCase;\n\nclass ExampleTest extends TestCase\n{\n    /**\n     * @return void\n     */\n    public function test_bot()\n    {\n        $bot = app(Nutgram::class);\n        \n        // ...\n    }\n}\n")))}m.isMDXComponent=!0}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.