telegram.mqh reply to msg - page 2

 
Lorentzos Roussos:

Oh , nice . 

You modified SendPhoto or SendMessageGetID ?

SendPhoto, but there is multiple SendPhoto function in the file, maybe I picked the wrong one?

 
tdbguy:

SendPhoto, but there is multiple SendPhoto function in the file, maybe I picked the wrong one?

No it was okay . 

#property strict
#include <Telegram\TelegramL.mqh>
input string channel_name="";//PRIVATE CHANNEL NAME (string)
input string channel_private_id_string="";//PRIVATE CHANNEL ID (string)
string InpToken="";//telegram input token
// to get private channel id
//Log into Telegram via web: https://web.telegram.org
//Find your channel and copy the URL. 
//You should have something like this: https://web.telegram.org/#/im?p=c**NUMBER**_number
//The numbers between "c" and "_" are the ID of your private channel.

//Now tell your bot to send the messages to chat_id=-100NUMBER

//mql code : 
/*
id in webtelegram c1334309342_1978467370643286368 >
1334309342 > 
-1001334309342 >
    string ids="-1001334309342";
    long idl=(long)StringToInteger(ids);
    bot.SendMessage(idl,messi,NULL,false,false);


*/
string private_channel_string_id="";
long private_channel_post_id=0,private_channel_prepost_id=0;
//id of last message for replies 
long last_message_id=-1;
/*
you can implement a solution where in your trades monitoring class 
you maintain the message id that was produced after the trade was posted in the telegram channel
for example ,you open a buy order ,you get its ticket ,you store it in your class array with your trades 
myTrades[x].ticket=ticket;
then after messaging the channel ,you can store the return id (the id of the message) in the same class array
myTrades[x].reply_message_id=last_message_id;

then when stop loss is hit for example you could : 
if(myTrades[x].reply_messge_id!=-1) > quote and message
*/
int OnInit()
  {
  last_message_id=-1;
  telegram_running=false;
  StartTelegramService();  
  //finding your channels id
    string ids="-100"+channel_private_id_string;
    private_channel_string_id=ids;
    long idl=(long)StringToInteger(ids);
    private_channel_prepost_id=(long)StringToInteger(channel_private_id_string);
    private_channel_post_id=idl;
    if(idl>=0) 
    {
    Alert("Invalid Private Channel ID");
    ExpertRemove();
    telegram_running=false;
    }
  //test message and its quote 
  /*
    string txt="This will be quoted";
    last_message_id=-1;
    int res=bot.SendMessageGetID(false,0,private_channel_post_id,txt,NULL,false,false,last_message_id);
                //if it was sent
                if(last_message_id!=-1)
                {
                Sleep(1000);
                //reply to it !
                txt="Reply test!";
                long reply_id=-1;
                bot.SendMessageGetID(true,last_message_id,private_channel_post_id,txt,NULL,false,false,reply_id);
                }  
  */
    string name_for_screenshot="Screenie.bmp";
    bool capture=ChartScreenShot(ChartID(),name_for_screenshot,100,100,ALIGN_RIGHT);
          ResourceCreate("SeV",name_for_screenshot);
          FileDelete(name_for_screenshot);
          ResourceSave("Sev",name_for_screenshot);
          ResourceFree("Sev");      
    //if screenshot taken 
    last_message_id=-1;
      if(capture)
      {
      string get_photo_id_string="";
      string caption="Caption Of Screenshot";
      bot.SendPhotoGetID(false,-1,get_photo_id_string,private_channel_post_id,name_for_screenshot,caption,false,1000,last_message_id);
      if(last_message_id!=-1)
        {
        Alert("ID for reply : "+IntegerToString(last_message_id));
        //delay
        Sleep(1000);
        //reply to photo ! 
        string txt="Reply to photo !";
        bot.SendMessageGetID(true,last_message_id,private_channel_post_id,txt,NULL,false,false,last_message_id);
        }
      }
    //if screenshot taken ends here   
  
  return(INIT_SUCCEEDED);
  }
  
  
//TELEGRAM STUFF 
class CMyBot: public CCustomBot
  {
private:
   int               m_radio_index;
   bool              m_lock_state;
   bool              m_mute_state;
public:
   //find chat id of private channel 
   long FindChannelChatId(string c_name,long normal_id,long post_id)
   {
   long returnio=-1;
   int total_chats=bot.ChatsTotal();
   for(int c=0;c<total_chats;c++)
    {
    CCustomChat chat=m_chats.GetNodeAtIndex(c);
    }
   return(returnio);
   }
   //find chat id of private channel ends here 
  };

//---



//---
CMyBot bot;
int getme_result;

//user structure - for signals 
long telegram_chat_ids[];
int telegram_chats_total=0;
int telegram_chats_size=0;
int telegram_chats_step=10;
bool telegram_running=false,must_hide_keyboard=false;

