Discussion of article "How to create bots for Telegram in MQL5" - page 6

 

The errors are very clear and exactly what they state.

The compiler warns you that it is unable to find the include file.

The include file holds the missing functions which is why additional errors are generated.

You have to make sure that the file telegram.mqh exists in the files folder.

Start there as there are more errors which seem to be related to other code issues.
 

Is it possible to make it from custom indicator instead of expert advisor?

 
Franky Frentiono Nangoy:

Is it possible to make it from custom indicator instead of expert advisor?

If you try to call the function WebRequest from an indicator, GetLastError() will return error 4014 — "Function is not allowed for call".

To do this, you need to remake the project to work with Wininet.dll from indicators.

Documentation on MQL5: Common Functions / WebRequest
Documentation on MQL5: Common Functions / WebRequest
  • www.mql5.com
Common Functions / WebRequest - Reference on algorithmic/automated trading language for MetaTrader 5
 
Andrey Voytenko:

If you try to call the function WebRequest from an indicator, GetLastError() will return error 4014 — "Function is not allowed for call".

To do this, you need to remake the project to work with Wininet.dll from indicators.


thank you, will take a look at it

 

look in the

<Telegram.mqh>
{ 
   "ok":true,
   "result":[ 
      { 
         "update_id":349778698,
         "message":{ 
            "message_id":2,
            "from":{ 
               "id":198289825,
               "first_name":"Andriy",
               "last_name":"Voitenko",
               "username":"avaticks"
            },
            "chat":{ 
               "id":198289825,
               "first_name":"Andriy",
               "last_name":"Voitenko",
               "username":"avaticks",
               "type":"private"
            },
            "date":1459775817,
            "text":"\/start"
         }
      }
   ]
}
 

Aimak:

I would appreciate if you could help me finding the error. Thanks in advance.

Switch your channel to public mode. Or using chatID (not channel name) for communication with your private channel.

 
Aimak:

The channel is public and I getting the same result using channel name or ID. See screenshot

Do you use the latest release of Telegram.mqh and Jason.mqh?

 

Dear Andrey, 

thanks a lot for your work. I've installed your files and configured an expert that does the following:

1) the expert sends messages to the bot every x minutes (just for test) ,

2) i read the message on telegram and click "open trade" on the keyboard of telegram client,

3) the metatrader opens the trade (if possible).

Everything works fine but after a while the bot is not anymore reachable; to make it work again i have to wake up him by pressing some command on the keyboard menu (for example charts or /start or account info....). The metatrader says "bad request:chat not found" but if I wake up the bot again it works perfectly for one hour or more but then it stops.

I've created the bot as you have suggested (with botFather). How can I solve this issue?

Thanks again

Files:
error.gif  26 kb
 

Thanks for the interesting article. There is an error in the code (you can see it on the screenshot):

Error when sending a photo, title

