Slackbot responses

v1
Tony Olagbaiye 7 years ago
parent 9d4be2e31d
commit e6c48d79ea

@ -25,6 +25,7 @@ SRCS=slack.c \
api/slack-api-message.c \ api/slack-api-message.c \
api/slack-api-user-typing.c \ api/slack-api-user-typing.c \
api/message/slack-api-message-bot-message.c \ api/message/slack-api-message-bot-message.c \
api/message/slack-api-message-slackbot-response.c \
api/message/slack-api-message-unimplemented.c \ api/message/slack-api-message-unimplemented.c \
request/slack-request-chat-postmessage.c \ request/slack-request-chat-postmessage.c \
request/slack-request-channels-list.c \ request/slack-request-channels-list.c \

@ -9,6 +9,7 @@
#include "../../slack-channel.h" #include "../../slack-channel.h"
#include "../../slack-user.h" #include "../../slack-user.h"
#include "../slack-api-message.h" #include "../slack-api-message.h"
#include "slack-api-message-bot-message.h"
static const char *subtype = "bot_message"; static const char *subtype = "bot_message";

@ -0,0 +1,94 @@
#include <json.h>
#include <string.h>
#include "../../weechat-plugin.h"
#include "../../slack.h"
#include "../../slack-workspace.h"
#include "../../slack-message.h"
#include "../../slack-api.h"
#include "../../slack-channel.h"
#include "../../slack-user.h"
#include "../slack-api-message.h"
#include "slack-api-message-slackbot-response.h"
static const char *subtype = "slackbot_response";
static inline int json_valid(json_object *object, struct t_slack_workspace *workspace)
{
if (!object)
{
weechat_printf(
workspace->buffer,
_("%s%s: error handling websocket %smessage.%s%s message: "
"unexpected response from server"),
weechat_prefix("error"), SLACK_PLUGIN_NAME,
weechat_color("chat_value"), subtype, weechat_color("reset"));
return 0;
}
return 1;
}
int slack_api_message_slackbot_response_handle(struct t_slack_workspace *workspace,
const char *channel, const char *user,
const char *text, const char *ts)
{
struct t_slack_channel *ptr_channel;
struct t_slack_user *ptr_user;
struct t_slack_channel_typing *ptr_typing;
ptr_channel = slack_channel_search(workspace, channel);
if (!ptr_channel)
return 1; /* silently ignore if channel hasn't been loaded yet */
ptr_user = slack_user_search(workspace, user);
if (!ptr_user)
return 1; /* silently ignore if bot user hasn't been loaded yet */
char *message = slack_message_decode(workspace, text);
weechat_printf_date_tags(
ptr_channel->buffer,
(time_t)atof(ts),
"slack_message,slack_slackbot_response",
_("%s%s"),
slack_user_as_prefix(workspace, ptr_user, "slackbot"),
message);
free(message);
ptr_typing = slack_channel_typing_search(ptr_channel,
ptr_user->profile.display_name);
if (ptr_typing)
{
slack_channel_typing_free(ptr_channel, ptr_typing);
slack_channel_typing_cb(ptr_channel, NULL, 0);
}
return 1;
}
int slack_api_message_slackbot_response(struct t_slack_workspace *workspace,
json_object *message)
{
json_object *channel, *user, *text, *ts;
channel = json_object_object_get(message, "channel");
if (!json_valid(channel, workspace))
return 0;
user = json_object_object_get(message, "user");
if (!json_valid(user, workspace))
return 0;
text = json_object_object_get(message, "text");
if (!json_valid(text, workspace))
return 0;
ts = json_object_object_get(message, "ts");
if (!json_valid(ts, workspace))
return 0;
return slack_api_message_slackbot_response_handle(workspace,
json_object_get_string(channel),
json_object_get_string(user),
json_object_get_string(text),
json_object_get_string(ts));
}

@ -0,0 +1,8 @@
#ifndef _SLACK_API_MESSAGE_SLACKBOT_RESPONSE_H_
#define _SLACK_API_MESSAGE_SLACKBOT_RESPONSE_H_
int slack_api_message_slackbot_response(
struct t_slack_workspace *workspace,
json_object *message);
#endif /*SLACK_API_MESSAGE_SLACKBOT_RESPONSE_H*/

@ -8,6 +8,7 @@
#include "../../slack-channel.h" #include "../../slack-channel.h"
#include "../../slack-user.h" #include "../../slack-user.h"
#include "../slack-api-message.h" #include "../slack-api-message.h"
#include "slack-api-message-unimplemented.h"
static const char *subtype = "unimplemented"; static const char *subtype = "unimplemented";

@ -11,6 +11,7 @@
#include "slack-api-message.h" #include "slack-api-message.h"
#include "message/slack-api-message-unimplemented.h" #include "message/slack-api-message-unimplemented.h"
#include "message/slack-api-message-bot-message.h" #include "message/slack-api-message-bot-message.h"
#include "message/slack-api-message-slackbot-response.h"
static const char *type = "message"; static const char *type = "message";
@ -46,6 +47,7 @@ static struct stringcase cases[] =
, { "message_replied", &slack_api_message_unimplemented } , { "message_replied", &slack_api_message_unimplemented }
, { "pinned_item", &slack_api_message_unimplemented } , { "pinned_item", &slack_api_message_unimplemented }
, { "reply_broadcast", &slack_api_message_unimplemented } , { "reply_broadcast", &slack_api_message_unimplemented }
, { "slackbot_response", &slack_api_message_slackbot_response }
, { "thread_broadcast", &slack_api_message_unimplemented } , { "thread_broadcast", &slack_api_message_unimplemented }
, { "unpinned_item", &slack_api_message_unimplemented } , { "unpinned_item", &slack_api_message_unimplemented }
}; };
@ -118,7 +120,7 @@ int slack_api_message_route_message(struct t_slack_workspace *workspace,
json_object *message) json_object *message)
{ {
struct stringcase key; struct stringcase key;
key.string = type; key.string = subtype;
size_t case_count = sizeof(cases) / sizeof(cases[0]); size_t case_count = sizeof(cases) / sizeof(cases[0]);
void *entry_ptr = bsearch(&key, cases, case_count, void *entry_ptr = bsearch(&key, cases, case_count,

Loading…
Cancel
Save