Advertisement
Guest User

Untitled

a guest
Apr 14th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.17 KB | None | 0 0
  1. #include "ESP8266TelegramBOT.h"
  2.  
  3.  
  4. TelegramBOT::TelegramBOT(String token, String name, String username)    {
  5.     _token=token;
  6.     _name=name;
  7.   _username=username;
  8. }
  9.  
  10.  
  11. void TelegramBOT::begin(void)   {
  12.       message[0][0]="0";   // number of received messages
  13.       message[1][0]="";    
  14.     message[0][1]="0";  // code of last read Message
  15. }
  16.  
  17.  
  18.  
  19. /**************************************************************************************************
  20.  * function to achieve connection to api.telegram.org and send command to telegram                *
  21.  * (Argument to pass: URL to address to Telegram)                                                 *
  22.  **************************************************************************************************/
  23. String TelegramBOT::connectToTelegram(String command)  {
  24.     String mess="";
  25.     long now;
  26.     bool avail;
  27.     // Connect with api.telegram.org      
  28.     IPAddress server(149,154,167,198);
  29.     if (client.connect(server, 443)) {  
  30.         //Serial.println(".... connected to server");
  31.         String a="";
  32.         char c;
  33.     int ch_count=0;
  34.         client.println("GET /"+command);
  35.         now=millis();
  36.         avail=false;
  37.         while (millis()-now<1500) {  
  38.             while (client.available()) {
  39.           char c = client.read();
  40.               //Serial.write(c);
  41.               if (ch_count<700)  {
  42.                  mess=mess+c;
  43.              ch_count++;
  44.           }
  45.               avail=true;
  46.         }
  47.             if (avail) {
  48.         //Serial.println();
  49.         //Serial.println(mess);
  50.         //Serial.println();
  51.         break;
  52.         }
  53.         }
  54.     }
  55.     return mess;
  56. }
  57.  
  58.  
  59.  
  60.  
  61. /***************************************************************
  62.  * GetUpdates - function to receive all messages from telegram *
  63.  * (Argument to pass: the last+1 message to read)             *
  64.  ***************************************************************/
  65. void TelegramBOT::getUpdates(String offset)  {
  66.    
  67.     Serial.println("GET Update Messages ");
  68.     String command="bot"+_token+"/getUpdates?offset="+offset;
  69.     String mess=connectToTelegram(command);       //recieve reply from telegram.org
  70.     // parsing of reply from Telegram into separate received messages
  71.     int i=0;                //messages received counter
  72.     if (mess!="") {
  73.             Serial.print("Sent Update request messages up to : ");
  74.             Serial.println(offset);
  75.             String a="";
  76.             int ch_count=0;
  77.             String c;
  78.             for (int n=1; n<mess.length()+1; n++) {   //Search for each message start
  79.                 ch_count ++;
  80.                 c =  mess.substring(n-1,n);
  81.                 //Serial.print(c);
  82.                 a=a+c;
  83.                 if (ch_count>8) {
  84.                      if (a.substring(ch_count-9)=="update_id")  {
  85.                          if (i>1) break;
  86.              message[i][0]=a.substring(0, ch_count-11);
  87.                          a=a.substring(ch_count-11);
  88.              i ++;
  89.                          ch_count=11;
  90.                      }
  91.                 }
  92.             }
  93.         if (i==1)  {
  94.             message[i][0]=a.substring(0, ch_count);   //Assign of parsed message into message matrix if only 1 message)
  95.         }
  96.         if (i>1)  i=i-1;
  97.     }
  98.     //check result of parsing process
  99.     if (mess=="") {    
  100.         Serial.println("failed to update");
  101.         return;
  102.     }  
  103.     if (i==0) {
  104.         Serial.println("no new messages");
  105.         Serial.println();
  106.         message[0][0]="0";
  107.     }
  108.     else {
  109.         message[0][0]=String(i);   //returns how many messages are in the array
  110.     //Serial.println();        
  111.     for (int b=1; b<i+1; b++)  {
  112.           Serial.println(message[b][0]);
  113.         }
  114.         Serial.println();
  115.         analizeMessages();
  116.     }
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123. /***********************************************************************
  124.  * SendMessage - function to send message to telegram                  *
  125.  * (Arguments to pass: chat_id, text to transmit and markup(optional)) *
  126.  ***********************************************************************/
  127. void TelegramBOT::sendMessage(String chat_id, String text, String reply_markup)  {
  128.  
  129.     bool sent=false;
  130.    // Serial.println("SEND Message ");
  131.     long sttime=millis();
  132.     if (text!="") {
  133.         while (millis()<sttime+8000) {    // loop for a while to send the message
  134.         String command="bot"+_token+"/sendMessage?chat_id="+chat_id+"&text="+text+"&reply_markup="+reply_markup;
  135.         String mess=connectToTelegram(command);
  136.         //Serial.println(mess);
  137.         int messageLenght=mess.length();
  138.         for (int m=5; m<messageLenght+1; m++)  {
  139.             if (mess.substring(m-10,m)=="{\"ok\":true")     {  //Chek if message has been properly sent
  140.                 sent=true;
  141.                 break;
  142.             }
  143.         }
  144.         if (sent==true)   {
  145.         //  Serial.print("Message delivred: \"");
  146.         //  Serial.print(text);
  147.         //  Serial.println("\"");
  148.         //  Serial.println();
  149.           break;
  150.         }
  151.         delay(1000);
  152.     //  Serial.println("Retry");
  153.  
  154.         }
  155.     }
  156.    // if (sent==false) Serial.println("Message not delivered");
  157. }
  158.  
  159.  
  160.  
  161.  
  162.  
  163. /******************************************************************************
  164.  * AnalizeMessage - function to achieve message parameters from json structure *
  165.  ******************************************************************************/
  166. void TelegramBOT::analizeMessages(void)     {
  167.  
  168.   int rif[6]= {0,0,0,0,0,0}; //pointers in message json (update_id, name_id, name, lastname, chat_id, text)
  169.   for (int i=1; i<message[0][0].toInt()+1; i++)      {
  170.     int messageLenght=message[i][0].length();
  171.     String a=message[i][0];
  172.     message[i][5]="";
  173.     for (int m=5; m<messageLenght+1; m++)             {
  174.         if (a.substring(m-12,m)=="\"update_id\":")     { //Search for "update_id" pointer start
  175.           rif[0]=m;
  176.         }
  177.         if (a.substring(m-13,m)=="\"from\":{\"id\":")  { //Search for "from" pointer start
  178.           rif[1]=m;
  179.         }
  180.         if (a.substring(m-14,m)=="\"first_name\":\"")  { //Search for "first_name" pointer start
  181.           rif[2]=m;
  182.         }
  183.         if (a.substring(m-13,m)=="\"last_name\":\"")   { //Search for "last_name" pointer start
  184.           rif[3]=m;
  185.         }
  186.         if (a.substring(m-13,m)=="\"chat\":{\"id\":")  { //Search for "chat" pointer start
  187.           rif[4]=m;
  188.         }
  189.         if (a.substring(m-8,m)=="\"text\":\"")         { //Search for "text" pointer start
  190.       rif[5]=m;
  191.         }
  192.         for (int n=0; n<2; n++)     {                    //Search for "update_id" and "from" pointers end
  193.             if (a.substring(m-1,m)==",")  {
  194.                 if (rif[n]!=0)  {
  195.                   message[i][n]=a.substring(rif[n],m-1);
  196.                 }
  197.             rif[n]=0;
  198.             }
  199.         }
  200.         if (a.substring(m-1,m)==",")  {                 //Search for "first name" pointer end
  201.               if (rif[2]!=0)  {
  202.                 message[i][2]=a.substring(rif[2],m-2);  //Write value into dedicated slot
  203.               }
  204.           rif[2]=0;
  205.         }
  206.         if (a.substring(m-1,m)==",")  {                 //Search for "last name" pointer end
  207.               if (rif[3]!=0)  {
  208.                 message[i][3]=a.substring(rif[3],m-3);  //Write value into dedicated slot
  209.               }
  210.           rif[3]=0;
  211.         }
  212.         if (a.substring(m-1,m)==",")  {                 //Search for "chat" pointer end
  213.               if (rif[4]!=0)  {
  214.                 message[i][4]=a.substring(rif[4],m-1);  //Write value into dedicated slot
  215.               }
  216.           rif[4]=0;
  217.         }
  218.         if (a.substring(m-2,m)=="\",")  {               //Search for "text" pointer end
  219.               if (rif[5]!=0)  {
  220.                 message[i][5]=a.substring(rif[5],m-2);    //Write value into dedicated slot
  221.             }
  222.           rif[5]=0;
  223.         }
  224.     if (a.substring(m-2,m)=="\"}")  {               //Search for "text" pointer end
  225.               if (rif[5]!=0)  {
  226.                 message[i][5]=a.substring(rif[5],m-2);    //Write value into dedicated slot
  227.             }
  228.           rif[5]=0;
  229.         }    
  230.     }
  231.     int id=message[message[0][0].toInt()][0].toInt()+1;
  232.     message[0][1]=id;                                   //Write id of last read message
  233.    
  234.   //  for (int j=0; j<6; j++)   {
  235.   //    Serial.println(message[i][j]);                                //print parsed data
  236.   //  }
  237.   }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement