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

 

Hi all,

Maybe some of you can help, I'm desperately trying to find a solution to send a local screenshot via MT4 to Telegram.

Quick code snipped

int res=bot.SendMessage(-100135482xxx,"Running a test " +TimeToString(TimeCurrent()));
int ac=bot.SendChatAction(-100135482xxx,ACTION_UPLOAD_PHOTO);
int pic=bot.SendPhoto(filepath+"\mypic.gif",-100135482xxx,filepath);

First one works, as I'm getting the message, even if I use @channelname

Second one works only with chatID, which is ok, I can live with it

Third one is throwing me the error:

2020.08.07 21:40:15.175 xxx1: {"ok":false,"error_code":400,"description":"Bad Request: chat not found"}

Any idea, what I'm doing wrong?


 
Tomasz Maria Burzanowski:

Hi all,

Maybe some of you can help, I'm desperately trying to find a solution to send a local screenshot via MT4 to Telegram.

Quick code snipped

First one works, as I'm getting the message, even if I use @channelname

Second one works only with chatID, which is ok, I can live with it

Third one is throwing me the error:

2020.08.07 21:40:15.175 xxx1: {"ok":false,"error_code":400,"description":"Bad Request: chat not found"}

Any idea, what I'm doing wrong?


yes you have to modify the mqh file so that it accepts that negative number.

 

@Marco vd Heijden thanks!

This is interesting I had a look at Telegram.mqh and I see three functions for SendPhoto, out of which I'm wondering how the code knows which one to use. The third one is anyway out of the question.


First:

   int SendPhoto(string &_photo_id,
                 const long _chat_id,
                 const string _local_path,
                 const string _caption=NULL,
                 const bool _common_flag=false,
                 const int _timeout=10000)

Second:

int SendPhoto(string &_photo_id,
                 const string _channel_name,
                 const string _local_path,
                 const string _caption=NULL,
                 const bool _common_flag=false,
                 const int _timeout=10000)

Could it be that the code does not know which one I want to take? I'm trying to trigger the first one, but actually the second one would be better.

Secondly I tried to search in the first function what is happening with _chat_id, but I can only see a simple integer to string conversion:

ArrayAdd(data,"\r\n");
      ArrayAdd(data,"--"+hash+"\r\n");
      ArrayAdd(data,"Content-Disposition: form-data; name=\"chat_id\"\r\n");
      ArrayAdd(data,"\r\n");
      ArrayAdd(data,IntegerToString(_chat_id));
      ArrayAdd(data,"\r\n");
 
Tomasz Maria Burzanowski:

notice second parameter one is a long the other is a string

 const long _chat_id,

and 

const string _channel_name,

this is part of mql basics u can have million functions with the same name with different parameters or number of parameters 

 

@Jefferson Metha yes I do understand that, it was just my thought, as it's been so tough to find the issues.

I used now the tips described in https://www.mql5.com/en/forum/89826/page15#comment_17079865, but still not getting anywhere.

I'd like to focus on the sendPhoto function where the input is Channel name, not chat ID, so let's solely focus on one thing.


What I did so far, in Telegarm.mqh in the function

   int SendPhoto(string &_photo_id,
                 const string _channel_name,
                 const string _local_path,
                 const string _caption=NULL,
                 const bool _common_flag=false,
                 const int _timeout=10000)

I've modified the following line:

      string name=StringTrim(_channel_name);
      name=_channel_name;
      /*if(StringGetCharacter(name,0)!='@') TB 09.08
         name="@"+name;*/

Basically removing the StringTrim function, but it's not getting better.

Discussion of article "How to create bots for Telegram in MQL5"
Discussion of article "How to create bots for Telegram in MQL5"
  • 2020.06.26
  • www.mql5.com
New article How to create bots for Telegram in MQL5 has been published: Author: Andrey Voytenko...
 

Finally figured it out.

Changes:

int SendPhoto(string &_photo_id,
                 const string _channel_name,
                 const string _local_path,
                 const string _caption=NULL,
                 const int _common_flag=0,//Changed to int not bool
                 const int _timeout=10000)

Removed:

string name=StringTrim(_channel_name);
      if(StringGetCharacter(name,0)!='@')
         name="@"+name;

And finally in the EA, as simple as:

