Exemplo n.º 1
0
        public override TelegramApi.MethodSendMessage HandleStart()
        {
            var sendMsg = new TelegramApi.MethodSendMessage();

            sendMsg.text = "What are your coordinates?\nPlease provide your latitude and longitude.\nFor example: 45.67 32.312";
            state        = State.waitPos;
            return(sendMsg);
        }
Exemplo n.º 2
0
 public void SendPlainMessage(HttpClient httpClient, long chatId, string text, bool disablePreview = false)
 {
     TelegramApi.MethodSendMessage sendMsg = new TelegramApi.MethodSendMessage();
     sendMsg.chat_id = chatId;
     sendMsg.text    = text;
     sendMsg.disable_web_page_preview = disablePreview;
     this.PostJsonSync(httpClient, "sendMessage", sendMsg);
 }
Exemplo n.º 3
0
        public override TelegramApi.MethodSendMessage HandleMessage(string msg)
        {
            switch (state)
            {
            case State.waitPos: {
                string reply = null;
                var    parts = msg.Split(' ');
                try {
                    this.lat = Convert.ToDouble(parts[0]);
                    this.lon = Convert.ToDouble(parts[1]);
                    if (lat <= -90.0 || lat > 90.0 || lon <= -180.0 || lon >= 180.0)
                    {
                        throw new Exception("Coordinates are out of bounds");
                    }
                    reply      = $"What is your time zone?\nFor example: 3.";
                    this.state = State.waitTimeZone;
                } catch (Exception e) {
                    Logger.LogLine($"Error parsing coordinates: {e}");
                    reply = "Provided coordinates are incorrect. Please try again.";
                }
                var sendMsg = new TelegramApi.MethodSendMessage();
                sendMsg.text = reply;
                return(sendMsg);

                break;
            }

            case State.waitTimeZone: {
                string reply = null;
                try {
                    this.timeZone = Convert.ToInt32(msg);
                    if (timeZone < -12 || timeZone > 12)
                    {
                        throw new Exception("Time zone is out of bounds");
                    }
                    reply      = $"What is the local time when you want to receive your forecasts?\nFor example: 9:30.";
                    this.state = State.waitAlertTime;
                } catch (Exception e) {
                    Logger.LogLine($"Error parsing time zone: {timeZone}");
                    Logger.LogLine($"{e}");
                    reply = "Provided time zone is incorrect. Please try again.";
                }
                var sendMsg = new TelegramApi.MethodSendMessage();
                sendMsg.text = reply;
                return(sendMsg);

                break;
            }

            case State.waitAlertTime: {
                string reply = null;
                var    parts = msg.Split(':', ' ');
                try {
                    this.alertHour   = Convert.ToUInt32(parts[0]);
                    this.alertMinute = Convert.ToUInt32(parts[1]);
                    if (alertHour >= 24 || alertMinute >= 60)
                    {
                        throw new Exception("Time format is incorrect");
                    }
                    Subscriber sub = new Subscriber(
                        this.userId,
                        this.chatId,
                        this.lat, this.lon,
                        this.alertHour, this.alertMinute,
                        this.timeZone);
                    Program.AddSubscriber(sub);
                    TimeSpan      timeUntilAlert = sub.TimeUntilAlert();
                    int           hours          = timeUntilAlert.Hours;
                    int           minutes        = timeUntilAlert.Minutes;
                    StringBuilder sb             = new StringBuilder(128);
                    sb.AppendLine($"Your coordinates are: {lat} {lon}");
                    sb.AppendLine($"Your time zone is: {timeZone}");
                    sb.AppendLine($"You will receive daily updates at {this.alertHour}:{this.alertMinute}");
                    sb.AppendLine($"Your next update will be in {hours} hours {minutes} minutes");
                    sb.AppendLine($"Have a nice day!");
                    reply = sb.ToString();
                    this.EndDialogue();
                } catch (Exception e) {
                    Logger.LogLine($"Error parsing time: {e}");
                    reply = "Provided time is incorrect. Please try again.";
                }
                var sendMsg = new TelegramApi.MethodSendMessage();
                sendMsg.text = reply;
                return(sendMsg);

                break;
            }
            }

            return(null);
        }