Telegram bot和CouchDB将新用户插入DB

时间:2017-03-07 08:05:15

标签: javascript couchdb telegram-bot node-telegram-bot-api

如何将新的Telegram Bot用户插入CouchDB?
我在数据库中插入了样本数据jack johns并且没问题,但我不知道我应该如何使用电报用户用户名并将其放入Db中。 这是我的代码:

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);
});

1 个答案:

答案 0 :(得分:1)

Myselfe解决, 对于您可以使用User_ID: msg.from.id

的用户ID

这是我的代码:

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);
});