Git Product home page Git Product logo

telegram-bot's Introduction

Telegram Bot


Starting project: telegram-bot-bootstrap by Wah Loon Keng

Prerequisites

Installation

  • new project folder
  • cd into it
  • run npm install telegram-bot-bootstrap

Usage

Local

  // testbot.js
  var bot = require('telegram-bot-bootstrap');
  var fs = require('fs');

  var myBot = new bot(your-token);

  myBot.getUpdates().then(console.log)
  // → you'll see an update message. Look for your user_id in "message.from.id"

  // Once you get your id to message yourself, you may:
  myBot.sendMessage(your-id, "Hello there")
  // → you'll receive a message from Alice.
  .then(console.log)
  // → optional, will log the successful message sent over HTTP
Messages Examples
  myBot.sendMessage(86953862, 'Hey wanna see some cool art?');

  myBot.sendPhoto(86953862, fs.createReadStream( __dirname + '/image.jpg' ), 'Image subtitle').then(console.log)

Message Example

Custom Keyboard Example

  var kb = {
          keyboard: [
              ['one'],
              ['two', 'three'],
              ['four', 'five', 'six']
          ],
          one_time_keyboard: true
      };
  myBot.sendMessage(86953862, "Choose a lucky number", undefined, undefined, kb)

Keyboard Example

Bootstrapped, Deployable Bot

See index.js for deployable app, and bot.js to customize bot commands.

We distinguish the bot from the API: bot.js extends API.js, and will be the deployed component.

This whole project is bootstrapped and deploy-ready: all the details of HTTP and server stuff taken care of for you. I deploy this git project onto my Heroku and voila, my bot is alive.

Setup

In addition to the token, you'll need a webhookUrl. If you deploy your Node app to Heroku, then the webhookUrl is simply your Heroku site url. Set both of them in the .env file:

PORT=8443
TOKEN=your-Telegram-bot-token
WEBHOOK=your-webhook-url

The sample available is an echo-bot. To make your bot do interesting stuff, head over to bot.js, under the handle method, start writing your own from below the Extend from here comment.

The bot inherits all the API methods, so you can simply call them for example by this.sendMessage.

Deployment

The server is deployed in index.js, and a bot is constructed to handle all HTTP POST calls from Telegram.

I use Heroku. This shall work for any other services too. Once I'm done setting up, I do:

git push heroku master

And done. Start talking to the bot.

telegram-bot's People

Contributors

giammaleoni avatar

Watchers

James Cloos avatar  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.