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="" -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 = ""> 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