1919package com .wire .bots .github ;
2020
2121import com .wire .bots .github .utils .SessionIdentifierGenerator ;
22- import com .wire .bots .sdk .Logger ;
2322import com .wire .bots .sdk .MessageHandlerBase ;
24- import com .wire .bots .sdk .Util ;
2523import com .wire .bots .sdk .WireClient ;
24+ import com .wire .bots .sdk .factories .StorageFactory ;
2625import com .wire .bots .sdk .models .TextMessage ;
2726import com .wire .bots .sdk .server .model .Member ;
2827import com .wire .bots .sdk .server .model .NewBot ;
2928import com .wire .bots .sdk .server .model .User ;
29+ import com .wire .bots .sdk .tools .Logger ;
30+ import com .wire .bots .sdk .tools .Util ;
3031
3132import javax .annotation .Nullable ;
32- import java .io .File ;
33- import java .io .IOException ;
3433import java .net .URLEncoder ;
3534import java .util .Collection ;
3635import java .util .Collections ;
3736import java .util .concurrent .TimeUnit ;
3837
3938public class MessageHandler extends MessageHandlerBase {
40- private final BotConfig config ;
39+ private static final String SECRET_FILE = "secret" ;
4140 private final SessionIdentifierGenerator sesGen = new SessionIdentifierGenerator ();
41+ private final StorageFactory storageFactory ;
4242
43- MessageHandler (BotConfig config ) {
44- this .config = config ;
43+ MessageHandler (StorageFactory storageFactory ) {
44+ this .storageFactory = storageFactory ;
4545 }
4646
4747 @ Override
@@ -65,7 +65,7 @@ public boolean onNewBot(NewBot newBot) {
6565 public void onNewConversation (WireClient client ) {
6666 try {
6767 String secret = sesGen .next (6 );
68- Util . writeLine ( secret , new File ( String . format ( "%s/%s/secret" , config . getCryptoDir (), client .getId ())) );
68+ storageFactory . create ( client .getId ()). saveFile ( SECRET_FILE , secret );
6969
7070 String help = formatHelp (client );
7171 client .sendText (help , TimeUnit .MINUTES .toMillis (15 ));
@@ -89,21 +89,21 @@ public void onText(WireClient client, TextMessage msg) {
8989 }
9090 }
9191
92- private String formatHelp (WireClient client ) throws IOException {
92+ private String formatHelp (WireClient client ) throws Exception {
9393 String botId = client .getId ();
9494 String host = getHost ();
95- String secret = Util . readLine ( new File ( String . format ( "%s/%s/secret" , config . getCryptoDir (), botId )) );
95+ String secret = storageFactory . create ( botId ). readFile ( SECRET_FILE );
9696 String name = client .getConversation ().name ;
9797 String convName = name != null ? URLEncoder .encode (name , "UTF-8" ) : "" ;
98- String owner = getOwner (client , botId );
98+ String owner = getOwner (client );
9999
100100 String url = String .format ("https://%s/%s#conv=%s,owner=@%s" , host , botId , convName , owner );
101101 return formatHelp (url , secret );
102102 }
103103
104104 private String formatHelp (String url , String secret ) {
105105 return String .format ("Hi, I'm GitHub bot. Here is how to set me up:\n \n "
106- + "1. Go to the repository that you want to connect to\n "
106+ + "1. Go to the repository that you would like to connect to\n "
107107 + "2. Go to **Settings / Webhooks / Add webhook**\n "
108108 + "3. Add **Payload URL**: %s\n "
109109 + "4. Set **Content-Type**: application/json\n "
@@ -117,12 +117,10 @@ private String getHost() {
117117 }
118118
119119 @ Nullable
120- private String getOwner (WireClient client , String botId ) throws IOException {
121- File originFile = new File (String .format ("%s/%s/origin.id" , config .getCryptoDir (), botId ));
122- if (!originFile .exists ())
123- return null ;
124- String origin = Util .readLine (originFile );
125- Collection <User > users = client .getUsers (Collections .singletonList (origin ));
120+ private String getOwner (WireClient client ) throws Exception {
121+ String botId = client .getId ();
122+ NewBot state = storageFactory .create (botId ).getState ();
123+ Collection <User > users = client .getUsers (Collections .singletonList (state .origin .id ));
126124 for (User user : users )
127125 return user .handle ;
128126 return null ;
0 commit comments