//start telegram service and get id's 
  void StartTelegramService()
  {
  telegram_running=true;
  must_hide_keyboard=false;
//--- set token
  bot.Token(InpToken);
//--- check token
  getme_result=bot.GetMe();
  if(getme_result!=0) telegram_running=false;
  //if(telegram_running) Alert("Bot Name : "+bot.Name);
  if(telegram_running==false)
   {
   Alert("Could not start telegram service!");
   ExpertRemove();
   }
  }
//start telegram service and get id's ends here 

//TELEGRAM STUFF ENDS HERE 
Files:
TelegramL.mqh  83 kb
 
Lorentzos Roussos:

No it was okay . 

You trully are amazing!

I learned many thing thanks to you, can I buy you a coffee? Its not much but its the least I can do given how much you gave me this past few days.

Please send me your paypal in private Lorentzos :)

 
tdbguy:

You trully are amazing!

I learned many thing thanks to you, can I buy you a coffee? Its not much but its the least I can do given how much you gave me this past few days.

Please send me your paypal in private Lorentzos :)

No problem , i try to code everyday to stay "code fit". We might have that coffee someday ,thats all . :) 

 
bot.SendPhotoGetID(true,last_message_id,get_photo_id_string,private_channel_post_id,name_for_screenshot,caption,false,1000,last_message_id);

There was nothing bounded to the first boolean above, so I changed it to true and added this code to SendPhotoGetID in TelegramLmqh  to reply with photos :

string params=""; 
      if(!replying_to_message) params=StringFormat("chat_id=%lld&text=%s",_chat_id,UrlEncode(_caption));
      if(replying_to_message)
        {
        params=StringFormat("chat_id=%lld&&reply_to_message_id=%lld&text=%s",_chat_id,message_id_replying_to,UrlEncode(_caption));
        //params+="&reply_to_message_id="+IntegerToString(message_id_replying_to);
        }

There is no error when I send messages to telegram but this doesnt work.

I'm guessing it has something to do with:

int res=WebRequest("POST",url,headers,_timeout,data,result,result_headers);

as the params variable isn't defined in it.

I tried to add it but WebRequest seems to have a defined parameters count. So I went with PostRequest like with SendMessageGetID but still no luck there...

 
Lorentzos Roussos:

No problem , i try to code everyday to stay "code fit". We might have that coffee someday ,thats all . :) 

Alright ahah.

 
string out;      
int res=WebRequest(out,url,headers,_timeout,data,result,result_headers);
            PrintFormat("out : "+out);

out is initialised but not defined, is it a variable that will receive its value by telegram in this case?

in any case, no error when I print it.


We can reply to a photo with a photo without problem in telegram so why not?


edit:

      string out;
      int res=PostRequest(out,url,params,WEB_TIMEOUT);
      PrintFormat("out : "+out)

here I have a bad request ("no photo in the request")

 

It returns "Bad request" if you print the "out" variable in the function 

I dont think you can reply to a photo with a photo - but i'll mod it ,see what i mean with code fit ? i forgot to place that functionality :D -

---- edits : 

  1. First go to the SendMessageGetID and follow the params -where they are sent , we see a PostRequest function.Go in there ...
  2. In the PostRequest function ,can see the params are sent in the data as a char array ,so now go to SendPhotoGetID and look for where these char arrays are ...
  3. There is a uchar array data[] thats where the photo data is sent so we utilize this , now we need to find if we plug our "replying_id" above or below the data (probably above)...
  4. We see a chat id is sent in the top section of the data ,so we plug the replying id there and add a line as well ...

Hmm ,doesnt look very reply-ish : (maybe cause im sending the same photo)

#property strict
#include <Telegram\TelegramL.mqh>
input string channel_name="";//PRIVATE CHANNEL NAME (string)
input string channel_private_id_string="";//PRIVATE CHANNEL ID (string)
string InpToken="";//telegram input token
// to get private channel id
//Log into Telegram via web: https://web.telegram.org
//Find your channel and copy the URL. 
//You should have something like this: https://web.telegram.org/#/im?p=c**NUMBER**_number
//The numbers between "c" and "_" are the ID of your private channel.

//Now tell your bot to send the messages to chat_id=-100NUMBER

