我试图创建一个松弛的应用程序,使用传入的网络钩子。我希望我的github存储库在更新wiki时发布到松弛。我相信我已经在github上设置了网络钩子,因为我可以看到,每当我更新wiki时,它都在尝试传递。然而,总有一个错误,"no_text“。我认为这个错误意味着slack期待一个名为"text“的项目,但是github的有效负载没有提供任何内容。我通过尝试命令提示符中的两个curl命令(我在windows上)验证了这一点:
curl -X POST -H "Content-type: application/json" --data "{\"text\":\"Hello, World!\"}" [MY_WEBHOOK_URL]
curl -X POST -H "Content-type: application/json" --data "{\"foobar\":\"Hello, World!\"}" [MY_WEBHOOK_URL]
第一个如预期的工作;消息“你好,世界!”被发布到我想要的空闲频道上,我从curl那里得到了"ok“消息。第二个没有工作;消息没有发布,我从curl中得到了消息"no_text“。
我可以想出解决这个问题的两种可能的办法:
我不知道如何做到这两件事,也不知道它们是否可能。或者还有另一种我没想过的办法?
注意:我已经尝试使用github松弛应用程序,但不知道如何让它发布更新到wiki。(如果你愿意的话,请看我的另一个问题:slack github integration doesn't find wiki repository)
发布于 2017-04-01 00:45:36
我现在想和你做同样的事。因为github和松弛钩子本质上是不同的,所以您需要在中间有一些东西才能将github webhooks处理成一条将通过传入的web钩子发布的slack消息。
您需要做几件不同的事情(没有特别的顺序):
对于github webhooks,您需要利用更强大的github来创建钩子。您可以使用curl
来完成这个任务,但是这有点痛苦,所以我使用JS脚本来处理它。在运行以下内容之前,您需要在同一个目录中运行npm install github bluebird
:
var GitHubApi = require("github");
var github = new GitHubApi({
// optional
debug: true,
protocol: "https",
host: "api.github.com", // should be api.github.com for GitHub
pathPrefix: "", // for some GHEs; none for GitHub
headers: {
"user-agent": "ocelotsloth-conf" // GitHub is happy with a unique user agent
},
Promise: require('bluebird'),
followRedirects: false, // default: true; there's currently an issue with non-get redirects, so allow ability to disable follow-redirects
timeout: 5000
});
// user token
github.authenticate({
type: "token",
token: "GITHUB_TOKEN_HERE",
});
// https://mikedeboer.github.io/node-github/#api-repos-createHook
github.repos.createHook({
owner: "ocelotsloth",
repo: "lib-ical",
name: "amazonsns",
events: [
//"commit_comment",
//"create",
//"delete",
//"gollum",
//"issue_comment",
"issues"
//"label",
//"milestone",
//"pull_request",
//"pull_request_review",
//"pull_request_review_comment",
//"push",
//"release"
],
config: {
aws_key: "AWS_KEY",
aws_secret: "AWS_SECRET",
sns_region: "us-east-1",
sns_topic: "SNS_TOPIC_ARN"
},
}, function(err, res) {
console.log(JSON.stringify(res, null, '\t'));
});
我记得不久前我在博客上写了一篇关于设置SNS主题以正常工作的文章,但我已经不记得它到底在哪里了。谷歌应该能帮上忙。此外,如果您想避免复杂性,您应该能够为github设置自己的服务器,以便将这些服务器发送到并避免设置AWS。有关该方法的具体说明,请参见https://mikedeboer.github.io/node-github/#api-repos-createHook。您需要在创建钩子之后使用editHook
,所以要么在第一次正确使用它,要么使用编辑它。只需将方法调用更改为editHook,并将id
添加到调用中即可。
重要的是,您可以定义希望github发送给您的所有不同的Events
。对于所有这些,以及它们的格式,请查看https://developer.github.com/v3/activity/events/types/。
为了真正地发布这些事件,我有一个lambda脚本,它现在看起来像这样(我今天刚刚开始编写这个脚本,除了发布问题事件之外,它还没有实现更多,但它应该是一个很好的起点)。对于这个脚本,您将需要npm install identify-github-event slack-webhook
并设置传入的web钩子。
var identifyGithubEvent = require('identify-github-event');
var SlackWebhook = require('slack-webhook')
// slack's link syntax
function link(url, txt) {
return "<" + url + "|" + txt + ">";
}
exports.handler = function(event, context) {
// 1. extract GitHub event from SNS message
var ghEvent = JSON.parse(event.Records[0].Sns.Message);
var eventType, eventName, numb;
console.log(ghEvent);
var ghEventType = identifyGithubEvent(ghEvent);
if (!ghEventType) {
return;
}
var text = "Event! " + ghEventType;
if (ghEventType === 'IssueCommentEvent') {
var who = link(ghEvent.comment.user.html_url, ghEvent.comment.user.login);
var what = link(ghEvent.issue.html_url, "Issue " + ghEvent.issue.number + ": \"" + ghEvent.issue.title + "\"");
text = who + " commented on " + what;
}
else if (ghEventType === 'IssuesEvent') {
var who = link(ghEvent.sender.html_url, ghEvent.sender.login);
var action = ghEvent.action;
var issueNumber = ghEvent.issue.number;
var issueName = link(ghEvent.issue.html_url, ghEvent.issue.title + "\"");
if (action === "opened" | action === "closed") {
text = {
attachments: [{
"fallback": who + " opened Issue" + issueNumber + ": " + issueName,
"color": "#36a64f",
"pretext": "New issue " + action + ":",
"author_name": ghEvent.sender.login,
"author_link": ghEvent.sender.html_url,
"thumb_url": ghEvent.sender.avatar_url,
"title": "#" + issueNumber + ": " + ghEvent.issue.title,
"title_link": ghEvent.issue.html_url,
"text": ghEvent.issue.body,
"fields": [
{
"title": "Status",
"value": ghEvent.issue.state,
"short": true
},
{
"title": "Labels",
"value": ghEvent.issue.labels.map(label => label.name).join("\n"),
"short": true
}
],
"footer": "lib-ical",
"footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png",
"mrkdwn_in": ["text"]
}]
};
} else return;
}
// 'commit_comment':
// 'create':
// 'delete':
// 'issues':
// 'label':
// 'member':
// 'milestone':
// 'pull_request':
// 'pull_request_review':
// 'pull_request_review_comment':
// 'push':
// 'release':
var slack = new SlackWebhook('https://hooks.slack.com/services/SLACK-WEBHOOK-URL', {
defaults: {
username: 'GitHub -- user/project',
channel: '#CHANNEL-NAME',
icon_emoji: ':github:'
}
})
slack.send(text);
};
这远非完美,但它给出了一个非常好的结果:
对于这个特定的例子,它是一个问题关闭,但目前,该脚本也将工作在开放。该脚本还执行有限的标记处理,因此如果问题包含任何源块,则将在松弛区内正确地呈现它。
我希望这有助于你的方法,请允许我详细说明其他任何事情。
https://stackoverflow.com/questions/43080513
复制相似问题