Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/**
* Example for using the Slack RTM API.
*/
const { RTMClient } = require('@slack/client');
// Get an API token by creating an app at
// It's always a good idea to keep sensitive data like the token outside your source code. Prefer environment variables.
const token = process.env.SLACK_API_TOKEN || '';
if (!token) { console.log('You must specify a token to use this example'); process.exitCode = 1; return; }
// Initialize an RTM API client
const rtm = new RTMClient(token);
// Start the connection to the platform
rtm.start();
// Log all incoming messages
rtm.on('message', (event) => {
// Structure of `event`:
console.log(`Message from ${event.user}: ${event.text}`);
})
// Log all reactions
rtm.on('reaction_added', (event) => {
// Structure of `event`:
console.log(`Reaction from ${event.user}: ${event.reaction}`);
});
rtm.on('reaction_removed', (event) => {
// Structure of `event`:
useEffect(() => {
const params = {
logLevel: LogLevel.DEBUG,
autoReconnect: true,
useRtmConnect: false
};
const rtm = new RTMClient(token, params);
rtm.start().then(res => console.log("rtm started", res));
rtm.on("message", message => {
// Log the message
console.log(
`(channel:${message.channel}) ${message.user} says: ${message.text}`
);
});
}, []);
}
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
var config = require('./config');
var RegexBot = require('./regexbot');
var randomiser = function (max) {
return Math.floor(Math.random() * max);
};
var regexbot = new RegexBot(config, randomiser);
const { RTMClient, WebClient, ErrorCode } = require('@slack/client');
var rtm = new RTMClient(config.slack_api_token);
rtm.start();
var web = new WebClient(config.slack_api_token);
rtm.on('authenticated', function (rtmStartData) {
console.log(`Logged in as "${rtmStartData.self.name}" of team "${rtmStartData.team.name}", but not yet connected to a channel`);
console.log(rtmStartData.self.id);
config.build(rtmStartData.self.id);
});
rtm.on('message', function (message) {
console.log('Received a message');
if (message.subtype === 'bot_message' || message.hasOwnProperty('bot_id')) {
return;
}
setupRTMClient(token) {
this.authorizationHeader = `Bearer ${token}`
const options = {
autoReconnect: true,
// This is passed to `retry`, should do retry in a short forever manner,
// since Slack can not recover if transiting to failure state, and we do
// not know when network would recover.
retryConfig: {maxTimeout: 2 * 1000, forever: true},
}
// Start real time client.
this.rtm = new RTMClient(token, options)
require('./private-apis').extend(this.rtm)
this.rtm.once('authenticated', this.ready.bind(this))
this.rtm.start({batch_presence_aware: true})
// Indicate whether this is reconnection.
this.isReconnect = false
this.rtm.on('error', this.reportError.bind(this))
this.rtm.on('connected', this.handleConnection.bind(this))
this.rtm.on('disconnected', this.handleDisconnection.bind(this))
this.rtm.on('connecting', this.handleConnecting.bind(this))
this.rtm.on('reconnecting', this.handleConnecting.bind(this))
this.rtm.on('message', this.dispatchMessage.bind(this))
this.rtm.on('reaction_added', this.setReaction.bind(this, true))
this.rtm.on('reaction_removed', this.setReaction.bind(this, false))
/**
* Created by kylejohnson on 09/10/2016.
*/
const data = require('../lib/_data');
const { RTMClient } = require('@slack/client');
const _ = require('lodash');
const config = require('../config')
const rtm = new RTMClient(config.slackToken, { logLevel: 'error' });
var channel = null;
const handleImageUrl = require('../inputs/image-url-input');
const handleGIFUrl = require('../inputs/gif-url-input');
const handleVideoUrl = require('../inputs/video-url-input');
var SlackClient = function () {
var self = this;
this.init = function () {
if (!config.slackToken) return;
rtm.on('error', err => console.log('Slack failed to initialise', err));
const promise = Promise.all([
data.post('https://slack.com/api/channels.list?token=' + config.slackToken + '&exclude_archived=1')
.then(function (res) {
return _.find(res.channels, { name: config.slackChannelName });
getNewSlackRtmClient(token) {
return new slack.RTMClient(token, {
logLevel: this.config.rtmClientLogLevel || 'info',
retryConfig: { forever: true, maxTimeout: 60000 },
});
}
}
Slack.prototype.connect = function initialize () {
if (this.config.token) {
this.slack = new SlackSDK.WebClient(this.config.token);
this.connection = new SlackSDK.RTMClient(this.config.token);
this.connection.on('ready', this.ready.bind(this));
this.connection.on('message', this.handler.bind(this));
this.connection.on('team_join', this._team_join.bind(this));
this.connection.on('channel_created', this._channel_created.bind(this));
this.connection.on('presence_change', this._presence_change.bind(this));
this.connection.on('member_joined_channel', this._member_joined_channel.bind(this));
this.connection.on('member_left_channel', this._member_left_channel.bind(this));
this.connection.start({
batch_presence_aware: true
});
}
};
export function startRTMForTeam(team: Team) {
if (!getRTMForTeam(team._id)) _rtmByTeam.set(team._id, new RTMClient(team.bot.access_token))
const rtm = getRTMForTeam(team._id)!
if (!rtm.connected) rtm.start()
return rtm
}
() => {
const webClient: any = new WebClient(token)
const rtmClient: any = new RTMClient(token)
Promise.resolve().then(async () => {
const { team } = await webClient.team.info()
const { channels } = await webClient.conversations.list({
exclude_archived: true,
types: 'public_channel,private_channel,im,mpim'
})
workspace.name = team.name
workspace.rooms = channels
workspace.sendText = (room, text) =>
webClient.chat.postMessage({
channel: room.id,
text
})
constructor() {
if (config.vpdb.logging.slack && config.vpdb.logging.slack.enabled) {
this.enabled = true;
this.config = config.vpdb.logging.slack;
this.web = new WebClient(this.config.token);
this.rtm = new RTMClient(this.config.token, { logLevel: 'info', mrkdwn: true } as RTMClientOptions);
this.rtm.on('authenticated', rtmStartData => {
logger.info(null, `[SlackBot] Logged in as ${rtmStartData.self.name} of team ${rtmStartData.team.name}, but not yet connected to a channel.`);
});
this.rtm.start({});
}
}