string filepath = "mypic.gif";
string photo_id;
int pic=bot.SendPhoto(photo_id,InpChannelName,filepath);
 
In vindows 7, 8 and 8.1 does not work neither through webquest in mt, nor through vininet .vindows server 2012 and everything above - works normally.Checked also access to api through Internet Explorer in these systems, so on 7 and 8 and 8.1 even the browser does not get the answer.
 
Thank you for your effort in composing this article! Extremely helpful for a newbie
 

Good afternoon.

Thanks for the detailed article. Very helpful.

I encountered that WEB request did not work.

It gives an error

2020.08.19 15:56:04.830 Moving Average Signal To Alert (RTSRIU0,M1) Error: URL is not allowed for WebRequest

2020.08.19 15:54:01.332 Moving Average Signal To Alert (RTSRIU0,M1) exit channel https://api.telegram.org/bot1234567:AAHwDJwwrh-m16O0EoIA7--3BG0I5ayk3E0/sendMessage chat_id=@MyTestSignal&text=HELLO

After researching it turned out to be missing ?( question) after /sendmessage?

Has this been fixed yet ? Is there a new version ?

I'll fix it myself.

 

I am sending text to telegram from the indicator. But I had to change POST to GET type of request.

If I send with POST, I get error 400. If I send the same code from the EA (but I use WebRequest instead of _WebRequest), the code works in POST mode. Can you tell me where I should look, as in the GET method I have a limit on the length of the request?

Error and with GET also error 400...

2020.08.21 08:52:20.138 testWININET GBPUSD,M1: Error loading 'https://api.telegram.org/bot123123123:ываываыукаыва /sendMessage?chat_id=-123123123123123&parse_mode=HTML&text=Hello', code 400

If I send it through the browser, the request goes through.



#property link "https://www.mql5.com"

#property version "1.00"

#property strict

#property indicator_chart_window


//#include <Wininet.mqh>

#include <Webrequest_dll.mqh>



#define telegramBaseURL "https://api.telegram.org"


//https://tlgrm.ru/docs/bots/api to read how to send what

input string inpToken="11111212:vapvapYVVAEFfer";//Token

input string inpChat_id="-3123123123123213";

bool inpParsMode =true;


int OnInit()

{

string data;

string url="https://api.telegram.org/bot" + inpToken + "/sendMessage?chat_id="+inpChat_id;

if(inpParsMode){

url = url + "&parse_mode=HTML"

}

// url = url + "&text= " + "<b>1114</b>"

// url = url + "&text= " + "<i>1114</i>";

url = url + "&text=" + "1118";

Print (url);

string cookie=NULL,headers;

char post[],result[];

int res=_WebRequest("GET",url,cookie,NULL,500,post,0,result,headers);

if(res==-1)

{

Print("Error in WebRequest. Error code =",GetLastError());

//--- the URL is probably missing from the list, print a message about the need to add it.

MessageBox("It is necessary to add the address '"+url+"' to the list of allowed URLs in the 'Advisors' tab", "Error",MB_ICONINFORMATION);

}

else

{

if(res==200)

{

//--- successful download

PrintFormat("File successfully uploaded, size %d bytes.",ArraySize(result));

//PrintFormat("Server headers: %s",headers);

//--- save data to file

int filehandle=FileOpen("url.htm",FILE_WRITE|FILE_BIN);

if(filehandle!=INVALID_HANDLE)

{

//--- save the contents of the result[] array to a file

FileWriteArray(filehandle,result,0,ArraySize(result));

//--- close the file

FileClose(filehandle);

}

else

Print("Error in FileOpen. Error code =",GetLastError());

}

else

PrintFormat("Error loading '%s', code %d",url,res);

}

// ReadUrl(url,data);


return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+

//| Custom indicator iteration function |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

const int prev_calculated,

const datetime &time[],

const double &open[],

const double &high[],

const double &low[],

const double &close[],

const long &tick_volume[],

const long &volume[],

const int &spread[])

{

//---

//--- return value of prev_calculated for next call

return(rates_total);

}

//+------------------------------------------------------------------+


Bots: An introduction for developers
  • core.telegram.org
Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and inline requests. You control your bots using HTTPS requests to our Bot API. 1. What can I do with bots? To name just a few things, you could use bots to: Get customized notifications and news . A bot can act as a smart...