//mql code : 
/*
id in webtelegram c1334309342_1978467370643286368 >
1334309342 > 
-1001334309342 >
    string ids="-1001334309342";
    long idl=(long)StringToInteger(ids);
    bot.SendMessage(idl,messi,NULL,false,false);


*/
string private_channel_string_id="";
long private_channel_post_id=0,private_channel_prepost_id=0;
//id of last message for replies 
long last_message_id=-1;
/*
you can implement a solution where in your trades monitoring class 
you maintain the message id that was produced after the trade was posted in the telegram channel
for example ,you open a buy order ,you get its ticket ,you store it in your class array with your trades 
myTrades[x].ticket=ticket;
then after messaging the channel ,you can store the return id (the id of the message) in the same class array
myTrades[x].reply_message_id=last_message_id;

then when stop loss is hit for example you could : 
if(myTrades[x].reply_messge_id!=-1) > quote and message
*/
int OnInit()
  {
  last_message_id=-1;
  telegram_running=false;
  StartTelegramService();  
  //finding your channels id
    string ids="-100"+channel_private_id_string;
    private_channel_string_id=ids;
    long idl=(long)StringToInteger(ids);
    private_channel_prepost_id=(long)StringToInteger(channel_private_id_string);
    private_channel_post_id=idl;
    if(idl>=0) 
    {
    Alert("Invalid Private Channel ID");
    ExpertRemove();
    telegram_running=false;
    }
  //test message and its quote 
  /*
    string txt="This will be quoted";
    last_message_id=-1;
    int res=bot.SendMessageGetID(false,0,private_channel_post_id,txt,NULL,false,false,last_message_id);
                //if it was sent
                if(last_message_id!=-1)
                {
                Sleep(1000);
                //reply to it !
                txt="Reply test!";
                long reply_id=-1;
                bot.SendMessageGetID(true,last_message_id,private_channel_post_id,txt,NULL,false,false,reply_id);
                }  
  */
    string name_for_screenshot="Screenie.bmp";
    bool capture=ChartScreenShot(ChartID(),name_for_screenshot,100,100,ALIGN_RIGHT);
          ResourceCreate("SeV",name_for_screenshot);
          FileDelete(name_for_screenshot);
          ResourceSave("Sev",name_for_screenshot);
          ResourceFree("Sev");      
    //if screenshot taken 
    last_message_id=-1;
      if(capture)
      {
      string get_photo_id_string="";
      string caption="Caption Of Screenshot";
      bot.SendPhotoGetID(false,-1,get_photo_id_string,private_channel_post_id,name_for_screenshot,caption,false,1000,last_message_id);
      if(last_message_id!=-1)
        {
        Alert("ID for reply : "+IntegerToString(last_message_id));
        //delay
        Sleep(1000);
        //reply to photo ! 
        /* reply with text 
        string txt="Reply to photo !";
        bot.SendMessageGetID(true,last_message_id,private_channel_post_id,txt,NULL,false,false,last_message_id);
        */
        //reply with same photo
        bot.SendPhotoGetID(true,last_message_id,get_photo_id_string,private_channel_post_id,name_for_screenshot,"",false,1000,last_message_id);
        }
      }
    //if screenshot taken ends here   
  
  return(INIT_SUCCEEDED);
  }
  
  
//TELEGRAM STUFF 
class CMyBot: public CCustomBot
  {
private:
   int               m_radio_index;
   bool              m_lock_state;
   bool              m_mute_state;
public:
   //find chat id of private channel 
   long FindChannelChatId(string c_name,long normal_id,long post_id)
   {
   long returnio=-1;
   int total_chats=bot.ChatsTotal();
   for(int c=0;c<total_chats;c++)
    {
    CCustomChat chat=m_chats.GetNodeAtIndex(c);
    }
   return(returnio);
   }
   //find chat id of private channel ends here 
  };

//---



//---
CMyBot bot;
int getme_result;

//user structure - for signals 
long telegram_chat_ids[];
int telegram_chats_total=0;
int telegram_chats_size=0;
int telegram_chats_step=10;
bool telegram_running=false,must_hide_keyboard=false;

//start telegram service and get id's 
  void StartTelegramService()
  {
  telegram_running=true;
  must_hide_keyboard=false;
//--- set token
  bot.Token(InpToken);
//--- check token
  getme_result=bot.GetMe();
  if(getme_result!=0) telegram_running=false;
  //if(telegram_running) Alert("Bot Name : "+bot.Name);
  if(telegram_running==false)
   {
   Alert("Could not start telegram service!");
   ExpertRemove();
   }
  }
//start telegram service and get id's ends here 

//TELEGRAM STUFF ENDS HERE 
Files:
TelegramL.mqh  84 kb
 

This is because this isn't a reply, and yes I thought exactly the same thing about the photo, but I tried with a different one and it doesnt change anything.


edit:

Alright I found what was the error

    if(replying_to_message)
      {
      ArrayAdd(data,"Content-Disposition: form-data; name=\"reply_to_message_id=\"\r\n");
      ArrayAdd(data,"\r\n");
      ArrayAdd(data,IntegerToString(message_id_replying_to));
      ArrayAdd(data,"\r\n");      
      }

I removed the "=" -> 

 form-data; name=\"reply_to_message_id=
    if(replying_to_message)
      {
      ArrayAdd(data,"--"+hash+"\r\n");
      ArrayAdd(data,"Content-Disposition: form-data; name=\"reply_to_message_id\"\r\n");
      ArrayAdd(data,"\r\n");
      ArrayAdd(data,IntegerToString(message_id_replying_to));
      ArrayAdd(data,"\r\n");      
      }
Files:
TelegramL.mqh  84 kb
 
tdbguy:

This is because this isn't a reply, and yes I thought exactly the same thing about the photo, but I tried with a different one and it doesnt change anything.


edit:

Alright I found what was the error

I removed the "=" -> 

Wow excellent :) . nice catch . 

(and also added ArrayAdd(data,"--"+hash+"\r\n"); after the if replying to message) 
Reason: