Skip to content

Instantly share code, notes, and snippets.

@na0AaooQ
Last active December 18, 2018 04:27
Show Gist options
  • Save na0AaooQ/190ebdc917b1905213e28f672a3d256b to your computer and use it in GitHub Desktop.
Save na0AaooQ/190ebdc917b1905213e28f672a3d256b to your computer and use it in GitHub Desktop.
Salesforce Visualforce + Apexで入力フォーム作成 (SalesforceからSlackへメッセージを通知する) ref: https://qiita.com/na0AaooQ/items/4d0d17c65b500b5661cd
xoxp-4********8-4********0-5**********9-2******************************b
$ curl -X POST -d "token=xoxp-4********8-4********0-5**********9-2******************************b" -d "channel=general" -d "text=test-messsages" https://slack.com/api/chat.postMessage
$ curl -X POST -d "token=xoxp-4********8-4********0-5**********9-2******************************b" -d "channel=general" -d "text=test-messsages" https://slack.com/api/chat.postMessage
{"ok":true,"channel":"C********","ts":"1545023860.000900","message":{"type":"message","subtype":"bot_message","text":"test-messsages","ts":"1545023860.000900","username":"Example Salesforce Slack Info","bot_id":"B********3"}}
////////////////////////////////////////
// Visualforceで入力されたメッセージ内容やタイトル等をChatworkやメール配信するApexクラス
////////////////////////////////////////
public with sharing class PostMessage {
// Visualforceページのテキストエリアで入力されたChatworkへ投稿するメッセージ本文を取得
public String chatwork_message { get; set; }
// Visualforceページのテキストボックスで入力されたChatworkへ投稿するメッセージタイトルを取得
public String chatwork_title { get; set; }
// Visualforceページのテキストボックスで入力されたメッセージ投稿先のChatworkルーム番号を取得
public String chatwork_post_room { get; set; }
// Visualforceページのテキストエリアで入力されたSlackへ投稿するメッセージ本文を取得
public String slack_message { get; set; }
// Visualforceページのテキストボックスで入力されたメッセージ投稿先のSlackチャネル名を取得
public String slack_channel { get; set; }
// Visualforceページのテキストボックスで入力されたメール送信先アドレスを取得
public String to_mail_address { get; set; }
public void post_message() {
PostChatworkMessage post_chatwork = new PostChatworkMessage();
PostSlackMessage post_slack = new PostSlackMessage();
SendMailMessage send_mail_message = new SendMailMessage();
String mail_subject = chatwork_title;
String mail_message = chatwork_message;
String slack_message = chatwork_message;
// Visualforceページで入力されたメッセージをChatworkへ投稿する
post_chatwork.post_chatwork_message( chatwork_title, chatwork_message, chatwork_post_room );
// Visualforceページで入力されたメッセージをSlackeへ投稿する
post_slack.post_slack_message( slack_message, slack_channel );
// Visualforceページで入力されたメッセージをメール送信する
send_mail_message.send_mail_message( mail_subject, mail_message, to_mail_address );
}
}
<apex:page controller="PostMessage">
<apex:form >
<apex:pageBlock title="PostMessageForm">
<apex:pageMessages />
<table class="chatwork_messages">
<tr>
<th>
Chatworkへ投稿するメッセージタイトル 及び 送信するメール件名を入力
</th>
<td>
<apex:pageBlockSection columns="1">
<apex:inputText id="chatwork_title" value="{!chatwork_title}" required="true" />
</apex:pageBlockSection>
</td>
</tr>
<tr>
<th>
Chatworkへ投稿するメッセージ 及び メール本文を入力
</th>
<td>
<apex:pageBlockSection columns="1">
<apex:inputTextarea id="chatwork_meesage" value="{!chatwork_message}" required="true" />
</apex:pageBlockSection>
</td>
</tr>
<tr>
<th>
Chatworkへメッセージを投稿する時の投稿先ルーム番号を入力
</th>
<td>
<apex:pageBlockSection columns="1">
<apex:inputText id="chatwork_post_room" value="{!chatwork_post_room}" required="true" />
</apex:pageBlockSection>
</td>
</tr>
<tr>
<th>
Slackへメッセージを投稿する時のSlackチャネル名を入力
</th>
<td>
<apex:pageBlockSection columns="1">
<apex:inputText id="slack_channel" value="{!slack_channel}" required="true" />
</apex:pageBlockSection>
</td>
</tr>
<tr>
<th>
メッセージ送信先のメールアドレスを入力
</th>
<td>
<apex:pageBlockSection columns="1">
<apex:inputText id="to_mail_address" value="{!to_mail_address}" required="true" />
</apex:pageBlockSection>
</td>
</tr>
</table>
<apex:commandButton action="{!post_message}" value=" Chatwork+Slack+メールでメッセージ送信 "/>
</apex:pageBlock>
</apex:form>
</apex:page>
////////////////////////////////////////
// Slackにメッセージを投稿するApex
////////////////////////////////////////
public with sharing class PostSlackMessage {
// カスタム設定からSlack APIトークン情報等を取得
private static String SlackApiEndPointUrl;
private static String SlackApiToken;
private static String SlackApiUri;
static {
SlackApiSetting__c setting = SlackApiSetting__c.getInstance();
SlackApiEndPointUrl = setting.SlackApiEndPoinUrL__c;
SlackApiToken = setting.SlackApiToken__c;
SlackApiUri = setting.SlackApiUriPostMessage__c;
}
public void post_slack_message(String slack_message, String slack_channel) {
String result_message = '';
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(SlackApiEndPointUrl + '/' + SlackApiUri);
request.setMethod('POST');
request.setBody('token=' + SlackApiToken + '&' + 'channel=' + slack_channel + '&' + 'text=' + slack_message);
HttpResponse response = http.send(request);
Integer status = response.getStatusCode();
if ( status == 200 ) {
result_message = response.getBody();
} else {
result_message = status.format() + response.getBody();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment