npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-telegram-bot-api-middleware-simpleauth

v0.1.0

Published

[![Build Status](https://travis-ci.org/idchlife/node-telegram-bot-api-middleware-simpleauth.svg?branch=master)](https://travis-ci.org/idchlife/node-telegram-bot-api-middleware-simpleauth)

Downloads

5

Readme

Build Status

Simple authentication middleware for https://github.com/idchlife/node-telegram-bot-api-middleware

So, what's the case for this middleware?

Imagine, that you have telegram bot. And you want it to become bot for you company, your friends and also somebody else. So... You have some private data and just answering to commands for you is not enough. You want some kind of 'registration' of users in your system. So, with this middleware you will get it! Beware, this is called SIMPLEauth for a reason. It uses sqlite3 and limited functionality.

As you know (if not yet, visit mentioned above repository node-telegram-bot-api-middleware), middleware is used to extend context of your next processed function callback for message/another middleware. This one does extend your list of middlewares with useful methods, properties for authentication and registration.

Installation

npm i node-telegram-bot-api-middleware-simpleauth --save

Usage

  // By default, simpleauth will create `simpleauth.sqlite3` file in your folder, 
  // for purpose of saving your authentication data. If you want to reset it,
  // just delete the file. You can also define your own filename. If you don't
  // want custom file, just omit passing arguments to .createMiddleware()
  const simpleauth = require('node-telegram-bot-api-simpleauth').createMiddleware({
    databaseFilename: 'YOUR_CUSTOM_DATABASE_FILE.sqlite3'
  });
  const use = require('node-telegram-bot-api-middleware');
  // When you use this middleware, your context will be populated with
  // object simpleauth. So you can use it via this.simpleauth
  const response = use(simpleauth);
  
  // Your configured bot
  const bot = require('./bot');
  
  bot.on('message', response(function* () {
    // You will have user, if user by chatId was already registered.
    // User object looks like this:
    // {
    //   id: number,
    //   firstName: string,
    //   lastName: string,
    //   chatId: number,
    //   username: string
    // }
    this.simpleauth.user
    
    // Method for checking if user authenticated
    this.simpleauth.isUserAuthenticated();
    
    // Method for checking if user is admin
    this.simpleauth.isCurrentUserAdmin();
    
    // Async method for making current user admin. Remember to use yield
    yield this.simpleauth.makeCurrentUserAdminAsync();
    
    // Async method for registering user who wrote the message with auth code
    yield this.simpleauth.registerCurrentTelegramUserWithCodeAsync(code);
    
    // Here start methods, that are from ./authentication.js
    // They are all async because they do work with database. Remember to use yield
    
    // Does auth code exist in database
    yield this.simpleauth.doesAuthCodeExist(code);
    
    // Did someone already use auth code for registration
    yield this.simpleauth.isAuthCodeAlreadyUsed(code);
    
    // Manually register user with your preferred arguments
    yield this.simpleauth.registerUser(username, firstName, lastName, chatId, code);
    
    // This one is important. Exactly it gives you new authentication/registration code,
    // so it can be passed to someone and used to register themselves in the system.
    // It's up to you how you use this method. In examples/working-auth-bot.js this used only by admin
    const code = yield this.simpleauth.generateAuthCode();
  });

Check example, it will show middleware in action

For working example you can see examples/working-auth-bot.js To test it you need to start it like this:

node examples/working-auth-bot.js YOUR_BOT_TOKEN

You can help with code, ideas and bugs by creating issues and pull requests.

Yes, this middleware may have some bugs and unexpected behaviour and you can help me to improve it!