How do I compare a timestamp from a telegram message to the current time

 

I've found a code here on the forums using webrequest to read messages on a telegram public channel.

It works great but I am wanting to only print the last message posted.

So I need a check added that will check the data from the webrequest and store the date/time in a variable maybe?

Then the next time the webrequest is executed check the data for the date/time and if it matches the last skip that message so that it will only print the last message posted and skip all the others.

I'm pretty new to coding in mql4 so I don't know where to even begin on this. The actual MSG_Add function looks pretty complicated

so I am not sure how to parse the time from that or from the ReadPublicChannelScrap function.

Since each part of the message is being pushed to the MSG_Add function I am not sure where I should interrupt it and how to stop the rest of the code from executing if it is found true.


//--- input parameters
input string   TelegramChannel = "YOUR_TELEGRAM_CHANNEL";

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    //--- create timer
    //EventSetTimer(20000);
    ClearMem();
    
    int read = ReadPublicChannelScrap("https://t.me/" + TelegramChannel, "https://t.me/s/" + TelegramChannel);
    if(read > 0)
    {
        for(int r = 0; r < MSG_TOTAL; r++)
        {
            Print("----");
            Print("Author : " + MSG[r].author);
            Print("Message : " + MSG[r].message_body);
            Print("Date : " + string(MSG[r].time_string));
            Print("Valid Time : " + string(MSG[r].valid_time));
            Print("Readable Time : " + TimeToString(MSG[r].time, TIME_DATE|TIME_MINUTES|TIME_SECONDS));
            // Readable Time : 2023.05.07 04:52:33
            //Print(body_message_parsed);
        }
    }

    //---
    return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    //--- destroy timer
    // EventKillTimer();
    ClearMem();

}

struct telegram_channel_message_props
{
    string author;
    string time_string;
    datetime time;
    string message_body;
    bool valid_time;
};

telegram_channel_message_props MSG[];

int MSG_TOTAL = 0, MSG_SIZE = 0, MSG_STEP = 100;
/*
   READ PUBLIC CHANNEL
   WITHOUT TELEGRAM API 
   WITHOUT TELEGRAM BOT
   THIS DEPENDS ON THE BROWSER CHANNEL PREVIEW
   IF CHANNEL IS NOT PUBLIC IT WONT WORK (but its untested)
   IT WILL READ THE LATEST AVAILABLE MESSAGES UPON LOAD (there must be a link for loading more ... maybe will be developed in the future)
   THE REFERRER MUST BE THE ORIGINAL CHANNEL LINK (although automation can be introduced with just the telegram link)
   <!> add the adress https://t.me in webrequest allowed urls 
*/
int ReadPublicChannelScrap(string original_channel_link, string preview_channel_link)
{
    int read = 0;
    string params[];
    string type="Content-Type: application/x-www-form-urlencoded";
    string user_agent="user-agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 OPR/65.0.3467.62";
    string html = WR_REQUEST(wr_get, preview_channel_link, params, type, user_agent, NULL, original_channel_link, 6000);
    
    //process response 
    MSG_TOTAL = 0;
    
    string message_body_class="tgme_widget_message_text js-message_text";
    string message_time_class="<time";
    string message_author_class="tgme_widget_message_author";
    
    int author_skip = StringLen(message_author_class);
    int body_skip = StringLen(message_body_class);
    int time_skip = StringLen(message_time_class);
    
    //scan process
    bool more = true;
    int location = 0;
    bool extract = false;
    
    telegram_channel_message_props candidate;
    
    while(more)
    {
        //find author - meaning a message ensues
        int fi_author = StringFind(html, message_author_class, location);
        if(fi_author == -1) more = false;
        if(fi_author != -1)
        {
            //find author 
            //</span end marks end of author name 
            fi_author += author_skip;
            
            int span_start = StringFind(html, "<span", fi_author);
            int span_start2 = StringFind(html, ">", span_start + 5) + 1;
            int span_end = StringFind(html, "</span>", fi_author) - 1;
            int span_length = span_end - span_start + 1;
    
            //get author
            candidate.author = StringSubstr(html, span_start, span_length);
        
            //find message body 
            int fi_body = StringFind(html, message_body_class, span_end + 6);
            if(fi_body == -1) more = false;
            if(fi_body != -1)
            {
                fi_body += body_skip;
                span_start = StringFind(html, "')\">", fi_body) + 4;
                span_end = StringFind(html, "</div>", span_start) - 1;
                span_length = span_end - span_start + 1;
                
                //get body
                candidate.message_body = StringSubstr(html, span_start, span_length);
                //body_message_parsed = string(candidate.message_body);

                //find message time 
                int fi_time = StringFind(html, message_time_class, span_end);
                if(fi_time == -1) more = false;
                if(fi_time != -1)
                {
                    fi_time += time_skip;
                    span_start = StringFind(html, "datetime=\"", fi_time);
                    span_start += StringLen("datetime=\"");
                    span_end = StringFind(html, "\"", span_start) - 1;
                    span_length = span_end-span_start +1;
                    candidate.time_string = StringSubstr(html, span_start, span_length);
                    //add
                    Print("TimeString: " +  candidate.time_string);
                    //if(candidate.time_string = 
                    read = MSG_Add(candidate);
                    location = span_end;
                    //add ends here
                }
                //find message time ends here 
            }
            //find message body ends here 
        }
    //find author - meaning a message ensues ends here 
    }
    //scan process ends here 
    //process response ends here 
    return(read);
}

