Beispiel #1
0
 public void Stop()
 {
     // if (tw != null) tw.Stop();
     TwilioClient.Invalidate();
     active    = false;
     setBreak  = true;
     subStatus = "Loop stopped";
 }
Beispiel #2
0
        public IList <string> SendSms(string text, Dictionary <string, SmsAuthority> receivers)
        {
            var errors = new List <string>();

            foreach (var receiver in receivers)
            {
                try
                {
                    TwilioClient.Init(receiver.Value.AccountSid, receiver.Value.Authtoken);
                    var message = MessageResource.Create(
                        body: text,
                        from: new Twilio.Types.PhoneNumber(receiver.Value.MainPhone),
                        to: new Twilio.Types.PhoneNumber(receiver.Key));

                    TwilioClient.Invalidate();
                }
                catch (Exception e)
                {
                    errors.Add($"Can't send sms to {receiver.Key}. {e.Message}");
                }
            }

            return(errors);
        }
Beispiel #3
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
            HttpRequest req, ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var data = await req.ReadAsStringAsync();

            var formValues = data.Split('&')
                             .Select(value => value.Split('='))
                             .ToDictionary(pair => Uri.UnescapeDataString(pair[0]).Replace("+", " "),
                                           pair => Uri.UnescapeDataString(pair[1]).Replace("+", " "));

            string senderNumber = "+" + formValues["From"].Substring(1);

            if (formValues["Body"].Trim().ToLower() != "i won")
            {
                var error = new MessagingResponse()
                            .Message($"DON'T @ ME!!  You sent {formValues["Body"]}.  I only respect 'I won' (no quotes)");
                var errorTwiml = error.ToString();
                errorTwiml = errorTwiml.Replace("utf-16", "utf-8");

                return(new HttpResponseMessage
                {
                    Content = new StringContent(errorTwiml.ToString(), Encoding.UTF8, "application/xml")
                });
            }

            string accountId = Environment.GetEnvironmentVariable("accountId");
            string authToken = Environment.GetEnvironmentVariable("authToken");
            var    myNumber  = new PhoneNumber(Environment.GetEnvironmentVariable("myNumber"));

            // lookup the sender

            string tourneyId  = Environment.GetEnvironmentVariable("ChallongeTournamentId");
            string apiKey     = Environment.GetEnvironmentVariable("ChallongeApiKey");
            var    tourneyDal = new TourneyDal(apiKey, tourneyId);
            var    mongoDal   = new MongoDal();

            Participant senderParticipant = await mongoDal.GetParticipantByPhoneNumberAsync(senderNumber);

            // get the sender's game
            JArray openGamesForSender = await tourneyDal.GetOpenMatchesAsync(senderParticipant.ChallongeId);

            var senderGame = (JObject)openGamesForSender.Single();

            string loserId;
            string orderedScore;

            if (senderGame["match"]["player1_id"].Value <string>() == senderParticipant.ChallongeId)
            {
                loserId      = senderGame["match"]["player2_id"].Value <string>();
                orderedScore = "2-0";
            }
            else
            {
                loserId      = senderGame["match"]["player1_id"].Value <string>();
                orderedScore = "0-2";
            }

            // get the loser to send them a message
            Participant loserParticipant = await mongoDal.GetParticipantByChallongeIdAsync(loserId);

            // assume sender won and update that game
            await tourneyDal.UpdateWinnerAsync(
                senderGame["match"]["id"].Value <string>(),
                senderParticipant.ChallongeId,
                orderedScore);

            // get matches that have been notified before
            Matches matches = await mongoDal.GetMatchesAsync();

            var notifiedMatchIds = new HashSet <string> (matches.MatchIds, StringComparer.OrdinalIgnoreCase);

            JObject nextMatch = await GetNextMatchToPlayAsync(tourneyDal, notifiedMatchIds);

            Participant player1 = await mongoDal.GetParticipantByChallongeIdAsync(
                nextMatch["match"]["player1_id"].Value <string>());

            Participant player2 = await mongoDal.GetParticipantByChallongeIdAsync(
                nextMatch["match"]["player2_id"].Value <string>());

            // text next players
            var nextPlayer1 = new PhoneNumber(player1.PhoneNumber);
            var nextPlayer2 = new PhoneNumber(player2.PhoneNumber);

            TwilioClient.Init(accountId, authToken);

            var congrats = MessageResource.Create(
                body: $"Congrats on winning! :D",
                from: myNumber,
                to: new PhoneNumber(senderNumber));

            var sorry = MessageResource.Create(
                body: $"Tough match :/",
                from: myNumber,
                to: new PhoneNumber(loserParticipant.PhoneNumber));

            var p2notification = MessageResource.Create(
                body: $"You're up now against {player1.Name}. Please go find the open TV.",
                from: myNumber,
                to: nextPlayer2);

            var p1notification = MessageResource.Create(
                body: $"You're up now against {player2.Name}. Please go find the open TV.",
                from: myNumber,
                to: nextPlayer1);

            TwilioClient.Invalidate();

            // update database
            await mongoDal.UpdateMatchListAsync(matches, nextMatch["match"]["id"].Value <string>());

            return(new HttpResponseMessage
            {
            });
        }