first commit

This commit is contained in:
Cadey Ratio 2017-01-06 12:37:24 -08:00
commit cf130e9fa8
7 changed files with 205 additions and 0 deletions

12
LICENSE Normal file
View File

@ -0,0 +1,12 @@
YOLO LICENSE
Version 1, July 10 2015
THIS SOFTWARE LICENSE IS PROVIDED "ALL CAPS" SO THAT YOU KNOW IT IS SUPER
SERIOUS AND YOU DON'T MESS AROUND WITH COPYRIGHT LAW BECAUSE YOU WILL GET IN
TROUBLE HERE ARE SOME OTHER BUZZWORDS COMMONLY IN THESE THINGS WARRANTIES
LIABILITY CONTRACT TORT LIABLE CLAIMS RESTRICTION MERCHANTABILITY SUBJECT TO
THE FOLLOWING CONDITIONS:
1. #yolo
2. #swag
3. #blazeit

60
bot.lua Normal file
View File

@ -0,0 +1,60 @@
local fs = require "lfs"
local http = require "http"
local json = require "json"
local telegram = require "telegram"
local yaml = require "yaml"
function restore()
local fin = io.open("var/data.json", "r")
local data, err = json.decode(fin:read("*a"))
if err ~= nil then
print("cannot load data: " .. err)
return
end
vars = data
end
function save()
local fout = io.open("var/data.json", "w")
local data = json.encode(vars)
fout:write(data)
fout:write("\n")
fout:close()
end
function main()
restore()
if vars == nil then
vars = {}
end
if vars.lastowotime == nil then
vars.lastowotime = 0
end
if vars.tolearnfrom == nil then
vars.tolearnfrom = {} -- array of id's of people
end
while true do
local update = tgupdates:receive()
if update ~= nil and update.message ~= nil then
local chatid = update.message.chat.ID
local userid = tostring(update.message.from.ID)
local text = update.message.text
if string.find(string.lower(text), "notices") and math.random(100) % 15 == 0 then
if vars.lastowotime + 600 < os.time() then
local msg = telegram.newMessage(chatid, "owo what's this?")
tg:send(msg)
vars.lastowotime = os.time()
end
end
end
save()
end
end

35
lib.lua Normal file
View File

@ -0,0 +1,35 @@
function switch(t)
t.case = function (self,x)
local f=self[x] or self.default
if f then
if type(f)=="function" then
f(x,self)
else
error("case "..tostring(x).." not a function")
end
end
end
return t
end
function split(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
function string.ends(String,End)
return End=='' or string.sub(String,-string.len(End))==End
end
math.randomseed(os.time())

67
main.go Normal file
View File

@ -0,0 +1,67 @@
package main
import (
"log"
"net/http"
"github.com/cjoudrey/gluahttp"
"github.com/kohkimakimoto/gluayaml"
"github.com/kr/pretty"
luar "github.com/layeh/gopher-luar"
"github.com/namsral/flag"
lua "github.com/yuin/gopher-lua"
"gopkg.in/telegram-bot-api.v4"
"layeh.com/gopher-json"
"layeh.com/gopher-lfs"
)
var (
discordBotToken = flag.String("discord-token", "", "Discord bot token")
telegramBotToken = flag.String("telegram-token", "", "Telegram bot token")
)
func main() {
flag.Parse()
bot, err := tgbotapi.NewBotAPI(*telegramBotToken)
if err != nil {
log.Fatal(err)
}
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
log.Fatal(err)
}
L := lua.NewState()
defer L.Close()
L.PreloadModule("yaml", gluayaml.Loader)
L.PreloadModule("http", gluahttp.NewHttpModule(&http.Client{}).Loader)
json.Preload(L)
lfs.Preload(L)
L.SetGlobal("tg", luar.New(L, bot))
L.SetGlobal("tgupdates", luar.New(L, updates))
L.SetGlobal("pprint", luar.New(L, pretty.Println))
preloadTG(L)
err = L.DoFile("lib.lua")
if err != nil {
log.Fatal(err)
}
err = L.DoFile("bot.lua")
if err != nil {
log.Fatal(err)
}
err = L.DoString("main()")
if err != nil {
log.Fatal(err)
}
}

30
tglua.go Normal file
View File

@ -0,0 +1,30 @@
package main
import (
luar "github.com/layeh/gopher-luar"
lua "github.com/yuin/gopher-lua"
"gopkg.in/telegram-bot-api.v4"
)
func preloadTG(L *lua.LState) {
L.PreloadModule("telegram", loadTelegram)
}
var tgapi = map[string]lua.LGFunction{
"newMessage": tgNewMessage,
}
func loadTelegram(L *lua.LState) int {
t := L.NewTable()
L.SetFuncs(t, tgapi)
L.Push(t)
return 1
}
func tgNewMessage(L *lua.LState) int {
chatid := L.CheckInt64(1)
text := L.CheckString(2)
L.Push(luar.New(L, tgbotapi.NewMessage(chatid, text)))
return 1
}

1
var/data.json Normal file
View File

@ -0,0 +1 @@
{"lastowotime":0,"tolearnfrom":null}

0
var/data.json:w Normal file
View File