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

 
Lorentzos Roussos #:

我回复的时候还没有代码,我知道你可以用一个将信息添加到突发列表的函数来替换 bot.SendMessage 函数。你也可以不断增加信息并将其从循环中发送出去,但这样会受到字符数的限制。

一个简单的示意图可以是这样的

  1. 您有一个名为 "发件箱 "的字符串数组
  2. 处理发件箱的时间间隔,所以要使用 "OnTimer()"(如果您也从电报中读取信息,您可能已经在使用它了)
  3. 然后,您可以不使用 Sleep() 函数,而是通过记住上一条信息的发送时间,来自行限制每条信息之间的毫秒间隔。
  4. 您可以使用 GetTickCount() 函数来轮询毫秒数,并存储信息离开的最后毫秒数,然后从当前毫秒数中减去该毫秒数,得到时间距离。
  5. 如果距离上一条消息的毫秒数大于您设定的毫秒数限制,则从发件箱发送下一条消息。
  6. 你现在不再在循环中调用 bot.SendMessage 而是调用 Outbox.add_message_for_sending 或其他。
  7. 如果修改后还能存储聊天 ID,就能存储消息的去向,这样就能解决多个用户的问题。

感谢您的回复,我会尝试您的方法

 
你好,伙计。文章写得很好。它对频道和群组都很有效,而且我必须让机器人成为管理员。但如果我想将它发送到个人电报聊天,该如何将机器人添加到个人聊天?可以吗?还是必须将消息发送到频道?
您怎么看?
 
lbgraf #:
您好!
非常感谢你的这种工作!!!!
请问如何更改字体、背景和文字颜色?
谢谢

例如,要使用粗体字体,您需要在 Telegram.mqh 中启用 HTML 发送功能。

const bool    _as_HTML=true

然后用 <b> 标签发送文本。

 
Herman Makmur #:

Never mind....

我通过将 AsHTML 标志设置为 true 找到了答案......

bot.SendMessage(InpTelegramId,"<b>Balance: $10056.21</b>",true);

抱歉...


您好,能分享一下如何实现这一功能的代码吗?我还在寻找将文本加粗、斜体并发送到电报服务器的代码。

 
你好 ,Andriy Voitenko,我稍微修改了 SendScreenShot() 函数 的代码 。SendPhoto(_chat_id,filename,screen_id,_symbol+"_"+StringSubstr(EnumToString(_period),7)) 与 <Telegram . mqh> 中任何SendPhoto() 函数模型的输入参数都不匹配
在发送 .gif 文件时,我收到错误 400,说明 SendPhoto() 函数中的 某个函数不工作(我使用的是<Telegram.mqh> 文件 655 行中三个函数模型中的第二个),能否请您更新代码使其工作?
Andriy Voitenko #:

Roman,如果您只需要机器人发送截图,您可以这样做:

1.向 @MyTelegramID_bot 询问您的聊天号码。

2.编写一个简单的机器人,检查是否有新项目,并将图片发送到指定 ID 的聊天室。例如

#include <Telegram.mqh>
//+------------------------------------------------------------------+
//| 输入参数|
//+------------------------------------------------------------------+
input ENUM_LANGUAGES    InpLanguage=LANGUAGE_EN;//语言
input string            InpToken="";//代币
input long              ChatId=0;   //聊天 ID
//---
CCustomBot bot;
int pos_count;
//+------------------------------------------------------------------+
int OnInit()
  {
   bot.Token(InpToken);
   int res=bot.GetMe();
   if(res!=0)
     {
      Print(GetErrorDescription(res));
     }
   pos_count=PositionCount(_Symbol);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnTick()
  {
   int pos_count_new=PositionCount(_Symbol);
   if(pos_count_new>pos_count)
     {
      pos_count=pos_count_new;
      int result=SendScreenShot(ChatId,_Symbol,0,NULL);
      if(result!=0)
         Print(GetErrorDescription(result,InpLanguage));
     }
  }
//+------------------------------------------------------------------+
int PositionCount(const string _symbol)
  {
   int count=0;
   int orders_total=OrdersTotal();
   for(int i=0; i<orders_total; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         PrintError(ERR_ORDER_SELECT);
         return(-1);
        }
      //---
      if(_symbol==NULL || OrderSymbol()==_symbol)
         count++;
     }
//---
   return(count);
  }
//+------------------------------------------------------------------+
int SendScreenShot(const long _chat_id,
                   const string _symbol,
                   const ENUM_TIMEFRAMES _period,
                   const string _template=NULL)
  {
   int result=0;

   long chart_id=ChartOpen(_symbol,_period);
   if(chart_id==0)
      return(ERR_CHART_NOT_FOUND);

   ChartSetInteger(ChartID(),CHART_BRING_TO_TOP,true);

//--- 更新图表
   int wait=60;
   while(--wait>0)
     {
      if(SeriesInfoInteger(_symbol,_period,SERIES_SYNCHRONIZED))
         break;
      Sleep(500);
     }

   if(_template!=NULL)
      if(!ChartApplyTemplate(chart_id,_template))
         PrintError(_LastError,InpLanguage);

   ChartRedraw(chart_id);
   Sleep(500);

   ChartSetInteger(chart_id,CHART_SHOW_GRID,false);

   ChartSetInteger(chart_id,CHART_SHOW_PERIOD_SEP,false);

   string filename=StringFormat("%s%d.gif",_symbol,_period);

   if(FileIsExist(filename))
      FileDelete(filename);
   ChartRedraw(chart_id);

   Sleep(100);

// if(ChartScreenShot(chart_id,filename,800,600,ALIGN_RIGHT))
   if(ChartScreenShot(chart_id,filename,1024,768,ALIGN_RIGHT))
     {
      Sleep(100);

      bot.SendChatAction(_chat_id,ACTION_UPLOAD_PHOTO);

      //--- 等待 30 秒以保存屏幕截图
      wait=60;
      while(!FileIsExist(filename) && --wait>0)
         Sleep(500);

      //---
      string screen_id;
      result=bot.SendPhoto(_chat_id,filename,screen_id,_symbol+"_"+StringSubstr(EnumToString(_period),7));
     }

   ChartClose(chart_id);
   return(result);
  }

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

