Skip to content

AnmoL-09/slack-api-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Slack Messaging Bot

A powerful and lightweight Node.js bot for automating Slack messaging operations. Built with simplicity and reliability in mind.

✨ Features

  • 📤 Send messages to any channel
  • 📝 Edit existing messages
  • 🗑️ Delete messages
  • ⏰ Schedule future messages
  • 📥 Retrieve message history
  • 🔒 Secure token handling
  • ⚡ Error handling & logging

🚀 Quick Start

# Clone the repository
git clone https://github.com/yourusername/slack-messaging-bot.git

# Navigate to project directory
cd slack-messaging-bot

# Install dependencies
npm install

# Create .env file
echo "SLACK_BOT_TOKEN=xoxb-your-token
CHANNEL_ID=your-channel-id" > .env

# Run the bot
npm start

🔧 Setup

  1. Create a Slack App

    • Visit api.slack.com/apps
    • Click "Create New App" → "From scratch"
    • Name your app and select workspace
  2. Configure Permissions

    • Go to "OAuth & Permissions"
    • Add these scopes:
      chat:write
      chat:write.public
      chat:delete
      chat:edit
      channels:history
      groups:history
      im:history
      mpim:history
      
  3. Install App

    • Click "Install to Workspace"
    • Copy the "Bot User OAuth Token"
  4. Environment Setup

    • Create .env file in project root
    • Add your credentials:
      SLACK_BOT_TOKEN=xoxb-your-token
      CHANNEL_ID=your-channel-id
      

💻 Usage

const { sendMessage, editMessage, deleteMessage } = require('./slackMessaging');

// Send a message
const ts = await sendMessage("Hello, world!");

// Edit after 2 seconds
await new Promise(r => setTimeout(r, 2000));
await editMessage(ts, "Updated message!");

// Delete after 2 seconds
await new Promise(r => setTimeout(r, 2000));
await deleteMessage(ts);

📚 API

sendMessage(text)

Sends a message to the configured channel.

const ts = await sendMessage("Hello!");

editMessage(ts, newText)

Edits an existing message.

await editMessage(ts, "Updated text");

deleteMessage(ts)

Deletes a message.

await deleteMessage(ts);

scheduleMessage(text, secondsAhead)

Schedules a message for future delivery.

await scheduleMessage("Future message", 3600); // 1 hour

getMessages(limit)

Retrieves recent messages.

const messages = await getMessages(10); // Last 10 messages

🛠️ Development

# Install dependencies
npm install

# Run the bot
npm start

# Format code
npm run format

# Lint code
npm run lint

🔍 Error Handling

The bot includes comprehensive error handling:

  • Input validation
  • API error handling
  • Custom error classes
  • Detailed logging

📦 Dependencies

  • axios - HTTP client
  • dotenv - Environment management

🤝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📝 License

This project is licensed under the ISC License.

👨‍💻 Author

Anmol Mahobiya

🙏 Acknowledgments

  • Slack API Documentation
  • Node.js Community

About

A demonstration on how to use the slack API for a bot.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors