Italian (IT)

This post is made to teach you how to create and set up a simple fully functional Telegram bot written in pure PHP.
There are many good ready to use SDKs written in PHP.
This is only a simple tutorial that explains how to interact with the Telegram APIs without use of any SDK.

Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and inline requests.
At the core, Telegram Bots are special accounts that do not require an additional phone number to set up.
Messages, commands and requests sent by users are passed to the software running on your servers.
Telegram intermediary server handles all encryption and communication with the Telegram API for you.
You communicate with this server via a simple HTTPS-interface that offers a simplified version of the Telegram API.
Telegram Bot API is an HTTP-based interface created for developers keen on building bots for Telegram.

How do I create a bot? There's a... bot for that.
BotFather is the one bot to rule them all. It will help you create new bots and change settings for existing ones.
Message @BotFather with the /newbot command to create a new bot. The BotFather will ask you for a name and username, then generate an authorization token for your new bot.
If you don't know how to message by username, click the search field on your Telegram app and type @BotFather, where you should be able to initiate a conversation. Be careful not to send it to the wrong contact, because some users have similar usernames to BotFather.

The name of your bot is displayed in contact details and elsewhere.

The Username is a short name, to be used in mentions and telegram.me links. Usernames are 5-32 characters long and are case insensitive, but may only include Latin characters, numbers, and underscores. Your bot's username must end in ‘bot’, e.g. ‘tetris_bot’ or ‘TetrisBot’.

The token is a string along the lines of 110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw that is required to authorize the bot and send requests to the Bot API. Keep your token secure and store it safely, it can be used by anyone to control your bot.
If your existing token is compromised or you lost it for some reason, use the /token command to generate a new one.

In order to make your Bot answering to requests from your Telegram users you can register a WebHook to automatically being called once updates are available.
The quickest and easiest way to set a WebHook for your Bot is to issue a GET request to the Bot API.
All you have to do is to call the setWebHook method in the Bot API via the following url write in your browser

https://api.telegram.org/bot{my_bot_token}/setWebhook?url={url_to_send_updates_to}


where
my_bot_token is the token you got from BotFather when you created your Bot
url_to_send_updates_to is the url of the piece of code you wrote to implement your Bot behavior (in order to set a Webhook you need a server with HTTPS)
For instance

https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/setWebhook?url=https://www.example.com/my-telegram-bot.php


And you’ve got it.
Now if you go to the following url (you have to replace {my_bot_token} with your Bot Token)

https://api.telegram.org/bot{my_bot_token}/getWebhookInfo


you should see something like this


{
    "ok":true,
    "result": 
    {
        "url":"https://www.example.com/my-telegram-bot.php",
        "has_custom_certificate":false,
        "pending_update_count":0,
        "max_connections":40
    }
}
                

Now let's explain how to develop the bot PHP file.
In your favorite editor, create a my-telegram-bot.php file with the following content.


<?php
$botToken = "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11";
$botAPI = "https://api.telegram.org/bot" . $botToken;
                        
$update = file_get_contents('php://input');
$update = json_decode($update, TRUE);
                        
if (!$update) {
    exit;
}
$message = isset($update['message']) ? $update['message'] : "";
                

where
$botToken is the bot token, generated by BotFather for your bot and used to register the bot on the Webhook service
$botAPI is the URL used to send commands to the Telegram API
file_get_contents('php://input') handle the call incoming from Telegram server
json_decode($update, TRUE) decode communication message in a JSON structure
if (!$update) { exit; } if it's not a valid JSON command, it exits the script
$message if it's a valid command, it extracts the sent message

The script can already be published; it still does nothing but works already.
Now let's add some lines to extract useful data from the message incoming


$messageId = isset($message['message_id']) ? $message['message_id'] : "";

$messageText = isset($message['text']) ? $message['text'] : "";
$messageText = trim($messageText);
$messageText = strtolower($messageText);
                        
$date = isset($message['date']) ? $message['date'] : "";
                        
$chatId = isset($message['chat']['id']) ? $message['chat']['id'] : "";
$username = isset($message['chat']['username']) ? $message['chat']['username'] : "";
$firstname = isset($message['chat']['first_name']) ? $message['chat']['first_name'] : "";
$lastname = isset($message['chat']['last_name']) ? $message['chat']['last_name'] : "";
                

where
$messageId is the unique identification number of the chat message
$messageText if it is a text message, the text contained in the message sent
$date is the date the message was sent
$chatId is the unique id of the user who sent the message
$username is the username of the user who sent the message
$firstname is the firs tname of the user who sent the message
$lastname is the last name of the user who sent the message

