0

How Can I insert New Telegram Bot user to CouchDB?
I inserted a Sample Data jack johnsin db and was ok, But I Don't Know How Should I Take Telegram users Username and Put That in Db. This is My Code:

import 'babel-polyfill';  
import './env';
import TelegramBot from 'node-telegram-bot-api';    
const bot = new TelegramBot(process.env.BOT_TOKEN, {polling: true});
///////////////////////////////////   Sample Data
var server = require('couch-db')('http://localhost:5984');

var db = server.database('users');
db.destroy(function(err) {
    // create a new database
    db.create(function(err) {
        // insert a document with id 'jack johns'
        db.insert({ _id: 'jack johns', name: 'jack' }, function(err, body) {
            if (err) {
                console.log('insertion failed ', err.message);
                return;
            }
            console.log(body);
            // body will like following:
            //   { ok: true,
            //     id: 'jack johns',
            //     rev: '1-610953b93b8bf1bae12427e2de181307' }
        });
    });
});
//////////////////////////////
bot.onText(/^[\/!#]start$/, msg => {
 const opts = {
    reply_to_message_id: msg.message_id,
    reply_markup: JSON.stringify({
      keyboard: [['Store username']],
      resize_keyboard:true,
      one_time_keyboard: true
    })
  };
  bot.sendMessage(msg.chat.id, 'You Are Exist in DB', opts);
});
Saeed Heidarizarei
  • 8,406
  • 21
  • 60
  • 103

1 Answers1

1

Solved By Myselfe, For User ID You Can Use User_ID: msg.from.id

This is My Code:

bot.onText(/^[\/!#]start$/, msg => {

    db.insert({ _id: msg.from.username }, function(err, body) {
            if (err) {
                console.log('insertion failed ', err.message);
                return;
            }
            console.log(body);

        });

 const opts = {
    reply_to_message_id: msg.message_id,
    reply_markup: JSON.stringify({
      keyboard: [['Store username']],
      resize_keyboard:true,
      one_time_keyboard: true
    })
  };
  bot.sendMessage(msg.chat.id, 'You Are Exist in DB', opts);
});
Saeed Heidarizarei
  • 8,406
  • 21
  • 60
  • 103