Read Public Telegram Channel Latest Messages

To add comments, please log in or register
Lorentzos Roussos
18810
Lorentzos Roussos  

Read public Channel  telegram messages 

<!> Havent tested constant refreshing of messages.I wonder if this will be blocked in scale -by telegram.

<!> add the adress https://t.me in webrequest allowed urls 


#property copyright "Copyright 2019"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   //EventSetTimer(60);
   ClearMem();
   int read=ReadPublicChannelScrap("https://t.me/LorioIFTTMT4Test","https://t.me/s/LorioIFTTMT4Test");
   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 : "+MSG[r].valid_time);
    Print("Readable Time : "+TimeToString(MSG[r].time,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
    }
   }

//---
   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);
          span_start=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)+1;
      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);
      //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
          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);   
  //Print("original : "+original);
  //Print("date part : "+date_part);
  //Print("time part : "+time_part);
  //Print("offs part : "+offs_part);
  //DATE PART : YYYY-MM-DD
  //TIME PART : HH:MM:SS
  //OFFSET PART : HH:MM
  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 
To add comments, please log in or register