Advertisement
Guest User

Untitled

a guest
Apr 14th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.10 KB | None | 0 0
  1.  
  2. /*
  3.   Copyright (c) 2015 Giancarlo Bacchio. All right reserved.
  4.  
  5.   TelegramBot - Library to create your own Telegram Bot using
  6.   ESP8266 on Arduino IDE.
  7.   Ref. Library at https:github/esp8266/Arduino
  8.  
  9.   This library is free software; you can redistribute it and/or
  10.   modify it under the terms of the GNU Lesser General Public
  11.   License as published by the Free Software Foundation; either
  12.   version 2.1 of the License, or (at your option) any later version.
  13.  
  14.   This library is distributed in the hope that it will be useful,
  15.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17.   Lesser General Public License for more details.
  18.  
  19.   You should have received a copy of the GNU Lesser General Public
  20.   License along with this library; if not, write to the Free Software
  21.   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22.   */
  23.  
  24.  
  25. #include "ESP8266TelegramBOT.h"
  26.  
  27.  
  28. TelegramBOT::TelegramBOT(String token, String name, String username)    {
  29.     _token=token;
  30.     _name=name;
  31.   _username=username;
  32. }
  33.  
  34.  
  35. void TelegramBOT::begin(void)   {
  36.       message[0][0]="0";   // number of received messages
  37.       message[1][0]="";    
  38.     message[0][1]="0";  // code of last read Message
  39. }
  40.  
  41.  
  42.  
  43. /**************************************************************************************************
  44.  * function to achieve connection to api.telegram.org and send command to telegram                *
  45.  * (Argument to pass: URL to address to Telegram)                                                 *
  46.  **************************************************************************************************/
  47. String TelegramBOT::connectToTelegram(String command)  {
  48.     String mess="";
  49.     long now;
  50.     bool avail;
  51.     // Connect with api.telegram.org      
  52.     IPAddress server(149,154,167,198);
  53.     if (client.connect(server, 443)) {  
  54.         //Serial.println(".... connected to server");
  55.         String a="";
  56.         char c;
  57.     int ch_count=0;
  58.         client.println("GET /"+command);
  59.         now=millis();
  60.         avail=false;
  61.         while (millis()-now<1500) {  
  62.             while (client.available()) {
  63.           char c = client.read();
  64.               //Serial.write(c);
  65.               if (ch_count<700)  {
  66.                  mess=mess+c;
  67.              ch_count++;
  68.           }
  69.               avail=true;
  70.         }
  71.             if (avail) {
  72.         //Serial.println();
  73.         //Serial.println(mess);
  74.         //Serial.println();
  75.         break;
  76.         }
  77.         }
  78.     }
  79.     return mess;
  80. }
  81.  
  82.  
  83.  
  84.  
  85. /***************************************************************
  86.  * GetUpdates - function to receive all messages from telegram *
  87.  * (Argument to pass: the last+1 message to read)             *
  88.  ***************************************************************/
  89. void TelegramBOT::getUpdates(String offset)  {
  90.    
  91.     Serial.println("GET Update Messages ");
  92.     String command="bot"+_token+"/getUpdates?offset="+offset;
  93.     String mess=connectToTelegram(command);       //recieve reply from telegram.org
  94.     // parsing of reply from Telegram into separate received messages
  95.     int i=0;                //messages received counter
  96.     if (mess!="") {
  97.             Serial.print("Sent Update request messages up to : ");
  98.             Serial.println(offset);
  99.             String a="";
  100.             int ch_count=0;
  101.             String c;
  102.             for (int n=1; n<mess.length()+1; n++) {   //Search for each message start
  103.                 ch_count ++;
  104.                 c =  mess.substring(n-1,n);
  105.                 //Serial.print(c);
  106.                 a=a+c;
  107.                 if (ch_count>8) {
  108.                      if (a.substring(ch_count-9)=="update_id")  {
  109.                          if (i>1) break;
  110.              message[i][0]=a.substring(0, ch_count-11);
  111.                          a=a.substring(ch_count-11);
  112.              i ++;
  113.                          ch_count=11;
  114.                      }
  115.                 }
  116.             }
  117.         if (i==1)  {
  118.             message[i][0]=a.substring(0, ch_count);   //Assign of parsed message into message matrix if only 1 message)
  119.         }
  120.         if (i>1)  i=i-1;
  121.     }
  122.     //check result of parsing process
  123.     if (mess=="") {    
  124.         Serial.println("failed to update");
  125.         return;
  126.     }  
  127.     if (i==0) {
  128.         Serial.println("no new messages");
  129.         Serial.println();
  130.         message[0][0]="0";
  131.     }
  132.     else {
  133.         message[0][0]=String(i);   //returns how many messages are in the array
  134.     //Serial.println();        
  135.     for (int b=1; b<i+1; b++)  {
  136.           Serial.println(message[b][0]);
  137.         }
  138.         Serial.println();
  139.         analizeMessages();
  140.     }
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147. /***********************************************************************
  148.  * SendMessage - function to send message to telegram                  *
  149.  * (Arguments to pass: chat_id, text to transmit and markup(optional)) *
  150.  ***********************************************************************/
  151. void TelegramBOT::sendMessage(String chat_id, String text, String reply_markup)  {
  152.  
  153.     bool sent=false;
  154.    // Serial.println("SEND Message ");
  155.     long sttime=millis();
  156.     if (text!="") {
  157.         while (millis()<sttime+8000) {    // loop for a while to send the message
  158.         String command="bot"+_token+"/sendMessage?chat_id="+chat_id+"&text="+text+"&reply_markup="+reply_markup;
  159.         String mess=connectToTelegram(command);
  160.         //Serial.println(mess);
  161.         int messageLenght=mess.length();
  162.         for (int m=5; m<messageLenght+1; m++)  {
  163.             if (mess.substring(m-10,m)=="{\"ok\":true")     {  //Chek if message has been properly sent
  164.                 sent=true;
  165.                 break;
  166.             }
  167.         }
  168.         if (sent==true)   {
  169.         //  Serial.print("Message delivred: \"");
  170.         //  Serial.print(text);
  171.         //  Serial.println("\"");
  172.         //  Serial.println();
  173.           break;
  174.         }
  175.         delay(1000);
  176.     //  Serial.println("Retry");
  177.  
  178.         }
  179.     }
  180.    // if (sent==false) Serial.println("Message not delivered");
  181. }
  182.  
  183.  
  184.  
  185.  
  186.  
  187. /******************************************************************************
  188.  * AnalizeMessage - function to achieve message parameters from json structure *
  189.  ******************************************************************************/
  190. void TelegramBOT::analizeMessages(void)     {
  191.  
  192.   int rif[6]= {0,0,0,0,0,0}; //pointers in message json (update_id, name_id, name, lastname, chat_id, text)
  193.   for (int i=1; i<message[0][0].toInt()+1; i++)      {
  194.     int messageLenght=message[i][0].length();
  195.     String a=message[i][0];
  196.     message[i][5]="";
  197.     for (int m=5; m<messageLenght+1; m++)             {
  198.         if (a.substring(m-12,m)=="\"update_id\":")     { //Search for "update_id" pointer start
  199.           rif[0]=m;
  200.         }
  201.         if (a.substring(m-13,m)=="\"from\":{\"id\":")  { //Search for "from" pointer start
  202.           rif[1]=m;
  203.         }
  204.         if (a.substring(m-14,m)=="\"first_name\":\"")  { //Search for "first_name" pointer start
  205.           rif[2]=m;
  206.         }
  207.         if (a.substring(m-13,m)=="\"last_name\":\"")   { //Search for "last_name" pointer start
  208.           rif[3]=m;
  209.         }
  210.         if (a.substring(m-13,m)=="\"chat\":{\"id\":")  { //Search for "chat" pointer start
  211.           rif[4]=m;
  212.         }
  213.         if (a.substring(m-8,m)=="\"text\":\"")         { //Search for "text" pointer start
  214.       rif[5]=m;
  215.         }
  216.         for (int n=0; n<2; n++)     {                    //Search for "update_id" and "from" pointers end
  217.             if (a.substring(m-1,m)==",")  {
  218.                 if (rif[n]!=0)  {
  219.                   message[i][n]=a.substring(rif[n],m-1);
  220.                 }
  221.             rif[n]=0;
  222.             }
  223.         }
  224.         if (a.substring(m-1,m)==",")  {                 //Search for "first name" pointer end
  225.               if (rif[2]!=0)  {
  226.                 message[i][2]=a.substring(rif[2],m-2);  //Write value into dedicated slot
  227.               }
  228.           rif[2]=0;
  229.         }
  230.         if (a.substring(m-1,m)==",")  {                 //Search for "last name" pointer end
  231.               if (rif[3]!=0)  {
  232.                 message[i][3]=a.substring(rif[3],m-3);  //Write value into dedicated slot
  233.               }
  234.           rif[3]=0;
  235.         }
  236.         if (a.substring(m-1,m)==",")  {                 //Search for "chat" pointer end
  237.               if (rif[4]!=0)  {
  238.                 message[i][4]=a.substring(rif[4],m-1);  //Write value into dedicated slot
  239.               }
  240.           rif[4]=0;
  241.         }
  242.         if (a.substring(m-2,m)=="\",")  {               //Search for "text" pointer end
  243.               if (rif[5]!=0)  {
  244.                 message[i][5]=a.substring(rif[5],m-2);    //Write value into dedicated slot
  245.             }
  246.           rif[5]=0;
  247.         }
  248.     if (a.substring(m-2,m)=="\"}")  {               //Search for "text" pointer end
  249.               if (rif[5]!=0)  {
  250.                 message[i][5]=a.substring(rif[5],m-2);    //Write value into dedicated slot
  251.             }
  252.           rif[5]=0;
  253.         }    
  254.     }
  255.     int id=message[message[0][0].toInt()][0].toInt()+1;
  256.     message[0][1]=id;                                   //Write id of last read message
  257.    
  258.   //  for (int j=0; j<6; j++)   {
  259.   //    Serial.println(message[i][j]);                                //print parsed data
  260.   //  }
  261.   }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement