GithubHelp home page GithubHelp logo

carlozamagni / serilog-sinks-telegram Goto Github PK

View Code? Open in Web Editor NEW
21.0 2.0 5.0 10 KB

A Serilog sink that writes events to a Telegram chat/group

C# 100.00%
telegram serilog serilog-sink csharp net logging logger log

serilog-sinks-telegram's Introduction

Serilog.Sinks.Telegram

Writes Serilog events to a given Telegram chat being it a private chat or a group.

Install


Install-Package Serilog.Sinks.Telegram

Note that a Telegram bot api key is required in order to configure the sink, if you don't know how the bot creation process works please refer to the official documentation.

Configuration

To configure the sink simply add "TeleSink" using the "WriteTo" method on the Serilog logger configuration.

new LoggerConfiguration()
   .MinimumLevel.Information()
   .WriteTo.TeleSink(
      telegramApiKey:"my-bot-api-key",
      telegramChatId:"the target chat id")
   .CreateLogger();

Optionally a minimum log level can be specified:

new LoggerConfiguration()
   .MinimumLevel.Information()
   .WriteTo.TeleSink(
      telegramApiKey:"my-bot-api-key",
      telegramChatId:"the target chat id",
      minimumLevel:LogEventLevel.Warning)
   .CreateLogger();

Q.A.

How do i discover the chat id parameter?

Once your bot is created simply open a chat towards it (or include it into a group), then you can use the Telegram API to get the last updates for your bot:


curl -X GET \
  https://api.telegram.org/bot<my-bot-api-key>/getUpdates \
  -H 'Cache-Control: no-cache'

The response should report the last conversations your bot had along with their chat ids:

{
    "ok": true,
    "result": [
        {
            "update_id": 123456789,
            "message": {
                "message_id": 2,
                "from": {
                    "id": 000000,
                    "is_bot": false,
                    "first_name": "XXX",
                    "last_name": "XXX",
                    "username": "XXX",
                    "language_code": "XX"
                },
                "chat": {
                    "id": 0000000,
                    "first_name": "XXX",
                    "last_name": "XXX",
                    "username": "XXX",
                    "type": "private"
                },
                "date": 1531306919,
                "text": "hello dear bot!"
            }
        }
    ]
}

How can i run unit tests?

You need an api key and a chat id in order to run the unit tests. You can add them to the Configuration/TestConfig.json config file or (better solution) you can add another config file named /Configuration/TestConfig_private.json and use it to store you secrets. This is the preferred solution if you want to make a pull request or fork & push the code to another repo since that path is already ignored.

serilog-sinks-telegram's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

serilog-sinks-telegram's Issues

send messages with mark down?

Hi, is is possible to send messages with mark down? i tried it, the bot receives the message but totally ignores the mark down syntax

support for outputTemplate?

Is there a way to use outputTemplate such as -

Log.Logger = new LoggerConfiguration()
                .Enrich.WithCaller()
                .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message} (at {Caller}){NewLine}{Exception}")
                .CreateLogger();

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.