Message is not necessarily a text, it is possible to managed the type of incoming message with the following lines


if (isset($message['text'])) {
    $response = "Received a text message: " . $message['text'];
} elseif (isset($message['audio'])) {
    $response = "Received an audio message";
} elseif (isset($message['document'])) {
    $response = "Received a document message";
} elseif (isset($message['photo'])) {
    $response = "Received a photo message";
} elseif (isset($message['sticker'])) {
    $response = "Received a sticker message";
} elseif (isset($message['video'])) {
    $response = "Received a video message";
} elseif (isset($message['voice'])) {
    $response = "Received a voice message";
} elseif (isset($message['contact'])) {
    $response = "Received a contact message";
} elseif (isset($message['location'])) {
    $response = "Received a location message";
} elseif (isset($message['venue'])) {
    $response = "Received a venue message";
} else {
    $response = "I received a message?";
}
                

When a photo is sent to the bot, you can retrieve the file and its info from Telegram API.


if (isset($message['photo'])) {
    $fileAPI = "https://api.telegram.org/file/bot" . $botToken;
    $url = $GLOBALS[botAPI] . '/getFile?file_id=' . $message['photo'][0]['file_id'];
    $fileinfo = file_get_contents($url);
    $fileinfo = json_decode($fileinfo, TRUE);                          
    $filePath = $fileinfo['result']['file_path'];
    $url = $GLOBALS[fileAPI] . '/' . $filePath;
                          
    $imgData = "File size: " . $message['photo'][0]['file_size'] . "byte" . chr(10)
        . "Width: " . $message['photo'][0]['width'] . "px" . chr(10)
        . "Height: " . $message['photo'][0]['height'] . "px" . chr(10)
        . "URL: " . $url);
}
                

where
$fileAPI is the URL used to retrieve file info from Telegram API
$fileinfo the script performs an HTTP GET call to get file info in JSON format
$filePath the script get the URL path of file
$message['photo'][0]['file_size'] is the size of the first file in byte
$message['photo'][0]['width'] and $message['photo'][0]['height'] are the dimension of the first file in pixels

Bot can send several type of message to the chat

Text messages


$url = $GLOBALS[botAPI] . '/sendMessage?chat_id=' . $chatId . '&text=' . urlencode($message);
file_get_contents($url);
                

Photo messages


$url = $GLOBALS[botAPI] . '/sendPhoto?chat_id=' . $chatId . '&photo=' . urlencode("https://webnoos.altervista.org/noos.jpeg");
file_get_contents($url);
                

Location messages


$url = $GLOBALS[botAPI] . '/sendLocation?chat_id=' . $chatId . '&latitude=' . urlencode("45.1") . '&longitude=' . urlencode("11.3");
file_get_contents($url);
                

Telegram also features the ability to display keyboards in chat like this


header("Content-Type: application/json");
$parameters = array('chat_id' => $chatId, "text" => $messageText);
$parameters["method"] = "sendMessage";
$parameters["reply_markup"] = '{ "keyboard": [["uno", "due"], ["tre"], ["quattro", "5", "6"]], "one_time_keyboard": false}';
echo json_encode($parameters);
                

The current keyboard can be removed from the chat in this way


header("Content-Type: application/json");
$parameters = array('chat_id' => $chatId, "text" => $messageText);
$parameters["method"] = "sendMessage";
$parameters["reply_markup"] = '{ "remove_keyboard" : true}';
echo json_encode($parameters);
                

You can also display an inline keyboard


header("Content-Type: application/json");
$parameters = array('chat_id' => $chatId, "text" => $messageText);
$parameters["method"] = "sendMessage";
$keyboard = ['inline_keyboard' => [[
    ['text' =>  'Vai su Google', 'url' => 'https://www.google.com'],
    ['text' => 'Vai su Microsoft', 'url' => 'https://www.microsoft.com']
]]];
$parameters["reply_markup"] = json_encode($keyboard, true);
echo json_encode($parameters);
                

Finally, you have the ability to manage messages sent to the bot as a series of commands, to do different things, as in this example


switch ($messageText) {
    case "/hi":
        // TODO
        break;
                          
    default:
        // TODO
}
                

This little starting tutorial ends here.

This is a short step-by-step tutorial to distribute a .NET Core application in a docker image.
I’ll cover how you can use Docker to run your application in an isolated, minimal environment with fewer moving parts.

To build and distribute a .NET Core application in Docker, first we need an application to Dockerize and some prerequisites.

In this tutorial, we'll perform the steps from a Terminal, like Windows command prompt.

First, create a working folder for our exercise

mkdir NetCoreDocker


