middlewares.lua
local ggram = require("ggram")
local bot = ggram("123456789:QWERTYUIOPASDFGHJKLZXCVBNM")
require("ggram.polling").start(bot)
bot.update(require("ggram.middlewares.session"), "session_middleware")
bot.update(function(ctx)
ctx.reply.markdown("Sleeping *4 seconds*")
ctx.reply.action("typing")
coroutine.yield(function(cb) (timer or require("gmod.timer")).Simple(4, cb) end)
end, "sleep")
bot.command("test", function(ctx)
local ses = ctx.session
ses.presses = (ses.presses or 0) + 1
ctx.reply.text(ctx.text .. " pressed " .. ses.presses .. " times")
end)
local http_Fetch = require("http_async").get
local function co_fetch(url)
return coroutine.yield(function(cb)
http_Fetch(url,
function(body, ...) cb(true, body, ...) end,
function(err) cb(false, err) end
)
end)
end
bot.command("delayed_handler", function(ctx)
ctx.reply.text("fetching urls")
local _, res1 = co_fetch("https://httpbin.org/delay/1")
local _, res2 = co_fetch("https://httpbin.org/get")
ctx.reply.text("fetching completed")
ctx.session.http_results = {res1, res2}
end)
bot.update(function(ctx)
ctx.reply.markdown("Stopped. The 'done' text will send only if /delayed_handler called")
return ctx.session.http_results ~= nil
end, "stop_middl")
bot.update(function(ctx)
ctx.reply.text("Done. It's the last middleware. You see this because the /delayed_handler command was executed")
assert(ctx.session.http_results ~= nil)
end, "done")
ggram.idle()