Z1woo

records robot v1.0

1 +node_modules
2 +package-lock.json
...\ No newline at end of file ...\ No newline at end of file
1 +const TelegramBot = require('node-telegram-bot-api');
2 +const express = require('express');
3 +const app = express();
4 +const mongoose = require('mongoose');
5 +
6 +const token = 'your bot token';
7 +const db = require('./config').mongoURI;
8 +
9 +const User = require('./models/User');
10 +// Bot
11 +const bot = new TelegramBot(token, { polling: true });
12 +
13 +// connect to mongoDB
14 +mongoose
15 + .connect(db, { useNewUrlParser: true })
16 + // { useCreateIndex: true, useUnifiedTopology: true, useNewUrlParser: true, useUnifiedTopology: true }
17 + .then(() => console.log("MongoDB Connected"))
18 + .catch(err => console.log(err));
19 +
20 +// start
21 +bot.onText(/\/start/, msg => {
22 + const chatId = msg.chat.id;
23 + bot.sendMessage(chatId, "Welcome");
24 +});
25 +
26 +// do
27 +bot.onText(/\/todo/, msg => {
28 + let todo = msg.text
29 + .split(' ')
30 + .slice(1)
31 + .join(' ');
32 +
33 + if (!todo) {
34 + return bot.sendMessage(msg.chat.id, "please input your todo item.");
35 + }
36 +
37 + User.findOne({ user: msg.chat.username })
38 + .then(user => {
39 + if (!user) {
40 + //creat new
41 + const newUser = new User({
42 + user: msg.chat.username,
43 + todos: [todo]
44 + });
45 +
46 + //save newuser
47 + newUser.save()
48 + .then(console.log('New User Saved'))
49 + .catch(err => console.log(err));
50 + }
51 + else {
52 + ///add new item
53 + user.todos.push(todo);
54 + User.update(
55 + { user: user.user },
56 + { $set: { todos: user.todos } },
57 + (err, raw) => {
58 + if (err) return console.log(err);
59 + console.log("Success Added new item");
60 + }); // there user => moudule key user: findOne user.findOne user key(Schema users config)
61 + }
62 + });
63 + bot.sendMessage(msg.chat.id, "You success added a item");
64 +});
65 +
66 +// list
67 +bot.onText(/\/list/, msg => {
68 + User.findOne({ user: msg.chat.username }).then(user => {
69 + if (!user) {
70 + return bot.sendMessage(msg.chat.id, "you haven\t added a item.")
71 + }
72 + else {
73 + if (user.todos.length === 0) return bot.sendMessage(msg.chat.id, 'You already done all!');
74 +
75 + let todoList = "";
76 + user.todos.forEach((todo, index) => {
77 + todoList += `[${index}] - ` + todo + "\n";
78 + });
79 + return bot.sendMessage(msg.chat.id, `your list: \n\n ${todoList}`);
80 + }
81 + });
82 +});
83 +
84 +/// Check
85 +bot.onText(/\/check/, msg => {
86 + User.findOne({ user: msg.chat.username }).then(user => {
87 + if (!user) {
88 + return bot.sendMessage(msg.chat.id, "you haven\t added a item.")
89 + }
90 + else {
91 + if (user.todos.length === 0) return bot.sendMessage(msg.chat.id, 'You already done all!');
92 + let num = msg.text.split(' ')[1];
93 +
94 + // No num passed in
95 + if (!num) {
96 + return bot.sendMessage(
97 + msg.chat.id,
98 + "Please input the number."
99 + );
100 + }
101 +
102 + // wrong num
103 + if (num >= user.todos.length || num < 0) {
104 + return bot.sendMessage(
105 + msg.chat.id,
106 + "There's no item with the number, please type /list and check it."
107 + );
108 + }
109 +
110 + //Remove todo from mongoDB
111 + user.todos.splice(num, 1);
112 + User.update({ user: user.user }, { $set: { todos: user.todos } }, (err, raw) => {
113 + if (err) return console.log(err);
114 + bot.sendMessage(msg.chat.id, "Done.");
115 + });
116 + }
117 + })
118 +})
119 +
1 +module.exports = {
2 + mongoURI:
3 + // mongodb url
4 + "mongodb+srv://<USERNAME>:<PASSWORD>@cluster0.prz2p.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"
5 +};
...\ No newline at end of file ...\ No newline at end of file
1 +const mongoose = require('mongoose');
2 +const Schema = mongoose.Schema;
3 +
4 +const UserSchema = new Schema({
5 + user: {
6 + type: String,
7 + require: true
8 + },
9 + todos: [
10 + {
11 + type: String,
12 + required: true
13 + }
14 + ]
15 +})
16 +
17 +module.exports = User = mongoose.model('users', UserSchema);
18 +
19 +// const mongoose = require('mongoose');
20 +// const Schema = mongoose.Schema;
21 +
22 +// // create Schema
23 +// const UserSchema = new Schema({
24 +// user: {
25 +// type: String,
26 +// required: true
27 +// },
28 +// todos: [
29 +// {
30 +// type: String,
31 +// require: true
32 +// }
33 +// ]
34 +// })
35 +
36 +// module.exports = User = mongoose.model('users', UserSchema)
...\ No newline at end of file ...\ No newline at end of file
1 +{
2 + "name": "telegrambot",
3 + "version": "1.0.0",
4 + "description": "",
5 + "main": "index.js",
6 + "scripts": {
7 + "test": "echo \"Error: no test specified\" && exit 1"
8 + },
9 + "repository": {
10 + "type": "git",
11 + "url": "http://khuhub.khu.ac.kr/2020105575/TelegramBot.git"
12 + },
13 + "author": "",
14 + "license": "ISC",
15 + "dependencies": {
16 + "express": "^4.17.1",
17 + "mongoose": "^5.2.6",
18 + "node-telegram-bot-api": "^0.53.0"
19 + }
20 +}