humanemulator.net — справка. Продукт, демо и покупка — на хуманэмулятор.рф | Демо | Купить | API | лог изменений

get_last_messages

get_last_messages($chat_id,$count=1); - Получить последние сообщения чата

Функция на вход принимает параметры:

  • $chat_id – ID чата
  • $count – лимит сообщений

    После выполнения функции возвращаемое значение будет равно одному из двух :
  • результат – массив объектов Message https://corefork.telegram.org/type/Message или null если произошла ошибка

    Пример использования
    <?php
    // Scenario: Demonstrates how to get Telegram last messages
    
    $xhe_host = getenv("RPABOT_HOST_URL");
    if (!isset($path)) {
    	// Path to the init.php file for connecting to the XHE API
    	$path = "../../../Templates/init.php";
    	// Including init.php grants access to all classes and functionality for working with the XHE API
    	require($path);
    }
    
    // beginning
    echo "\n<font color=blue>telegram->get_last_messages</font>\n";
    
    // get from: https://core.telegram.org/api
    $appID = "xxx";
    $appHash = "xxx";
    $myPhoneNumber = "+7xxx";
    $password = "xxx!";
    
    // step 1
    echo ("1. Connect to telegram: ");
    echo (WEB::$telegram->connect($appID, $appHash));
    
    // step 2
    echo ("\n2. Request authorization: ");
    $res = WEB::$telegram->request_authorization($myPhoneNumber);
    echo ($res);
    
    if ($res != "connected") {
    	// verification code input
    	if ($res == "verification_code") {
    		$auth_code = WINDOW::$app->get_dlg_input_string("dlg_1", "Enter authorization code from SMS or chat:");
    
    		// step 3
    		echo ("\n3. Authorize: ");
    		$res = WEB::$telegram->authorization($myPhoneNumber, $res, $auth_code);
    		echo ($res);
    	}
    
    	// password input
    	if ($res == "password") {
    		$auth_code = WINDOW::$app->get_dlg_input_string("dlg_1", "Enter password:");
    
    		// step 3
    		echo ("\n3. Authorize: ");
    		$res = WEB::$telegram->authorization($myPhoneNumber, $res, $password);
    		echo ($res);
    	}
    
    	if ($res != "ok") {
    		echo ("\n4. Authorization failed: ");
    		WINDOW::$app->quit();
    	}
    }
    
    // step 5
    echo ("\n5. Get channels: ");
    $channels = WEB::$telegram->get_chats("chanel");
    echo (count($channels));
    
    // step 6
    $channel_index = 13;
    $channel_id = $channels[$channel_index]->id;
    $channel_title = $channels[$channel_index]->title;
    
    // Example 1
    echo ("\n6. Get 2 last messages in channel $channel_title: ");
    $limit = 2;
    $messages = WEB::$telegram->get_last_messages($channel_id, $limit);
    echo (count($messages));
    $messages_json = json_encode($messages);
    SYSTEM::$textfile->write_file("d:/messages.res", $messages_json);
    WINDOW::$app->shell_execute("open", "d:/messages.res");
    
    // step 7
    echo ("\n7. Disconnect: ");
    echo (WEB::$telegram->disconnect());
    
    // end
    echo "\n";
    
    // Quit
    WINDOW::$app->quit();
    # Additional paths
    import sys
    sys.path.insert(0, '../../../Templates PY/')
    
    xhe_host = "127.0.0.1:7040"
    from xweb_human_emulator import *
    
    # начало
    echo("<hr><font color=blue>seo.xxxxxxxxx</font><hr>")
    
    # получить можно тут : https://core.telegram.org/api
    appID = "xxx";
    appHash = "xxx";
    myPhoneNumber="+7xxx";
    password="xxx"
    
    # 1 шаг
    echo("1. Соединится с телеграм : ");
    echo(telegram.connect(appID, appHash));
    
    # 2 шаг
    echo("\n2. Запросить авторизацию : ");
    res = telegram.request_authorization(myPhoneNumber);
    echo(res);
    
    if res!="connected":
    	# ввод кода верификации
    	if res=="verification_code":
    		auth_code=app.get_dlg_input_string("dlg_1","Введите код авторизации из смс или чата:");
    
    		# 3 шаг
    		echo("\n3. Авторизоваться : ");
    		res = telegram.authorization(myPhoneNumber,res,auth_code);
    		echo(res);
    
    	# ввод пароля
    	if res=="password":
    		auth_code=app.get_dlg_input_string("dlg_1","Введите пароль:");
    
    		# 3 шаг
    		echo("\n3. Авторизоваться : ");
    		res = telegram.authorization(myPhoneNumber,res,password);
    		echo(res);	
    
    	if res!="ok":
    		echo("\n4. Не удалось авторизоваться : ");
    		app.quit();
    
    # 5 шаг
    echo("\n5. Получить каналы : ");
    chanels = telegram.get_chats("chanel");
    echo (len(chanels));
     
    # 6 шаг
    id =chanels[13]["id"];
    title=chanels[13]["title"];
    echo("\n6. Получить 2 последних сообщения в канале "+title +": ");
    messages = telegram.get_last_messages(id,2);
    echo(len(messages));
    textfile.write_file("d:/messages.res",json.dumps(messages));
    app.shell_execute("open","d:/messages.res");
    
    # 7 шаг
    echo("\n7. Отсоеденится: ");
    echo (telegram.disconnect());
    
    # конец
    echo("<hr><br>")
    
    # Quit
    app.quit()
    #region using
    
    using System;
    using System.Diagnostics;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using System.Text;
    using System.Threading;
    
    using XHE;
    using XHE.XHE_DOM;
    using XHE.XHE_System;
    using XHE.XHE_Window;
    using XHE.XHE_Web;
    
    using Newtonsoft.Json.Linq;
     
    #endregion
    
     class Program:XHEScript
     {
    	  static void Main(string[] args)
    	  {
    			// init XHE
    			server = Environment.GetEnvironmentVariable("RPABOT_HOST_URL");
    			InitXHE();
    
    			// начало
    			echo("<hr><font color=blue>submitter.generate_random_city</font><hr>");
    				
    			// получить можно тут : https://core.telegram.org/api
    			var appID = "xxx";
    			var appHash = "xxx";
    			var myPhoneNumber="+7xxx";
    			var password="xxx";
    
    			// 1 шаг
    			echo("1. Соединится с телеграм : ");
    			echo(telegram.connect(appID, appHash));
    
    			// 2 шаг
    			echo("\n2. Запросить авторизацию : ");
    			var res = telegram.request_authorization(myPhoneNumber);
    			echo(res);
    
    			if (res!="connected")
    			 {
    				// ввод кода верификации
    				if (res=="verification_code")
    				 {
    					var auth_code=app.get_dlg_input_string("dlg_1","Введите код авторизации из смс или чата:");
    
    					// 3 шаг
    					echo("\n3. Авторизоваться : ");
    					var res1 = telegram.authorization(myPhoneNumber,res,auth_code);
    					echo(res1);
    				}
    
    				// ввод пароля
    				if (res=="password")
    				 {
    					var auth_code=app.get_dlg_input_string("dlg_1","Введите пароль:");
    
    					// 3 шаг
    					echo("\n3. Авторизоваться : ");
    					var res1 = telegram.authorization(myPhoneNumber,res,password);
    					echo(res1);
    				}
    
    				if (res!="ok")
    				{
    					echo("\n4. Не удалось авторизоваться : ");
    					app.quit();
    				}
    			}
    
    			// 5 шаг
    			echo("\n5. Получить каналы : ");
    			var chanels = telegram.get_chats("chanel");
    			echo (chanels.Count);
    			 
    			// 6 шаг
    			var id =Convert.ToInt32(chanels[13]["id"]);
    			var title=chanels[13]["title"];
    			echo("\n6. Получить 2 последних сообщения в канале "+title+" : ");
    			var messages = telegram.get_last_messages(id,2);
    			echo(messages.Count);
    			textfile.write_file("d:/messages.res",JArray.FromObject(messages).ToString());
    			app.shell_execute("open","d:/messages.res");
    
    			// 7 шаг
    			echo("\n7. Отсоеденится: ");
    			echo (telegram.disconnect());
    
    			// конец
    			echo("\n\n");
    
    			app.quit();            
    	  }
    }
    // подключим функциональные объекты, если еще не подключен
    xhe_host="127.0.0.1:7040";
    echo=require("../../../Templates JS/init.js");
    
    // начало
    echo("<hr><font color=blue>seo.get_alexa_rank</font><hr>");
    
    // получить можно тут : https://core.telegram.org/api
    appID = "xxx";
    appHash = "xxx";
    myPhoneNumber="xxx";
    password="xxx"
    
    // 1 шаг
    echo("1. Соединится с телеграм : ");
    echo(telegram.connect(appID, appHash));
    
    // 2 шаг
    echo("\n2. Запросить авторизацию : ");
    res = telegram.request_authorization(myPhoneNumber);
    echo(res);
    
    if (res!="connected")
     {
    	// ввод кода верификации
    	if (res=="verification_code")
    	 {
    		auth_code=app.get_dlg_input_string("dlg_1","Введите код авторизации из смс или чата:");
    
    		// 3 шаг
    		echo("\n3. Авторизоваться : ");
    		res = telegram.authorization(myPhoneNumber,res,auth_code);
    		echo(res);
    	}
    
    	// ввод пароля
    	if (res=="password")
    	 {
    		auth_code=app.get_dlg_input_string("dlg_1","Введите пароль:");
    
    		// 3 шаг
    		echo("\n3. Авторизоваться : ");
    		res = telegram.authorization(myPhoneNumber,res,password);
    		echo(res);
    	}
    
    	if (res!="ok")
    	{
    		echo("\n4. Не удалось авторизоваться : ");
    		app.quit();
    	}
    }
    	
    // 5 шаг
    echo("\n5. Получить каналы : ");
    chanels = telegram.get_chats("chanel");
    echo (chanels.length);
     
    // 6 шаг
    id =chanels[13].id;
    title=chanels[13].title;
    echo("\n6. Получить 2 последних сообщения в канале  title : ");
    messages = telegram.get_last_messages(id,2);
    echo(messages.length);
    textfile.write_file("d:/messages.res",JSON.stringify(messages));
    app.shell_execute("open","d:/messages.res");
    
    // 7 шаг
    echo("\n7. Отсоеденится: ");
    echo (telegram.disconnect());
    
    // конец
    echo("\n");
    
    // Quit
    app.quit();

    =============================================
    Telegram    Объекты    DOM  System  Vision  Web  Window        
    =============================================
    если что-то непонятно или необходимо узнать или считаете что надо добавить по работе этой функции, пишите в комментарии или на наш форум
    .