Coder Social home page Coder Social logo

laravel-telegram-log's Introduction

Installation and setup

Installing dependency via composer

composer require gjae/laravel-telegram-log

Once you have installed this package, execute the following command at laravel command prompt:

php artisan vendor:publish --provider="Gjae\TelegramLogChannel\Providers\TelegramChannelProvider"

It will create a file named config/telegram_channel.php. It has the basic config. Now you will need to add a "chat ID" and a "bot api token" in this file.

Quickstarter with telegram bots

Creation of new bots is managed by the BotFather.

Search @BotFather on telegram or click here: botfhater.

Now, create a new bot by typing the command /newbot into the BotFather chat box or choose it by clicking on the square button at the rigth corner of the chat box. Then, select "/newbot". The name of your new bot must have the suffix "Bot", for example:"laravel_log_bot", "LaravelBot". The BotFather will ask you for a name and a username for your new bot (both of them must have the suffix "Bot").

finally, the @BotFather will send you a message like this:

Done! Congratulations on your new bot. You will find it at t.me/[YourUsernameBot]. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this. Use this token to access the HTTP API: [TOKEN] Keep your token secure and store it safely, it can be used by anyone to control your bot. For a description of the Bot API, see this page: https://core.telegram.org/bots/api

GREAT, you have already created your first telegram bot.

Next, copy the token (the one that has the format XXXX:YYYYYYYY) and save it in a safe place.

Usage

Open your ".env file" and look for the following lines and paste the Token and Telegram Id (the ones you had copied before) as values for the following variables:

  1. TELEGRAM_BOT_ACCESS_TOKEN= paste here your SECRET BOT API TOKEN .,
  2. TELEGRAM_CHAT_ID= paste here your chat ID

NOTE 1: Sometimes the ID value may have a "-" mark at the beginning; you should copy this symbol too.

NOTE 2: IF THE ID VALUE HAS ANY SYMBOL AT THE BEGINING, PUT THE ID VALUE INSIDE QUOTATION MARKS. Example: "-121412312"

next open the config/logging.php file, look for the "channels" array and overwrite it like this:

[
    ...
    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['telegram', 'daily'],
        ],

        'telegram'  =>[
            'driver'  => 'monolog',
            'handler' => \Gjae\TelegramLogChannel\TelegramChannel::class,
            'level'   => 'error'
        ],

        ...
    ]
]

How to get the chat_id value

You can make a request to the url: [https://api.telegram.org/bot[YourBotAccessToken]/getUpdates] (https://api.telegram.org/bot[YourBotAccessToken]/getUpdates) (change [YourBotAccessToken] by the "bot token" you have just gotten). Now you'll get a json like this:

{
    "ok": true,
    "result": [
        {
            "update_id": 193532624,
            "message": {
                "message_id": 2,
                "from": {
                    "id": 259222478,
                    "is_bot": false,
                    "first_name": ....,
                    "last_name": ...,
                    "username": ...,
                    "language_code": ...
                },
                "chat": {
                    "id": 259222478,
                    "first_name": ....,
                    "last_name": ...,
                    "username": ...,
                    "type": "private"
                },
                "date": 1589138780,
                "text": "@channelusername",
                "entities": [
                    {
                        "offset": 0,
                        "length": 16,
                        "type": "mention"
                    }
                ]
            }
        },
    ]
}

YOU'RE DONE!

laravel-telegram-log's People

Contributors

gjae avatar yo-hanapr avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

yo-hanapr

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.