Coder Social home page Coder Social logo

forons / telegram-log Goto Github PK

View Code? Open in Web Editor NEW
17.0 5.0 6.0 18 KB

Send a Telegram message when your scripts fire an exception or when they finish their execution.

License: Apache License 2.0

Java 87.94% Python 12.06%
telegram telegram-api logging logger log4j telegram-bot-api appender

telegram-log's Introduction

Telegram-Log โ€“ Send message to a Telegram Bot or use it as a logger!

Telegram-Log is a simple way to interact from multiple programming languages to a Telegram Bot.

It is released under a Apache License 2.0.

Before everything: Create the bot

Following what is written here, the first step to create a bot is to start a new conversation with BotFather. With it, create a new bot with /newbot, then assign it a name and a unique username following the instructions.

The BotFather will answer with a TOKEN, which will be needed to later use the Telegram-Log application.

Then, start a conversation with the new created bot, searching it through the global search.

Now, write /start and start a conversation.

Go to https://api.telegram.org/bot<TOKEN>/getUpdates (replacing <TOKEN> with the token of your bot) and you will receive a JSON that contains something like:

..."chat":{"id":123456789,"first_name":"your_name","username":"your_username",type":"private"}...

You also need the chat id in order to use the Telegram-Log application.

Now you are ready to go to the next step!

Quickstart

Java

To use the application you need to:

git clone https://github.com/forons/telegram-log.git
mvn clean install

Then, you will be able to import this project in any of your projects simply by adding in the pom.xml:

	<dependency>
		<groupId>it.forons.utils</groupId>
		<artifactId>telegram</artifactId>
		<version>1.0-SNAPSHOT</version>
	</dependency>
Usage 1: Send message

As explained in src/main/java/it/forons/utils/telegram/TelegramSender.java there two ways to send messages to a Telegram Bot:

TelegramBot bot = new TelegramBot(TOKEN, CHAT_ID);
int responseCode = bot.sendMessage("Option 1: build the class and send the message.");

or

TelegramBot.sendMessage(TOKEN, CHAT_ID, "Option 2: static method.");
Usage 2: log4j Appender

To use Telegram-Log as a log4j appender it is just needed to add the maven dependency and to set the following lines into the log4j.properties file:

# Root logger option
log4j.rootLogger=INFO, telegram

# Direct log messages to the telegram bot
log4j.appender.telegram=it.forons.utils.telegram.appender.TelegramAppender
log4j.appender.telegram.TOKEN=<TOKEN>
log4j.appender.telegram.CHAT_ID=<CHAT_ID>
log4j.appender.telegram.layout=org.apache.log4j.PatternLayout
log4j.appender.telegram.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Python

Just configure

TELEGRAM_TOKEN = ''
TELEGRAM_CHAT_ID = ''

with your TOKENID and CHATID, then use it as a simple Python logger:

logger = logging.getLogger('trymeApp')
	logger.setLevel(logging.WARNING)

	handler = RequestsHandler()
	formatter = LogstashFormatter()
	handler.setFormatter(formatter)
	logger.addHandler(handler)

	logger.setLevel(logging.WARNING)

	logger.error('We have a problem')

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

Marco De Nadai and Paolo Sottovia for the inspiration


Thank you to everyone who will report bugs, propose extra features, and suggest fixes.

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.