Check .NET Core version installed on your computer

dotnet --info


Check Docker version installed on your computer

docker version


Place a file named global.json in exercise directory with the following content


{ 
    "sdk": { 
        "version": "3.0.100" 
    } 
}
                

This will force the compiler to use 3.0 version of the SDK

Then create .NET Core console application

dotnet new console -o app -n myapp


The folder structure will be similar to the following example


NetCoreDocker 
│   global.json 
│ 
└───app 
    │   myapp.csproj 
    │   Program.cs 
    │ 
    └───obj 
        myapp.csproj.nuget.cache 
        myapp.csproj.nuget.g.props 
        myapp.csproj.nuget.g.targets 
        project.assets.json
                

.NET Core creates a console application that is executed, then terminated.
We modify the application that runs continuously instead.

Modify Program.cs as below


using System; 
 
namespace myapp 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            var counter = 0; 
            var max = args.Length != 0 ? Convert.ToInt32(args[0]) : -1; 
            while(max == -1 || counter < max) 
            { 
                counter++; 
                Console.WriteLine($"Counter: {counter}"); 
                System.Threading.Tasks.Task.Delay(1000).Wait(); 
            } 
        } 
    } 
}
                

Move in app sub-folder and test application

dotnet run


Finally, publish the application

dotnet publish -c Release


Application will be published in this folder

NetCoreDocker\app\bin\Release\netcoreapp3.0\publish


Place a file named Dockerfile in the working folder with the following content


FROM mcr.microsoft.com/dotnet/core/runtime:3.0
COPY app\bin\Release\netcoreapp3.0\publish app/ 
ENTRYPOINT ["dotnet", "app/myapp.dll"]
                

Now we can build Docker image of our application

docker build -t myimage -f Dockerfile .


Finally, verify the right build of the image

docker images


Final notice

If you want to try starting from the source code of your application (contained in the /src folder, not in the /app folder like now) and compile directly on a Docker image, change the Dockerfile contents with this multistage builds


FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
WORKDIR /src
COPY ["myapp.csproj", "./"]
RUN dotnet restore "./myapp.csproj"
COPY . .
RUN dotnet build "myapp.csproj" -c Release -o /app
                        
FROM build AS publish
RUN dotnet publish "myapp.csproj" -c Release -o /app
                        
FROM mcr.microsoft.com/dotnet/core/runtime:3.0 AS base
WORKDIR /app
EXPOSE 5001
                        
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "myapp.dll"]
                

Taken from: http://enyojs.com

Enyo 2 has its roots in the framework developed to power the applications on HP’s TouchPad, an innovative device powered by webOS that embraced the web stack as its primary application environment. Over a thousand apps, including the stock applications that shipped with the TouchPad, were built using Enyo 1. Although Enyo was conceived from the start as platform-independent framework, Enyo 1 out of necessity targeted webOS and the TouchPad specifically.

The basics Create an empty gadget.xml file with Notepad and save it. In the Encoding drop-down, select UTF-8. Create an empty MyGadget.html file with Notepad and save it. In the Encoding drop-down, select UTF-8.

Create the manifest gadget.xml file and save it to folder.

<?xml version="1.0" encoding="utf-8" ?>
<gadget>
 <name>MyGadget</name>
 <version>1.0.0.0</version>
 <hosts>
 <host name="sidebar">
 <base type="HTML" apiVersion="1.0.0" src="/MyGadget.html" />
 <permissions>Full</permissions>
 <platform minPlatformVersion="1.0" />
 </host>
 </hosts>
</gadget>

Tratto da: openoffice.org

Supponete di dover inserire ripetutamente sempre le stesse informazioni. Sebbene sia possibile memorizzare le informazioni negli appunti, se questi nel frattempo vengono utilizzati, il loro contenuto cambia. Una semplice soluzione consiste nel memorizzare i dati in una macro (in alcuni semplici casi, come l'esempio utilizzato in questa sede, una soluzione migliore consiste nell'utilizzo del completamento automatico).

New-Mailbox -Name 'First and Last Name' -Alias 'MailboxAlias'
 -OrganizationalUnit 'MyDomain/Users'
 -UserPrincipalName ' [email protected]' -SamAccountName 'MailboxAlias'
 -FirstName 'First Name' -Initials '' -LastName 'Last Name'
 -Password 'System.Security.SecureString' -ResetPasswordOnNextLogon $false
 -Database 'MyMailServer\First Storage Group\Mailbox Database'

 

Page 1 of 8

© 2007 - 2019 Bruno Tessaro. My life in the web. All right reserved.

This website uses cookies to ensure you get the best experience on our website.