Esempio n. 1
0
        public static void Main(string[] args)
        {
            AppConfig appConfig;

            try
            {
                appConfig = JsonConvert.DeserializeObject <AppConfig>(File.ReadAllText(APP_CONFIG_JSON_PATH));
                Console.WriteLine($"Config loaded from file: {APP_CONFIG_JSON_PATH}");
            }
            catch (FileNotFoundException)
            {
                appConfig = (AppConfig)AppConfig.DefaultConfig.Clone();
                Console.WriteLine("No config file found, using defaults");
                File.WriteAllText(APP_CONFIG_JSON_PATH, JsonConvert.SerializeObject(appConfig, Formatting.Indented));
                Console.WriteLine($"Saved config to file: {APP_CONFIG_JSON_PATH}");
            }
            catch
            {
                throw;
            }

            Console.WriteLine($"Opening connection to MatrixRpcJs @ {appConfig.MatrixRpcJsEndpoint}");
            Channel c = new Channel(appConfig.MatrixRpcJsEndpoint, ChannelCredentials.Insecure);

            if (!c.ConnectAsync().Wait(CONNECT_TIMEOUT))
            {
                throw new Exception($"MatrixRpcJs connection @ {appConfig.MatrixRpcJsEndpoint} timed out");
            }

            // TODO: Detect MatrixRpcJs disconnection at runtime, currently it just hangs...
            Matrix.Matrix.MatrixClient matrixClient = new Matrix.Matrix.MatrixClient(c);

            AuthParams authParams;

            try
            {
                authParams = JsonConvert.DeserializeObject <AuthParams>(File.ReadAllText(AUTH_PARAMS_JSON_PATH));
                Console.WriteLine($"Existing auth params loaded from file: {AUTH_PARAMS_JSON_PATH}");
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine($"Auth params file not found ('{AUTH_PARAMS_JSON_PATH}')");
                Console.WriteLine($"Authenticating...");
                Console.Write("username: "******"password: "******"Saved auth params to file: {AUTH_PARAMS_JSON_PATH}");
            }
            catch
            {
                throw;
            }

            var session = matrixClient.StartSession(new StartSessionRequest()
            {
                Homeserver      = appConfig.Homeserver,
                AccessToken     = authParams.AccessToken,
                UserId          = authParams.UserId,
                DeviceId        = authParams.DeviceId,
                DisplayName     = appConfig.DisplayName,
                InitialPresence = appConfig.InitialPresence,
            });

            var bot     = new Bot(matrixClient, session, authParams.UserId);
            var botTask = bot.HandleEvents();

            var con     = new BotConsole(bot);
            var conTask = con.Serve();

            Task.WaitAll(botTask, conTask);

            Console.WriteLine("Quitting. Waiting for connection to close...");
            c.ShutdownAsync().Wait();
            Console.WriteLine("Bye!");
        }
Esempio n. 2
0
 public Bot(Matrix.Matrix.MatrixClient matrixClient, AsyncServerStreamingCall <SessionEvent> session, string userId)
 {
     this.matrixClient = matrixClient;
     this.session      = session;
     this.userId       = userId;
 }