stuck in creating simple telegram bot with openresty

491 views
Skip to first unread message

Meysam Khezri

unread,
May 5, 2016, 8:56:15 AM5/5/16
to openresty-en
Hello everyone
I am trying to write simple telegram bot with lua + nginx. I have no experiense with both of them!
I was registering webhook with this :

curl https://api.telegram.org/bot189827093:AAHv3CoiJZD4l9EOMtDp-m7EZBHBu6v4A4g/setWebhook     -F "url=https://example.com:8443/mybot"     -F "certificate=@/etc/nginx/ssl/nginx.pem"

and it works : {"ok":true,"result":true,"description":"Webhook was set"}

I am installed openresty and this is part of my nginx config file relating to bot code:


server {
        listen       8443 ssl;
        server_name  localhost;
        ssl_certificate /etc/nginx/ssl/nginx.pem;
        ssl_certificate_key /etc/nginx/ssl/nginx.key;
        access_log /var/log/nginx/t8443.access.log;
        error_log /var/log/nginx/t8443.error.log;

        location /mybot {

           resolver 8.8.8.8;

           content_by_lua '

                local botToken = "189827093:AAHv3CoiJZD4l9EOMtDp-m7EZBHBu6v4A4g";
                local website  = "https://api.telegram.org/bot"..botToken.."/sendmessage?";

                -- load json package
                cjson = require "cjson"

                -- read POST body
                ngx.req.read_body()
                local body = ngx.req.get_body_data()

                if not body then
                        ngx.say("failed to parse request: ",err)
                        return
                end

                local data = cjson.decode(body)
                local chatId = data.message.chat.id
                local text = data.message.text

                local response = "goodbye"

                if text=="/hi" then
                        response = "Hello"
                end

                -- the problem is in this part

                local http = require "resty.http"
                local httpc = http.new()
                local res, err = httpc:request_uri(website, {
                        method = "POST",
                        body = "chat_id="..chatId.."&text="..response,
                        headers = {
                          ["Content-Type"] = "application/x-www-form-urlencoded",
                        }
                })

                if not res then
                        ngx.log(1,"failed to request: ", err)
                        return
                end
           ';

        }
}

and in the error.log I see this:
2014/03/02 13:45:53 [emerg] 8656#0: *5 [lua] content_by_lua(test8443:82):40: failed to request: 19: self signed certificate in certificate chain, client: 149.154.167.217, server: localhost, request: "POST /mybot HTTP/1.1", host: "example.com"

actually this is the ssl verification problem (my test website has a self signed certificate)

I write similar bot with php and it works like a charm :

<?php

//set up the Bot API token
$botToken = "189827093:AAHv3CoiJZD4l9EOMtDp-m7EZBHBu6v4A4g";
$website = "https://api.telegram.org/bot".$botToken;

//Grab the info from the webhook, parse it and put it into $message
$content = file_get_contents("php://input");
$update = json_decode($content, TRUE);
$message = $update["message"];

//Make some helpful variables
$chatId = $message["chat"]["id"];
$text = $message["text"];

switch ($text) {
        case "/hi":
                $response="Hello";
                break;
        case "/start":
                $response="start";
                break;
        default:
                $response="goodbye";
}

file_get_contents($website."/sendmessage?chat_id=".$chatId."&text=".$response);

actually what I need is something similar to

file_get_contents($website."/sendmessage?chat_id=".$chatId."&text=".$response);

in openresty

thanks for your help




Lord Nynex

unread,
May 5, 2016, 12:30:10 PM5/5/16
to openre...@googlegroups.com
Hello,

Please excuse my brief or vague answer, I'm really short on time right now but wanted to give you some info to point you in the right direction. 

Please see https://github.com/pintsized/lua-resty-http/issues/42 for tips/ideas on your particular issue. Your example does not appear to perform an ssl handshake (I'm not sure if the simple request_uri interface does that automatically for resty.http). Additionally, you'll want to look for some nginx configurations around 'SSL verify chain depth'. I believe by default, nginx will not recurse a chain of authority during certificate validation. So you must enable this configuration to properly verify. 

Please feel free to ask more questions if the above information doesn't prove useful. Also feel free to browse resty.https issue tracker for users who have had similar issues. 

-Brandon

--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Meysam Khezri

unread,
May 6, 2016, 2:17:54 AM5/6/16
to openresty-en

Thank you Brandon

I solved the problem by adding ssl_verify = flase to the code :


                local res, err = httpc:request_uri(website, {
                        method = "POST",
                        ssl_verify = false,

                        body = "chat_id="..chatId.."&text="..response,
                        headers = {
                          ["Content-Type"] = "application/x-www-form-urlencoded",
                        }
                })

now it works

Yichun Zhang (agentzh)

unread,
May 7, 2016, 1:29:01 PM5/7/16
to openresty-en
Hello!

On Thu, May 5, 2016 at 5:56 AM, Meysam Khezri wrote:
> and in the error.log I see this:
> 2014/03/02 13:45:53 [emerg] 8656#0: *5 [lua] content_by_lua(test8443:82):40:
> failed to request: 19: self signed certificate in certificate chain, client:

This error is usually an indication of too short certificate
verification depth or the lack of trusted (root) CA certificates. See

https://github.com/openresty/lua-nginx-module#lua_ssl_verify_depth

https://github.com/openresty/lua-nginx-module#lua_ssl_trusted_certificate

Regards,
-agentzh
Reply all
Reply to author
Forward
0 new messages