Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function ViberEnabledConnector(opts) {
var self = this;
this.options = opts || {};
this.viberBot = new ViberBot({
authToken: this.options.Token,
name: this.options.Name,
// It is recommended to be 720x720, and no more than 100kb.
avatar: this.options.AvatarUrl,
logger: logger
});
this.viberBot.on(BotEvents.MESSAGE_RECEIVED, (message, response) => {
self.processMessage(message, response);
});
this.viberBot.on(BotEvents.CONVERSATION_STARTED, (response, onFinish) => {
// onFinish(new TextMessage(`Hi, ${userProfile.name}! Nice to meet you.`))
var self = this;
var userProfile = response.userProfile;
var addr = {
const WebServerUrl = 'https://YOUR_BOT_SITE_URL'; // ToDo: This is the URL where the Viber bot is hosted. Has to be an external URL
const WebServerPort = 8080; // ToDo: This is the port of the Viber bot.
function createLogger() {
const logger = new winston.Logger({
level: "debug" // We recommend using the debug level for development
});
logger.add(winston.transports.Console, toYAML.config());
return logger;
}
const logger = createLogger();
// Creating the bot with access token, name and avatar
const bot = new ViberBot(logger, {
authToken: process.env.VIBER_PUBLIC_ACCOUNT_ACCESS_TOKEN_KEY || ViberPublicAccountAccessTokenKey,
name: ViberBotName,
avatar: ViberBotImageUrl
});
// create the MBF bot instance
const mbfBot = new MbfBotConnector(logger, new MbfPlatformViber(), {
clientName: MicrosoftBotDirectLineClientName,
secret: process.env.MICROSOFT_BOT_DIRECT_LINE_SECRET || MicrosoftBotDirectLineSecret,
pollInterval: process.env.MICROSOFT_BOT_POLL_INTERVAL || 1000});
mbfBot.on(MbfEvents.MBF_MESSAGE_RECEIVED, function(recipient, message) {
// send a MBF bot message back to Viber
bot.sendMessage(recipient, message);
});
public connect(): Observable {
if (this.connected) {
return Observable.of({ type: "connected", serviceID: this.serviceId() });
}
this.connected = true;
if (!this.token
|| !this.username) {
return Observable.throw(new Error("Credentials should exist."));
}
this.session = new Bot({
authToken: this.token,
avatar: this.avatar,
name: this.username,
});
this.webhookServer = http.createServer(this.session.middleware())
.listen(this.HTTPOptions.port, this.HTTPOptions.host, () =>
this.session.setWebhook(this.HTTPOptions.webhookURL)
.then(() =>
this.logger.info(`Server listening at port ${this.HTTPOptions.host}:${this.HTTPOptions.port}...`))
.catch((e: Error) => {
this.logger.error(e);
this.webhookServer.close();
}));
return Observable.of({ type: "connected", serviceID: this.serviceId() });
public connect(): Observable {
if (this.connected) {
return Observable.of({ type: 'connected', serviceID: this.serviceId() });
}
if (!this.token || !this.username) {
return Observable.throw(new Error('Credentials should exist.'));
}
if (!this.webhookURL) {
return Observable.throw(new Error('webhookURL should exist.'));
}
this.connected = true;
this.session = new Bot({
authToken: this.token,
avatar: this.avatar,
name: this.username,
});
this.router.post('/', this.session.middleware());
this.router.get('/', this.session.middleware());
if (this.webhookServer) {
this.webhookServer.listen();
}
return Observable.fromPromise(new Promise((resolve, reject) => {
this.session.setWebhook(this.webhookURL)
.then(() => resolve(true))
.catch((e: Error) => {
this.logger.error(e);
connect() {
if (this.connected) {
return Rx_1.Observable.of({ type: 'connected', serviceID: this.serviceId() });
}
if (!this.token || !this.username) {
return Rx_1.Observable.throw(new Error('Credentials should exist.'));
}
if (!this.webhookURL) {
return Rx_1.Observable.throw(new Error('webhookURL should exist.'));
}
this.connected = true;
this.session = new viber_bot_1.Bot({
authToken: this.token,
avatar: this.avatar,
name: this.username,
});
this.router.post('/', this.session.middleware());
this.router.get('/', this.session.middleware());
if (this.webhookServer) {
this.webhookServer.listen();
}
return Rx_1.Observable.fromPromise(new Promise((resolve, reject) => {
this.session.setWebhook(this.webhookURL)
.then(() => resolve(true))
.catch((e) => {
this.logger.error(e);
this.disconnect();
reject(e);
connect() {
if (this.connected) {
return Rx_1.Observable.of({ type: "connected", serviceID: this.serviceId() });
}
this.connected = true;
if (!this.token
|| !this.username) {
return Rx_1.Observable.throw(new Error("Credentials should exist."));
}
this.session = new viber_bot_1.Bot({
authToken: this.token,
avatar: this.avatar,
name: this.username,
});
this.webhookServer = http.createServer(this.session.middleware())
.listen(this.HTTPOptions.port, this.HTTPOptions.host, () => this.session.setWebhook(this.HTTPOptions.webhookURL)
.then(() => this.logger.info(`Server listening at port ${this.HTTPOptions.host}:${this.HTTPOptions.port}...`))
.catch((e) => {
this.logger.error(e);
this.webhookServer.close();
}));
return Rx_1.Observable.of({ type: "connected", serviceID: this.serviceId() });
}
disconnect() {
public connect(): Observable {
if (this.connected) {
return Observable.of({ type: "connected", serviceID: this.serviceId() });
}
this.connected = true;
if (!this.token
|| !this.username) {
return Observable.throw(new Error("Credentials should exist."));
}
this.session = new Bot({
authToken: this.token,
avatar: this.avatar,
name: this.username,
});
this.webhookServer = http.createServer(this.session.middleware())
.listen(this.HTTPOptions.port, this.HTTPOptions.host, () =>
this.session.setWebhook(this.HTTPOptions.webhookURL)
.then(() =>
this.logger.info(`Server listening at port ${this.HTTPOptions.host}:${this.HTTPOptions.port}...`))
.catch((e: Error) => {
this.logger.error(e);
this.webhookServer.close();
}));
return Observable.of({ type: "connected", serviceID: this.serviceId() });