Coder Social home page Coder Social logo

telegraf-docs's Introduction

Telegraf docs

This repo will house the future of Telegraf's documentation. For now, it houses bot examples.

How to use these examples:

In the interest of brevity, the examples in the repo don't explain certain good-to-haves.

Graceful stop

On NodeJS, to make sure connections are gracefully closed before process is killed, you can use the process.once hooks. This is safe to add to the end of your bot's entry file.

process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));

Secrets

This section talks about how to pass variables like token, port, webhook domain to the bot.

The recommendation is to pass them via environment variables and not directly include them in code. This is especially true for sensitive secrets such as token. This should probably be added before calling new Telegraf().

const {
	PORT,
	BOT_TOKEN: token,
	WEBHOOK_DOMAIN: webhookDomain,
} = process.env;

// default to port 3000 if PORT is not set
const port = Number(PORT) || 3000;

// assert and refuse to start bot if token or webhookDomain is not passed
if (!token) throw new Error('"WEBHOOK_URL" env var is required!');
if (!webhookDomain) throw new Error('"BOT_TOKEN" env var is required!');

Important note: Only use the vars needed for your bot. If you don't use webhooks, you don't need WEBHOOK_DOMAIN or the corresponding assertion.

Different deployment methods may have their own ways to pass environment variables. If you deploy to a Function-as-a-Service host such as Heroku, Netlify, AWS Lambda, GCP Functions, use the platform-specific way to set the correct environment variables.

If you use a conventional server, you can use the npm package "dotenv" to load vars from an .env file. Place this file in root and don't forget to add this file to .gitignore:

# .env
PORT=3000
WEBHOOK_DOMAIN=bot.example.com
BOT_TOKEN=123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11

telegraf-docs's People

Contributors

dotcypress avatar wojpawlik avatar mkrhere avatar piterden avatar knorpelsenf avatar khuzha avatar vrumger avatar edjopato avatar ejnshtein avatar zfeed avatar kavayzin avatar tiagodanin avatar septs avatar backmeupplz avatar knightniwrem avatar fiblan avatar erfanium avatar dmbaranov avatar dzamataev avatar activedev1 avatar fowiflowi avatar krivahtoo avatar petrivah avatar zetavg avatar samleo8 avatar kylmakalle avatar siloss avatar suciptoid avatar temasm avatar delvedor avatar

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.