大家好、

我正试图使用机器人从 MT5 向 Telegram 发送信息。但是,由于出现错误,我无法从 MT5 向 Telegram 发送消息:错误代码 400 说明 "错误请求:未找到聊天"。

有人遇到过同样的问题吗?您能解释一下为什么会出现这种错误吗?

我在网上做了很多研究,但没有得到正确的答案。

关于交易、自动交易系统和测试交易策略的论坛

MT5 到 Telegram 的错误:错误代码 400 描述 "错误请求:未找到聊天"。

Cerilo Cabacoy, 2023.11.21 18:14

先生,感谢您的回复。下面是完整的源代码。这只是一个简单的专家顾问,它从文本文件中提取数据,然后尝试将数据发送到 Telegram 频道。但是,它遇到了上述错误。

#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#include <Telegram.mqh>
CCustomBot tgbot;

input string TelegramBotToken = "6770842913:AAGcnR666ddL7hCB8HeTNs6HdNe28y3F-ik";
input string TelegramChatID = "-1002063516288";
input string TelegramAPIurl = "https://api.telegram.org";
input string namefile = "WagScores.txt";

datetime h1time = 0;
string channelname = "";
//+------------------------------------------------------------------+
int OnInit() {

   tgbot.Token(TelegramBotToken);
   int res = tgbot.GetMe();      Print("GetMe() results: "+(string)res);
   channelname = tgbot.Name();   Print("bot name: "+channelname);
   
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {

   
}
//+------------------------------------------------------------------+
void OnTick() {

   ChartRedraw();
   if(NewH1Bar()) {
      string data[];
      string output = "";
      GetTxtDataToArray(namefile,data); 
      string message = StringFormat("Time: %s\n",TimeToStr(TimeCurrent()));  
      StringAdd(output,message);   
      for(int i = 0; i < ArraySize(data); i++) {
         string strmsg = StringFormat("%s\n",data[i]);
         StringAdd(output,strmsg);     
      }     
      int res = tgbot.SendMessage(TelegramChatID,output);      Print((string)__LINE__+" "+(string)res);
      SendNotification(output);
   }
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
bool NewH1Bar() { 
     
   datetime newtime = iTime(Symbol(),PERIOD_H1,0);
   if(newtime==h1time) return false;
   h1time = newtime;                                
   return true;
}
//+------------------------------------------------------------------+ 
void GetTxtDataToArray(string filename,string &array[]) { 
             
   if(!FileIsExist(filename)) return;
   int handle = FileOpen(filename,FILE_TXT|FILE_READ|FILE_ANSI);
   if(handle==INVALID_HANDLE) { Print(""+__FUNCTION__+" "+(string)__LINE__+" opening file error"); return; }
   FileSeek(handle,0,SEEK_SET);
   while(!FileIsEnding(handle)) {
      string line = FileReadString(handle); 
      ArrayResize(array,ArraySize(array)+1);         
      array[ArraySize(array)-1] = line;
   }
   FileClose(handle);    
}

OOP in MQL5 by Example: Processing Warning and Error Codes
OOP in MQL5 by Example: Processing Warning and Error Codes
  • www.mql5.com
The article describes an example of creating a class for working with the trade server return codes and all the errors that occur during the MQL-program run. Read the article, and you will learn how to work with classes and objects in MQL5. At the same time, this is a convenient tool for handling errors; and you can further change this tool according to your specific needs.
[删除]  

关于交易、自动交易系统和测试交易策略的论坛

在 telegram 消息中添加表情符号。

Frédéric LEBRE, 2023.12.04 13:56

您好、

请帮帮我。

我尝试使用 emoji 向 telegram 发送信息。

当 emoji unicode 为 U+2702 时,我使用字符串值 " \x2702" 并成功发送。

SendTelegramMessage(TelegramApiUrl, TelegramBotToken, ChatId, "\x2702");

但当 unicode 像这样 :U+1F648 时却不起作用。

我在主题中看到了 <Telegram.mqh>,但我不知道如何做得更多。

感谢您的解答。

 
感谢您提供这个很酷的图书馆!不过,有一个问题。我还没有找到 用户发送消息的 方法。我想让它在 Expert Advisor 发现信号时直接通知我。我不想创建一个组
 
Roboboy18 向 用户发送消息的 方法。我想让它在 Expert Advisor 发现信号时直接通知我。我不想建立一个群组

我找到了方法,如果有人感兴趣,请向我咨询)。

 
Roboboy18 #:

找到方法了,有兴趣的人可以问问 )

你可以直接写下来。这样别人就不用找你问了。