FB-BotMill 旨在簡化 Facebook 內部機器人的開發、設計和運作流程。
它提供了一個語義 Java API,可以將其導入到您的 Java EE 專案中以發送和接收來自 Facebook 的訊息,以便開發人員可以專注於開發實際的應用程序,而不是處理 Facebook API 端點。
<dependency>
<groupId>co.aurasphere.botmill</groupId>
<artifactId>fb-botmill</artifactId>
<version>2.0.0-RC3</version>
</dependency>
搖籃
compile 'co.aurasphere.botmill:fb-botmill:2.0.0-RC3'
格羅維
@Grapes(
@Grab(group='co.aurasphere.botmill', module='fb-botmill', version='2.0.0-RC3')
)
其他導入方式,請造訪 Maven 中央倉庫站點
< servlet >
< servlet-name >myFbBot</ servlet-name >
< servlet-class >co.aurasphere.botmill.fb.FbBotMillServlet</ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name >myFbBot</ servlet-name >
< url-pattern >/myFbBot</ url-pattern >
</ servlet-mapping >
記下url 映射,因為這將用於 Facebook 中的 webhook 設定。
第一:設定頁面令牌和驗證令牌。在類別路徑中建立 botmill.properties 檔案並新增您的令牌。
fb.page.token =<PAGE_TOKEN>
fb.validation.token =<VALIDATION_TOKEN>
請注意,您可以使用我們內建的基於 jaspyt 的加密來加密屬性檔案。請造訪我們的 Wiki,以了解如何設定加密的botmill.properties檔案。
第二:設定您的加密類別。我們嚴格推動使用 Jaspyt 來加密令牌,為此,我們需要確保您建立自己的 Jaspyt 加密類別。為此,請在您的專案中建立以下內容。
@ BotEncryption
public class DefaultEncryption {
public DefaultEncryption () {
StandardPBEStringEncryptor enc = new StandardPBEStringEncryptor ();
enc . setPassword ( "password" ); // can be sourced out
ConfigurationUtils . loadEncryptedConfigurationFile ( enc , "botmill.properties" );
}
}
密碼由您決定,可以從任何地方取得(透過 https 或 ftp)。這裡的關鍵是 Jaspyt 將使用此文字來解密 botmill.properties 檔案。
...
enc . setPassword ( "https://mydomain.com/encryptionpassword/password.txt" ); // can be sourced out
..
完成此操作後,我們需要使用 botmill-crypto-util 專案來建立頁面令牌和驗證令牌的加密版本。下載 botmill-crypto-util [此處] (https://oss.sonatype.org/content/repositories/snapshots/co/aurasphere/botmill/botmill-crypto-util/0.0.1-SNAPSHOT/botmill-crypto-util -0.0.1-20170228.035750-1-jar-with-dependency.jar) 並執行下列指令:
java -jar botmill-crypto-util-0.0.1-20170228.035750-1-jar-with-dependency.jar enc <page_token> java -jar botmill-crypto-util-0.0.1-20170228.0botmill-crypto-util-0.0.1-20170228.0botmill-crypto-util-0.0.1-20170228.0botmill-depend-with .jar enc <驗證令牌>
這將輸出文字檔案的加密版本。使用這些值修改 botmill.properties,但請確保將其放入 ENC(***) 內
fb.page.token =ENC(<ENCRYPTED_PAGE_TOKEN>)
fb.validation.token =ENC(<ENCRYPTED_VALIDATION_TOKEN>)
重新部署就可以了。
步驟三:設定 BotConfiguration BotConfiguration 類別將處理需要發生的一次性進程(持久選單、facebook api 驗證等)。在下面建立一個 FbBotConfiguration 並將所有初始配置(一次性配置)放在建構函式上。這也將初始化 fb 身份驗證。
@ BotConfiguration
public class MyBotConfiguration extends FbBotConfiguration {
public MyBotConfiguration () {
MessengerProfileApi . setGetStartedButton ( "get_started" );
MessengerProfileApi . setGreetingMessage ( "Hello!" );
List < PersistentMenu > persistentMenus = new ArrayList < PersistentMenu >();
PersistentMenu persistentMenu = new PersistentMenu ( "default" , false );
persistentMenu . addCallToAction ( ButtonFactory . createPostbackButton ( "Menu 1" , "menu1" ));
persistentMenu . addCallToAction ( ButtonFactory . createPostbackButton ( "Menu 2" , "menu2" ));
CallToActionNested theNestedMenu = new CallToActionNested ( "Menu 3 Nested" );
theServices . addCallToActionButton ( ButtonFactory . createPostbackButton ( "Nested1" , "nested1" ));
theServices . addCallToActionButton ( ButtonFactory . createPostbackButton ( "Nested2" , "nested2" ));
theServices . addCallToActionButton ( ButtonFactory . createPostbackButton ( "Nested3" , "nested3" ));
persistentMenu . addCallToAction ( theNestedMenu );
persistentMenus . add ( persistentMenu );
MessengerProfileApi . setPersistentMenus ( persistentMenus );
HomeUrl homeUrl = new HomeUrl ();
homeUrl . setInTest ( true );
homeUrl . setUrl ( "https://extensionlink.co" );
homeUrl . setWebviewHeightRatio ( WebViewHeightRatioType . TALL );
homeUrl . setWebviewShareButton ( WebViewShareButton . SHOW );
MessengerProfileApi . setHomeUrl ( homeUrl );
}
}
第四:設定 FbBot 類。我們的框架透過將類別標記為行為對象,可以輕鬆、直接地定義 Facebook 機器人行為。
@ Bot
public class MyBotClass extends FbBot {
@ FbBotMillController ( eventType = FbBotMillEventType . MESSAGE , text = "Hi" , caseSensitive = true )
public void sendMessage ( MessageEnvelope envelope ) {
reply ( new MessageAutoReply ( "Hello World!" ));
}
}
@ Bot ( state = BotBeanState . PROTOTYPE ) // creates a new instance per call
public class MyBotClass1 extends FbBot {
@ FbBotMillController ( eventType = FbBotMillEventType . MESSAGE , text = "Hi" , caseSensitive = true )
public void sendMessage ( MessageEnvelope envelope ) {
reply ( new MessageAutoReply ( "Hello World on BotClass1" ));
}
}
@ Bot ( state = BotBeanState . SINGLETON ) // uses the same reference/instance (this is the default).
public class MyBotClass2 extends FbBot {
@ FbBotMillController ( eventType = FbBotMillEventType . MESSAGE , text = "Hi" , caseSensitive = true )
public void sendMessage ( MessageEnvelope envelope ) {
reply ( new MessageAutoReply ( "Hello World on BotClass2" ));
}
}
捕捉模式并快速回复
@ FbBotMillController ( eventType = FbBotMillEventType . MESSAGE_PATTERN , pattern = "(?i:hi)|(?i:hello)|(?i:hey)|(?i:good day)|(?i:home)" )
public void replyWithQuickReply ( MessageEnvelope envelope ) {
reply ( new AutoReply () {
@ Override
public FbBotMillResponse createResponse ( MessageEnvelope envelope ) {
return ReplyFactory . addTextMessageOnly ( "Text message with quick replies" )
. addQuickReply ( "Quick reply 1" , "Payload for quick reply 1" ). build ( envelope );
}
});
}
或透過按鈕響應
@ FbBotMillController ( eventType = FbBotMillEventType . MESSAGE_PATTERN , pattern = "(?i:hi)|(?i:hello)|(?i:hey)|(?i:good day)|(?i:home)" )
public void replyWithButtonTemplate ( MessageEnvelope envelope ) {
reply ( new AutoReply () {
@ Override
public FbBotMillResponse createResponse ( MessageEnvelope envelope ) {
return ReplyFactory . addButtonTemplate ( "Test button template" )
. addPostbackButton ( "postback button" , "postback button payload" )
. addPhoneNumberButton ( "phone number button" , "+123456789" )
. addUrlButton ( "web url button" , "https://github.com/BotMill/fb-botmill" ). build ( envelope );
}
});
}
請造訪我們的文件以取得事件類型和回應的完整清單。
建構 ChatBot 的關鍵組件
版權所有 (c) 2016-2017 BotMill.io