All Projects → coin-au-carre → slacking

coin-au-carre / slacking

Licence: other
Modern Slack C++ 11 library for communicating with the Web Slack API

Programming Languages

C++
36643 projects - #6 most used programming language

Projects that are alternatives of or similar to slacking

slackr
Simple shell command to send or pipe content to slack via webhooks. (To upload snippets or files use: www.github.com/a-sync/slackfu)
Stars: ✭ 41 (-29.31%)
Mutual labels:  slackbot, slack-api
git-slack-notify
Sends Slack notifications for new commits in Git repositories
Stars: ✭ 12 (-79.31%)
Mutual labels:  slackbot, slackapi
fridaybot
Slack bot for https://spb-frontend.slack.com
Stars: ✭ 29 (-50%)
Mutual labels:  slackbot, slack-api
operationcode-pybot
Operation Code's Official Slackbot
Stars: ✭ 29 (-50%)
Mutual labels:  slackbot, slack-api
Slack Ruby Client
A Ruby and command-line client for the Slack Web, Real Time Messaging and Event APIs.
Stars: ✭ 957 (+1550%)
Mutual labels:  slackbot, slack-api
Slack Machine
A sexy, simple, yet powerful and extendable Slack bot
Stars: ✭ 91 (+56.9%)
Mutual labels:  slackbot, slack-api
inspireNuggetsSlackBot
A simple Slackbot that displays random inspiring techie quotes and jokes for developers/designers.
Stars: ✭ 15 (-74.14%)
Mutual labels:  slackbot, slack-api
dienstplan
Slack bot app for duty rotations
Stars: ✭ 14 (-75.86%)
Mutual labels:  slackbot, slack-api
Slacker
Slack Bot Framework
Stars: ✭ 495 (+753.45%)
Mutual labels:  slackbot, slack-api
Slack Poster
Simple gem to post messages on Slack using web hooks.
Stars: ✭ 49 (-15.52%)
Mutual labels:  slackbot, slack-api
Slack Block Builder
Lightweight, no-dependency JavaScript library for creating Slack Block Kit UIs, with a builder syntax, inspired by SwiftUI.
Stars: ✭ 129 (+122.41%)
Mutual labels:  slackbot, slack-api
slackify
Build Slackbot on Rails using Slack Event API
Stars: ✭ 20 (-65.52%)
Mutual labels:  slackbot
Knary
A simple HTTP(S) and DNS Canary bot with Slack/Discord/MS Teams & Pushover support
Stars: ✭ 187 (+222.41%)
Mutual labels:  slackbot
Chatskills
Run and debug Alexa skills on the command-line. Create bots. Run them in Slack. Run them anywhere!
Stars: ✭ 171 (+194.83%)
Mutual labels:  slackbot
Fantasy football chat bot
GroupMe Discord and Slack Chatbot for ESPN Fantasy Football
Stars: ✭ 166 (+186.21%)
Mutual labels:  slackbot
on-demand-delivery-fastlane-slack
iOS On-Demand delivery architecture via slack/jenkins/fastlane. Demo project for: #PragmaConf2019
Stars: ✭ 18 (-68.97%)
Mutual labels:  slackbot
coalibot
Slack bot for the school 42 using nlopes/slack Slack api client. This bot provide tools for student to access school informations.
Stars: ✭ 22 (-62.07%)
Mutual labels:  slackbot
Office Simulator
Miss the office life? You won't any more with this wonderful office slack simulator.
Stars: ✭ 152 (+162.07%)
Mutual labels:  slackbot
slack-java-webhook
Java Client to Slack's Webhook feature.
Stars: ✭ 67 (+15.52%)
Mutual labels:  slackapi
Slackbotapi
node.js Slack RTM API module
Stars: ✭ 135 (+132.76%)
Mutual labels:  slackbot

Slacking - Be lazy and send messages easily in Slack with C++

Language Standard License Build Status Build status GitHub version Slack Channel

Simple C++ Slack API

Slacking is a lightweight C++11 header only library (only two header files !) for communicating with the Slack Web API.
Slacking aims to be easy to use and stick to the API style proposed by Slack. Slacking requires to have an API token.

Requirements

No special requirement. You should already have these :

  • C++11 compatible compiler. Tested with Clang (3.5, 3.6, 3.7), GCC (4.9, 5), MSCV (VS 14 2015, VS 15 2017)
  • libcurl

Note: Slacking uses Nlohmann Json which is available in include/json.hpp

Installation

Just copy the include/slacking folder in your project and you can #include "slacking.hpp" to your code. That's all.

Example usage

Initialize a slack instance

auto& slack = slack::create("xxx-xxx-xxx-xxx"); // "xxx-xxx-xxx-xxx" is your Slack API token
slack.chat.channel = "#general"; // set a default channel

Sending a message in Slack is easy (preview)

slack.chat.postMessage("Hello there!"); // will send the message "Hello there!" in the channel #general with the registered token

Note that we try to stick with the Slack API documentation: the chat.postMessage syntax is easily recognizable.

Sending a message with a specified channel, username and icon (preview)

slack.chat.channel_username_iconemoji("#superchannel", "Ghost Bot", ":ghost:");
slack.chat.postMessage("Slacking is simple"); // will send the message "Hello there!" in the channel #general with the registered token

Note that theses changes are persistent inside the slack instance.

Sending complex messages (functionnal approach) (preview)

If you need maximum control, you can use the generic functions slack::post or slack::get.
Everything available in Web Slack API is possible from here.

slack::post (   
    "chat.postMessage",
    {
        {"text"      , "Slacking is awesome!" },
        {"channel"   , "#mychannel"           },
        {"username"  , "peach"                },
        {"icon_emoji", ":princess:"           }
    } // note that "token" is not needed here and is a "registered" parameter
); // it is automatically inserted when using slack::post()

Sending complex messages (JSON approach) (preview)

If you prefer to mimic the JSON approach given in the API, you can also use this syntax:

 auto json = R"({
    "text":         "But Our Princess is in Another Castle!",
    "channel":      "#general",
    "username":     "peach",
    "icon_emoji":   ":princess:"
})"_json;

slack::post("chat.postMessage", json);

Check out the examples for more illustrations.

A more elaborated example (preview)

You can make richly-formated messages with attachments.

slack.chat.channel_username_iconemoji("#ticker-channel", "Support Bot", ":hamster:");

auto json_attachments = R"([
    {
        "fallback": "New ticket from Bjarne Stroustrup - Ticket #2017: Still looking for reflection",
        "pretext": "New ticket from Bjarne Stroustrup",
        "title": "Ticket #2017: Still looking for reflection",
        "title_link": "https://www.youtube.com/watch?v=ND-TuW0KIgg",
        "text": "Help me adding reflection!",
        "color": "#7CD197",
        "image_url": "https://img.youtube.com/vi/ND-TuW0KIgg/2.jpg"
    }
])"_json;

slack.chat.attachments = json_attachments;
auto response = slack.chat.postMessage(); // get a slack::Json object
std::cout << response << std::endl;

Slacking attachments

The output received is a JSON response sent back by the Slack API:

{"channel":"C1AUF9AN4","message":{"attachments":[{"color":"7CD197","fallback":"New ticket from Bjarne Stroustrup - Ticket #2017: Still looking for reflection","id":1,"image_bytes":4820,"image_height":90,"image_url":"https://img.youtube.com/vi/ND-TuW0KIgg/2.jpg","image_width":120,"pretext":"New ticket from Bjarne Stroustrup","text":"Help me adding reflection!","title":"Ticket #2017: Still looking for reflection","title_link":"https://www.youtube.com/watch?v=ND-TuW0KIgg"}],"bot_id":"B20LJ4Y12","icons":{"emoji":":hamster:","image_64":"https://slack.global.ssl.fastly.net/d4bf/img/emoji_2015_2/apple/1f439.png"},"subtype":"bot_message","text":" ","ts":"1464251666.000063","type":"message","username":"Support Bot"},"ok":true,"ts":"1464251666.000063"}

Since Slack::Json is a typedef to a nlohmann::json, you have all the features of the latter one (conversions, STL like access, ...). For instance, response["ok"] will give true.

A word about error handling

By default, Slacking will throw a runtime error exception if the curl request does not succeed, if the response from Slack is not correct, or if response["ok"] received is not true. You are free to handle these exceptions the way you like.

Since 0.2, you are now able to prevent throw exceptions by setting false to these functions slack::create("xxx-xxx", false) or slack.set_throw_exception(false). If you do that, a warning will be displayed and you won't have to try/catch every postMessage for instance if you want to avoid brutal stops in your program.

Ongoing work

You can use the slack::post or slack::get methods to fully exploit the Slack Web API methods. You can refer to examples/06-custom_post_get.cpp.cpp.

Following C++ helpers free functions and member methods are available in Slacking for convenience :

Try out the "magic" functions for grabbing ready-to-use structures. This is an ongoing work so more convenient helpers functions and structures might come in the near future...
If you need any features feel free to ask and contribute.

Manage Slacking instance

Here are two approaches to keep alive the Slacking session in your program so you can use it anytime, anywhere.

Use Meyers singleton

Slacking provides free convenient functions : slack::create(const std::string& token) and slack::instance(). Initialize the Slacking instance with:

auto& slack = slack::create("xxx-xxx-xxx-xxx");

And when you are in another scope and you have lost the slack reference, you can grab it again with :

auto& slack = slack::instance();

It might not be the recommended way but since we generally want to handle only one Slack instance (one token), it is highly convenient. You can refer to the example usage and examples/01-basic.cpp.

Pass by reference (or by pointer)

An other approach is to pass the Slacking instance by reference, store it, and call the appropriate methods when needed.

void foo(slack::Slacking& slack) {
    slack.chat.postMessage("I am slacking!", "#random");
}

int main() {
    slack::Slacking slack_instance{"xxx-xxx-xxx-xxx"};
    foo(slack_instance);
}

You can use a std::reference_wrapper as shown in examples/02-basic.cpp. This strategy is useful if you have to manage several Slacking instances.

Build the examples

mkdir build && cd build
cmake .. && make
examples/[whatever]

In your project, if you want a verbose output like when running the examples, add the following compilation flag:
-DSLACKING_VERBOSE_OUTPUT=1.

Note for Windows users

You might have difficulties handling libcurl where CMake throws Could NOT find CURL (missing: CURL_LIBRARY CURL_INCLUDE_DIR).

One way to solve this is to grab the curl version for Windows here, copy the content of include in appropriate folders available visible in your PATH (e.g. if in your Git installation [...]/Git/mingw64/include/). You also need to grab the curl.lib and the libcurl.dll files from here and copy them in appropriate folders (e.g. if in your Git installation [...]/Git/mingw64/lib/).

mkdir build && cd build
cmake .. -DCMAKE_GENERATOR_PLATFORM=x64
cmake --build .
cmake --build . --target 00-showcase # For a specific target

Or if you prefer using GNU GCC on Windows

cmake -G "MSYS Makefiles" -D CMAKE_CXX_COMPILER=g++ ..
make
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].