int MSG_Add(telegram_channel_message_props & candidate)
{
    MSG_TOTAL++;
    if(MSG_TOTAL > MSG_SIZE)
    {
        MSG_SIZE = MSG_SIZE + MSG_STEP;
        ArrayResize(MSG, MSG_SIZE, 0);
    }
    MSG[MSG_TOTAL - 1] = candidate;
    MSG[MSG_TOTAL - 1].valid_time = false;
    
    //transform to readable time 
    string original = candidate.time_string;
    string scrapper[];
    
    //date portion 
    int fi_t = StringFind(original, "T", 0);
    int fi_p = StringFind(original, "+", 0);
    int added_prefix = 1;//+1 -1
    
    if(fi_p == -1)
    {
        fi_p = StringFind(original, "-", fi_t);
        added_prefix = -1;
    }
    
    int original_len = StringLen(original);
    string date_part = StringSubstr(original, 0, fi_t);
    string time_part = StringSubstr(original, fi_t + 1, fi_p - fi_t - 1);
    string offs_part = StringSubstr(original, fi_p + 1, 5);   
    MqlDateTime Tim;
    ushort usep = StringGetCharacter("-", 0);
    int k = StringSplit(date_part, usep, scrapper);
    
    if(k == 3)
    {
        Tim.year = (int)StringToInteger(scrapper[0]);
        Tim.mon = (int)StringToInteger(scrapper[1]);
        Tim.day = (int)StringToInteger(scrapper[2]);
        ushort dsep = StringGetCharacter(":", 0);
        int g = StringSplit(time_part, dsep, scrapper);
        //time split
        if(g == 3)
        {
            Tim.hour = (int)StringToInteger(scrapper[0]);
            Tim.min = (int)StringToInteger(scrapper[1]);
            Tim.sec = (int)StringToInteger(scrapper[2]);
            //offset split
            int o = StringSplit(offs_part, dsep, scrapper);
            if(o == 2)
            {
                int o_hours = (int)StringToInteger(scrapper[0]);
                int o_mins = (int)StringToInteger(scrapper[1]);
                datetime timer = StructToTime(Tim);           
                MSG[MSG_TOTAL - 1].time = timer;
                MSG[MSG_TOTAL - 1].valid_time = true;
            }
            //offset split ends here 
        }
        //time split 
    }
    ArrayFree(scrapper);
    //transform to readable time ends here
return(MSG_TOTAL);
}
//clear memory 
void ClearMem()
{
    ArrayFree(MSG);
    MSG_TOTAL = 0;
    MSG_SIZE = 0;
}

//REQUEST CODE 
enum wr_type
{
    wr_get = 0,//GET
    wr_post = 1//POST
};

string WR_REQUEST(wr_type request_type, string url, string &params[], string type, string user_agent, string cookie, string referer, uint timeout)
{
    string response = "not sent";
    string headers = "";
    
    /*
    for headers , in type string include header descriptor (e.g. Content-Type:)
                  in user agent string include user agent descriptor (e.g. user-agent:)
    */
    if(type != NULL) headers = type;
    if(user_agent != NULL) headers = headers + "\r\n" + user_agent;
    char post[], result[];
    int res;
    string target_url = url;
    string specs = "";
    
    //fill specs if they exist 
    int params_total = ArraySize(params);
    bool noparams = false;
    if(params_total == 1 && params[0] == "") noparams = true;
    if(noparams == false)
    {
        for(int fp = 0; fp < params_total; fp++)
        {
            specs = specs + params[fp];
            if(fp < params_total - 1) specs = specs + "&";
        }
    }
    
    if(request_type == wr_get && noparams == false) target_url = target_url + "?" + specs;
    char data[];
    int data_size = 0;
    int slen = StringLen(specs);
    
    if(request_type == wr_post) data_size = StringToCharArray(specs, data, 0, slen, CP_UTF8);
    ResetLastError();
    string req_string = "GET";
    
    if(request_type == wr_post) req_string = "POST";
    res=WebRequest(req_string, target_url, cookie, referer, timeout, data, data_size, result, headers);
    
    if(res == -1)
    {
        Print("Error in WebRequest. Error code  =",GetLastError());
        MessageBox("Add the address '"+url+"' in the list of allowed URLs on tab 'Expert Advisors'", "Error", MB_ICONINFORMATION);
    }
        else
    {
        //PrintFormat("The file has been successfully loaded, File size =%d bytes.",ArraySize(result));
        int tit = ArraySize(result)-1;
        string html = "";
        for(int xx = 0; xx <= tit; xx++)
        {
            html = html + CharToStr(result[xx]);
        }
        response = html;
    }
    return(response);
    ArrayFree(result);
}
//REQUEST CODE ENDS HERE 

I honestly do not know how to title this question. If there is a better title I should use for this thread let me know and I'll try to change it. Thanks.

 
  • Usually people who cannot code do not receive free help on this forum, although it could happen if you are lucky. Be patient.
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community.
  • If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free).
  • Finally, you also have the option to hire a programmer in the Freelance section.