Need fixes in SendPhoto method of CCustomBot class. I suggest this option:

   int SendPhoto(const string _channel_name,
                 const string _local_path,
                 string &_photo_id,
                 const string _caption=NULL,
                 const bool _common_flag=false,
                 const int _timeout=10000)
     {
      //---
      // const long _chat_id
      //---
      
      if(m_token==NULL)
         return(ERR_TOKEN_ISEMPTY);

      string name=StringTrim(_channel_name);
      if(StringGetCharacter(name,0)!='@')
         name="@"+name;
      
      
      ResetLastError();
      //--- copy file to memory buffer
      if(!FileIsExist(_local_path,_common_flag))
         return(ERR_FILE_NOT_EXIST);

      //---
      int flags=FILE_READ|FILE_BIN|FILE_SHARE_WRITE|FILE_SHARE_READ;
      if(_common_flag)
         flags|=FILE_COMMON;

      //---
      int file=FileOpen(_local_path,flags);
      if(file<0)
         return(_LastError);

      //---
      int file_size=(int)FileSize(file);
      uchar photo[];
      ArrayResize(photo,file_size);
      FileReadArray(file,photo,0,file_size);
      FileClose(file);

      //--- create boundary: (data -> base64 -> 1024 bytes -> md5)
      uchar base64[];
      uchar key[];
      CryptEncode(CRYPT_BASE64,photo,key,base64);
      //---
      uchar temp[1024]={0};
      ArrayCopy(temp,base64,0,0,1024);
      //---
      uchar md5[];
      CryptEncode(CRYPT_HASH_MD5,temp,key,md5);
      //---
      string hash=NULL;
      int total=ArraySize(md5);
      for(int i=0;i<total;i++)
         hash+=StringFormat("%02X",md5[i]);
      hash=StringSubstr(hash,0,16);
      //---

      //--- WebRequest
      uchar result[];
      string result_headers;

      string url=StringFormat("%s/bot%s/sendPhoto",TELEGRAM_BASE_URL,m_token);

      string part1="Content-Type: multipart/form-data; boundary="+hash+"\r\n";
      part1+="\r\n";

      //--- add chart_id
      part1+="--"+hash+"\r\n";
      part1+="Content-Disposition: form-data; name=\"chat_id\"\r\n";
      part1+="\r\n";
      part1+=name; //IntegerToString(_chat_id);
      part1+="\r\n";

      //--- add caption
      if(_caption!=NULL)
        {
         part1+="--"+hash+"\r\n";
         part1+="Content-Disposition: form-data; name=\"caption\"\r\n";
         //part1+="Content-Type: text/plain\r\n";
         //part1+="Content-Transfer-Encoding: quoted-printable\r\n";
         part1+="\r\n";
         //part1+=UrlEncode(_caption);
         part1+=_caption;
         part1+="\r\n";
        }

      //--- add image
      part1+="--"+hash+"\r\n";
      part1+="Content-Disposition: form-data; name=\"photo\"; filename=\"lampash.gif\"\r\n";
      part1+="\r\n";

      //--- 1
      uchar array1[];
      int size1=StringLen(part1);
      StringToCharArray(part1,array1,0,size1);

      //--- 2
      //photo
      int size2=ArraySize(photo);

      //---
      string part3="\r\n--"+hash+"--\r\n";
      int size3=StringLen(part3);
      uchar array3[];
      StringToCharArray(part3,array3,0,size3);

      //---
      uchar data[];
      ArrayResize(data,size1+size2+size3);
      ArrayCopy(data,array1,0);
      ArrayCopy(data,photo,size1);
      ArrayCopy(data,array3,size1+size2);

      //---
      string headers="Content-Type: multipart/form-data; boundary="+hash+"\r\n";
      int res=WebRequest("POST",url,headers,_timeout,data,result,result_headers);
      if(res==200)//OK
        {
         //--- delete BOM
         int start_index=0;
         int size=ArraySize(result);
         for(int i=0; i<fmin(size,8); i++)
           {
            if(result[i]==0xef || result[i]==0xbb || result[i]==0xbf)
               start_index=i+1;
            else
               break;
           }

         //---
         string out=CharArrayToString(result,start_index,WHOLE_ARRAY,CP_UTF8);

         //--- parse result
         CJAVal js(NULL,jtUNDEF);
         bool done=js.Deserialize(out);
         if(!done)
            return(ERR_JSON_PARSING);

         //--- get error description
         bool ok=js["ok"].ToBool();
         if(!ok)
            return(ERR_JSON_NOT_OK);

         total=ArraySize(js["result"]["photo"].m_e);
         for(int i=0; i<total; i++)
           {
            CJAVal image=js["result"]["photo"].m_e[i];

            long image_size=image["file_size"].ToInt();
            if(image_size<=file_size)
               _photo_id=image["file_id"].ToStr();
           }

         return(0);
        }
      else
        {
         if(res==-1)
           {
            string out=CharArrayToString(result,0,WHOLE_ARRAY,CP_UTF8);
            //Print(out);
            return(_LastError);
           }
         else
           {
            if(res>=100 && res<=511)
              {
               string out=CharArrayToString(result,0,WHOLE_ARRAY,CP_UTF8);
               //Print(out);
               return(ERR_HTTP_ERROR_FIRST+res);
              }
            return(res);
           }
        }
      //--- 
      return(0);
     }
 
Andrey Emelyanov:

Thanks for the interesting article. There is an error in the code (you can see it on the screenshot):

Need fixes in SendPhoto method of CCustomBot class. I suggest this option:

Thank you for your message. However, your corrected variant works only with Latin characters.

Logically, you should convert the string into an array with UTF-8 encoding and substitute it as caption in the request body.

My variant is in the attachment.

Files:
Telegram.mqh  64 kb