文章 "如何采用 MQL5 创建用于 Telegram 的 bots" - 页 32

 

大家好、

我正在拼命寻找一种通过 MT4 向 Telegram 发送本地截图的解决方案。

快速代码截图

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

第一段有效,因为即使我使用 @channelname 也能收到消息

第二段代码只适用于 chatID,这没关系,我可以接受

第三个代码会出错:

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

你知道我做错了什么吗?


 
Tomasz Maria Burzanowski:

大家好

也许你们中有人能帮上忙,我正在拼命寻找通过 MT4 向 Telegram 发送本地截图的解决方案。

快速代码截图

第一段有效,因为即使我使用 @channelname 也能收到消息

第二个代码只对聊天 ID 有效,没关系,我可以接受

第三种方法会让我出错:

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

你知道我做错了什么吗?


是的,你必须修改 mqh 文件,使其接受负数。

 

@Marco vd Heijden 谢谢!

这很有趣,我查看了 Telegram.mqh,发现 SendPhoto 有三个函数,我想知道代码是如何知道使用哪个函数的。反正第三个是不可能的。


第一个

   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)

第二个:

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)

会不会是代码不知道我要使用哪个函数?我试图触发第一个,但实际上第二个会更好。

其次,我试图在第一个函数中搜索 _chat_id 发生了什么,但我只能看到简单的整数到字符串的转换:

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:

注意第二个参数一个是长字符串,另一个是字符串

 const long _chat_id,

而且

const string _channel_name,

这是 mql 基础知识的一部分,你可以有数百万个同名函数,但参数或参数个数各不相同

 

@Jefferson Metha 是的,我明白,这只是我的想法,因为要找到问题太难了。

我现在使用了 https://www.mql5.com/en/forum/89826/page15#comment_17079865 中描述的提示,但仍然没有任何进展。

我想把重点放在 sendPhoto 功能上,在这个功能中输入的是频道名称,而不是聊天 ID,所以我们只关注一件事。


到目前为止,我在 Telegarm.mqh 中的函数

   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)

我修改了下面一行:

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

基本上删除了 StringTrim 函数,但情况并没有好转。

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...
 

终于想通了。

更改:

int SendPhoto(string &_photo_id,
                 const string _channel_name,
                 const string _local_path,
                 const string _caption=NULL,
                 const int _common_flag=0,/改为 int 而不是 bool
                 const int _timeout=10000)

删除:

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

最后,在 EA 中,就这么简单:

string filepath = "mypic.gif";
string photo_id;
int pic=bot.SendPhoto(photo_id,InpChannelName,filepath);
 
在 Windows 7、8 和 8.1 中,既不能通过 mt 中的 webquest 进行操作,也不能通过 vininet 进行操作,而在 Windows server 2012 及以上的所有系统中均能正常工作。
 
感谢您为撰写这篇文章所做的努力!对新手极有帮助
 

下午好。

感谢您的详细文章。非常有帮助。

我遇到的问题是,WEB 请求不起作用。

它给出了一个错误

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

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

经过研究,发现是 /sendmessage 后缺少了 ?(问题)。

这个问题已经解决了吗?有新版本吗?

我会自己修复的。

 

我正在从指示器向电报发送文本。但我不得不将 POST 类型的请求改为 GET 类型的请求。

如果用 POST 发送,会出现错误 400。如果我从 EA 发送相同的代码(但我使用的是 WebRequest 而不是 _WebRequest),代码就能在 POST 模式下工作。在 GET 方法中,我的请求长度有限制,你能告诉我该从哪里查找吗?

错误,GET 也会出现错误 400...

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

如果我通过浏览器发送,请求会通过。



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

#property version "1.00"

#property strict

#property indicator_chart_window


//#include <Wininet.mqh

#include <Webrequest_dll.mqh> #define telegramBase.mqh.



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


//https://tlgrm.ru/docs/bots/api 阅读如何发送

输入字符串 inpToken="11111212:vapvapYVVAEFfer";//Token

输入字符串 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="" + inpToken + "/endMessage?chat_id="+inpChat_id; if(inpParsMode){

}

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

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

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

打印 (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.错误代码 =",GetLastError());

//--- 列表中可能缺少 URL,请打印需要添加 URL 的信息。

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

}

否则

{

if(res==200)

{

//--- 下载成功

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

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

//--- 将数据保存到文件

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

if(filehandle!=INVALID_HANDLE)

{

//--- 将结果[] 数组的内容保存到文件中

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

//--- 关闭文件

FileClose(filehandle);

}

else

Print("Error in FileOpen.错误代码 =",GetLastError());

}

否则

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

}

// ReadUrl(url,data);


return(INIT_SUCCEEDED);

}

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

//| 自定义指标迭代函数

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

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[])

{

//---

//--- 为下一次调用返回 prev_calculated 的值

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...