From a50784bbaaf7a9420f1aba60595a001977c7970a Mon Sep 17 00:00:00 2001 From: Maksymimlian Tomczyk Date: Tue, 11 Dec 2018 23:31:32 +0100 Subject: [PATCH] Added tools class to comment object --- README.MD | 136 ++++++++++++++++++++++++----------------- models/CommentTools.js | 26 ++++++++ models/Server.js | 3 + 3 files changed, 108 insertions(+), 57 deletions(-) create mode 100644 models/CommentTools.js diff --git a/README.MD b/README.MD index 5078d32..d437636 100644 --- a/README.MD +++ b/README.MD @@ -2,63 +2,65 @@ **IMPORTANT!** Incredbot is still under development, but it was successfully used for some projects! - * [What is incredbot?](#what-is-incredbot-) - * [Including](#including) - - [Configuration options](#configuration-options) - * [Server](#server) - + [Starting server](#starting-server) - + [Catching events](#catching-events) - + [Server events](#server-events) - + [Incredbot Message vs Raw Message](#incredbot-message-vs-raw-message) - * [Sender](#sender) - + [Methods](#methods) - - [`text(message, options)`](#-text-message--options--) - - [`quick_replies(message, replies, options)`](#-quick-replies-message--replies--options--) - - [`buttons(text, buttons, options)`](#-buttons-text--buttons--options--) - - [`attachment(type, url, options)`](#-attachment-type--url--options--) - - [`generic(elements, options)`](#-generic-elements--options--) - * [Helpers](#helpers) - + [Button](#button) - + [Generic Message](#generic-message) - + [Get Started Button](#get-started-button) - + [Greeting](#greeting) - + [Quick Reply](#quick-reply) - * [User object](#user-object) - + [Get user data](#get-user-data) - + [Send message](#send-message) - * [Static elements](#static-elements) - + [`incredbot.send.setting(data)`](#-incredbotsendsetting-data--) - - [Greeting](#greeting-1) - - [Get Started Button](#get-started-button-1) - - [Persistent Menu](#persistent-menu) - * [Natural typing](#natural-typing) - * [Generator Module](#generator-module) - + [Supported types](#supported-types) - - [Text](#text) - - [Quick Replies](#quick-replies) - - [Buttons](#buttons) - - [Generic](#generic) - + [Frame class](#frame-class) - * [Broadcast API](#broadcast-api) - + [Create Creative Message](#create-creative-message) - + [Create label](#create-label) - + [List labels](#list-labels) - + [Delete label](#delete-label) - + [Assign label to user](#assign-label-to-user) - + [Remove label from user](#remove-label-from-user) - + [List labels assigned to users](#list-labels-assigned-to-users) - + [List broadcasts](#list-broadcasts) - + [Get broadcast info](#get-broadcast-info) - + [Cancel planned broadcast](#cancel-planned-broadcast) - + [Send broadcast](#send-broadcast) - - [Options](#options) - + [Start broadcast size estimationId](#start-broadcast-size-estimationid) - + [Get estimation results](#get-estimation-results) - + [Get metrics of broadcast](#get-metrics-of-broadcast) - * [Examples](#examples) - + [Echo bot full example](#echo-bot-full-example) - * [License](#license) - * [About me 👨🏻‍💻](#about-me--------) +- [What is incredbot?](#what-is-incredbot-) +- [Including](#including) + - [Configuration options](#configuration-options) +- [Server](#server) + - [Starting server](#starting-server) + - [Catching events](#catching-events) + - [Server events](#server-events) + - [Incredbot Message vs Raw Message](#incredbot-message-vs-raw-message) +- [Sender](#sender) + - [Methods](#methods) + - [`text(message, options)`](#-text-message--options--) + - [`quick_replies(message, replies, options)`](#-quick-replies-message--replies--options--) + - [`buttons(text, buttons, options)`](#-buttons-text--buttons--options--) + - [`attachment(type, url, options)`](#-attachment-type--url--options--) + - [`generic(elements, options)`](#-generic-elements--options--) +- [Helpers](#helpers) + - [Button](#button) + - [Generic Message](#generic-message) + - [Get Started Button](#get-started-button) + - [Greeting](#greeting) + - [Quick Reply](#quick-reply) +- [User object](#user-object) + - [Get user data](#get-user-data) + - [Send message](#send-message) +- [Static elements](#static-elements) + - [`incredbot.send.setting(data)`](#-incredbotsendsetting-data--) + - [Greeting](#greeting-1) + - [Get Started Button](#get-started-button-1) + - [Persistent Menu](#persistent-menu) +- [Natural typing](#natural-typing) +- [Generator Module](#generator-module) + - [Supported types](#supported-types) + - [Text](#text) + - [Quick Replies](#quick-replies) + - [Buttons](#buttons) + - [Generic](#generic) + - [Frame class](#frame-class) +- [Broadcast API](#broadcast-api) + - [Create Creative Message](#create-creative-message) + - [Create label](#create-label) + - [List labels](#list-labels) + - [Delete label](#delete-label) + - [Assign label to user](#assign-label-to-user) + - [Remove label from user](#remove-label-from-user) + - [List labels assigned to users](#list-labels-assigned-to-users) + - [List broadcasts](#list-broadcasts) + - [Get broadcast info](#get-broadcast-info) + - [Cancel planned broadcast](#cancel-planned-broadcast) + - [Send broadcast](#send-broadcast) + - [Options](#options) + - [Start broadcast size estimationId](#start-broadcast-size-estimationid) + - [Get estimation results](#get-estimation-results) + - [Get metrics of broadcast](#get-metrics-of-broadcast) +- [Comments interaction](#comments-interaction) + - [Private Reply](#private-reply) +- [Examples](#examples) + - [Echo bot full example](#echo-bot-full-example) +- [License](#license) +- [About me 👨🏻‍💻](#about-me--------) ## What is incredbot? @@ -596,6 +598,26 @@ Fetches metrics of broadcast. const metrics = await incredbot.broadcast.getMetrics('2209764209284356') ``` +## Comments interaction + +For now, the only interaction with comment which is supported by incredbot is private replying. + +### Private Reply + +`comment.tools.replyPrivately(text)` + +Object tools, which contains replyPrivately method is only inside comment object. To send privte reply to comment you need to catch it's event first. + +```javascript +bot.on('comment', async (comment, raw) => { + try { + await comment.tools.replyPrivately('Hi! This is private reply!') + } catch (e) { + console.error(e) + } +}) +``` + ## Examples ### Echo bot full example diff --git a/models/CommentTools.js b/models/CommentTools.js new file mode 100644 index 0000000..b2db3d3 --- /dev/null +++ b/models/CommentTools.js @@ -0,0 +1,26 @@ +const axios = require('axios') + +const logger = require('../modules/winston') + +class CommentTools { + constructor(config, comment, user) { + this.comment = comment || null + this.user = user || null + this.private_reply_url = `https://graph.facebook.com/${config.api_version}/${this.comment.id}/private_replies?access_token=${config.access_token}` + } + + async replyPrivately(text) { + try { + if (!text) throw new Error('Private reply must contain text message!') + const data = await axios.post(this.private_reply_url, { + message: text + }) + return data.data + } catch (e) { + logger.error(e) + throw e + } + } +} + +module.exports = CommentTools diff --git a/models/Server.js b/models/Server.js index 750cda2..a19e5da 100644 --- a/models/Server.js +++ b/models/Server.js @@ -7,6 +7,8 @@ const emitter = new Emitter() const logger = require('../modules/winston') const Sender = require('./Sender.js') +const CommentTools = require('./CommentTools.js') + const app = express() app.use(bodyParser.urlencoded({ extended: false @@ -106,6 +108,7 @@ class Server { text: change.value.message, id: change.value.comment_id } + o.tools = new CommentTools(that.config, o.comment, o.user) emitter.emit('comment', o, change) } })