LennyK LennyK - 7 months ago
2697 0

Written with C# Library Telegram.Bot

C#

Telegram Bot Example

using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Http;
using Telegram.Bot.Types;
using File = System.IO.File;

namespace MetarInfoBot
{
    static class Program
    {
        private static void Main(string[] args)
        {
            Run().Wait();
        }

        private static async Task Run()
        {
            var Bot = new Telegram.Bot.Api("YOUR_BOT_API");
            var me = Bot.GetMe().Result;
            Console.WriteLine("My name is " + me.FirstName);

            var offset = 0;

            while (true)
            {
                var updates = await Bot.GetUpdates(offset);
                var metartext = "";
                foreach (var update in updates)
                {
                    if (update.Message.Type == MessageType.TextMessage && update.Message.Text.StartsWith("/metar"))
                    {
                        var station = update.Message.Text.Substring(update.Message.Text.Length -4);
                        await Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
                        await Task.Delay(2000);


                        
                        try
                        {
                            var url = "ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/" + station + ".TXT";
                            var ftprequest = (FtpWebRequest)WebRequest.Create(url);
                            ftprequest.Credentials = new NetworkCredential("anonymous", "anonymous@example.com");
                            ftprequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;

                            try
                            {
                                FtpWebResponse response = (FtpWebResponse)ftprequest.GetResponse();
                                var request = new WebClient();
                                var newFileData = request.DownloadData(url);
                                var fileString = System.Text.Encoding.UTF8.GetString(newFileData);
                                metartext = fileString;
                            }
                            catch (WebException ex)
                            {
                                FtpWebResponse response = (FtpWebResponse)ex.Response;
                                if (response.StatusCode ==
                                    FtpStatusCode.ActionNotTakenFileUnavailable)
                                {
                                    metartext = "METAR for this Airport was not found.";
                                }
                            }

                            
                        }
                        catch (Exception e)
                        {
                            metartext = "A server error occured. Please try again later. Message:" + e.Message;
                        }

                        
                        var t = await Bot.SendTextMessage(update.Message.Chat.Id, metartext);
                    }

                    offset = update.Id + 1;
                }
                await Task.Delay(1000);
            }
        }
    }
}
Comments