Telegram + 1C + Webhooks + Apache + Self-Signed Certificate

Many lines are written about the integration of Telegram and 1C. But nowhere did I see the complete instructions for installing and configuring webhooks. I'll try to write it.

For all this we need (or it will be more correct to say what was used by me):

  1. Apache 2.2.24
  2. OpenSSL (included with Apache installation)
  3. 1C (with web server modules)
  4. Own domain
  5. Created bot in Telegram (I will not describe its creation, because it is quite trivial)

It is assumed that all the software you have installed.

So, let's start by getting a certificate. Open the command line and execute the following code:

openssl req -newkey rsa:2048 -sha256 -nodes -keyout YOURPRIVATE.key -x509 -days 365 -out YOURPUBLIC.pem -subj "/C=US/ST=New York/L=Brooklyn/O=Example Brooklyn Company/CN=YOURDOMAIN.EXAMPLE"

Where:

YOURPRIVATE.key is the private key of the certificate. It will be used in the

apache YOURPUBLIC.pem - the public key of the certificate. Will be used when registering a webhook

YOURDOMAIN.EXAMPLE - the address of your domain with a webhook. It must still match the webhook address !!!

After executing this code, the key files will appear in the openssl folder (I have this “C: \ Program Files \ Apache Software Foundation \ Apache2.2 \ bin").

I copied them to the Apache conf folder.

Let's move on to configuring Apache.

I have seen many different ways. The following worked for me:

The following lines were added to httpd.conf:

Listen 443 

so that the Apache “listens” to port 443.

The <IfModule ssl_module> block has the following form:

<IfModule ssl_module>
SSLMutex default
SSLSessionCache none
</IfModule>

At the very end, lines are added where I indicate the path to the certificate:

SSLEngine On
SSLCertificateFile conf/YOURPUBLIC.pem
SSLCertificateKeyFile conf/YOURPRIVATE.key

And uncomment the line:

LoadModule ssl_module modules/mod_ssl.so 

In the configuration we create an HTTP service. It will be he who will answer the telegram and process his appeals.

In my case, the following parameters are specified:

Name: TGWebhuk RootURL
: webhook
Session reuse: Do not use (it didn’t work with automatic mode)
Lifetime: 20
URL patterns: Any pattern was created with two methods: GET and POST

image

image

image

Method handlers are created default. I’ll add the following to the POST handler, just to check the connection:

 POST()

	(""); //chat_id
	 =  HTTP(200);
	 ;
	



&
 ()
	
		 = " ";
		 = "";//  telegram
		 = "api.telegram.org";
	     = "bot" +  + "/sendMessage?chat_id=" + ((, "=; =; =."), ".", "") + "&text=" + ;
   		  =   HTTP(,443,,,,, OpenSSL());
		 =  HTTP();
		 = .();
	

It remains to publish the database and attach the webhook.

The publication is done as always, you just need to add checkmarks to the publication of the HTTP service:

image

As a last step, we will attach our 1C to the telegram. For this, I used a simple html page with the following code:

<html>
<body>

<form action="https://api.telegram.org/bot<>/setwebhook" method="post" enctype="multipart/form-data">
    Select Certificate to upload:
    <input type="file" name="certificate" id="fileToUpload">
	URL: <input type="text" name="url"  value="https://<YOURWEBSITE>/<YOUR_PHP_URL>"><br>
    <input type="submit" value="Upload Certificate" name="submit">
</form>

</body>
</html>

In the form, just select the public key and enter the full path to our http-service. I remind you that the full path to the service will look like this:

YourDomain / BaseName / hs / ServiceName / v1

Please do not kick the code, some things are intentionally done by hardcode, because all this was done solely for demonstration purposes.

The publication was written because I have not found a single working example on working with webhooks on the site, except for Telegram bot constructor. But it is paid and, perhaps, not everyone needs it in this form.

The archive contains the installation of Apache 2.2.24 along with Openssl (for some reason, it took me a long time to find it), an html file for registering a web hook, an Apache configuration file and cf configuration with an http service and an example of sending a test message. It’s optional to download, because all content is in the article.

All Articles