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.035750-1-jar-with-dependency .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