public static void SendSMS(TwilioConf twilioConfig, TickerSMSSettings ticker, Decimal current)
        {
            try
            {
                TwilioClient.Init(twilioConfig.Account, twilioConfig.Token);

                var message = MessageResource.Create(
                    body: ticker.TickerName + " - " + current + " - Thresholds high: " + ticker.High + " low: " + ticker.Low,
                    from: new Twilio.Types.PhoneNumber(twilioConfig.From),
                    to: new Twilio.Types.PhoneNumber(ticker.Mobile)
                    );

                Console.WriteLine(message.Sid);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            TwilioConf tw = new TwilioConf()
            {
                Token   = ConfigurationManager.AppSettings["SMSKeyToken"],
                From    = ConfigurationManager.AppSettings["SMSFrom"],
                Account = ConfigurationManager.AppSettings["SMSAccount"]
            };
            FinProvider fin = new FinProvider()
            {
                Name    = ConfigurationManager.AppSettings["FinProviderName"],
                Match   = ConfigurationManager.AppSettings["FinProviderMatch"],
                Replace = ConfigurationManager.AppSettings["FinProviderReplace"],
                Url     = ConfigurationManager.AppSettings["FinProviderURL"]
            };

            Console.WriteLine("Starting TickerUpdater cycle");
            TickerContext _db = new TickerContext(ConfigurationManager.ConnectionStrings["TickerSQLConnection"].ConnectionString);

            // Initialize the product database.
            Database.SetInitializer(new TickerDatabaseInit());
            while (true)
            {
                try
                {
                    foreach (Ticker ticker in _db.Tickers)
                    {
                        ticker.LastRead = DateTime.UtcNow;
                        var currRead = HTTPhandy.TickerFromAPI(fin, ticker.TickerName);
                        ticker.Current = currRead > 0 ? currRead: ticker.Current;
                        Console.WriteLine(ticker.TickerName + ": " + ticker.Current);
                    }
                    _db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                try
                {
                    var results = from t1 in _db.Tickers
                                  join t2 in _db.TickerSMSSettings on t1.TickerName equals t2.TickerName
                                  where (t1.Current <= t2.Low) || (t1.Current >= t2.High)
                                  select new
                    {
                        t1.TickerName,
                        t1.Current,
                        t2.Mobile,
                        t2.Low,
                        t2.High,
                        t1.LastRead
                    };
                    TickerSMSSettings found;
                    foreach (var smsSettings in results.ToList())
                    {
                        Console.WriteLine(smsSettings.TickerName + " " + smsSettings.Mobile + " " + smsSettings.Current);
                        found = _db.TickerSMSSettings.Where(s => s.Mobile == smsSettings.Mobile)
                                .FirstOrDefault <TickerSMSSettings>();
                        found.TickerDate = smsSettings.LastRead;
                        HTTPhandy.SendSMS(tw, found, smsSettings.Current);
                        _db.TickerSMSSettings.Remove(found);
                    }
                    _db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Thread.Sleep(5000);
            }
        }