commit cf130e9fa89bc0c94a55e0ee426bd06baf90f60f Author: Christine Dodrill Date: Fri Jan 6 12:37:24 2017 -0800 first commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4cb9bcc --- /dev/null +++ b/LICENSE @@ -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 diff --git a/bot.lua b/bot.lua new file mode 100644 index 0000000..3f6847b --- /dev/null +++ b/bot.lua @@ -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 diff --git a/lib.lua b/lib.lua new file mode 100644 index 0000000..0f6b431 --- /dev/null +++ b/lib.lua @@ -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()) diff --git a/main.go b/main.go new file mode 100644 index 0000000..153c814 --- /dev/null +++ b/main.go @@ -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) + } +} diff --git a/tglua.go b/tglua.go new file mode 100644 index 0000000..cad6a84 --- /dev/null +++ b/tglua.go @@ -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 +} diff --git a/var/data.json b/var/data.json new file mode 100644 index 0000000..37792b7 --- /dev/null +++ b/var/data.json @@ -0,0 +1 @@ +{"lastowotime":0,"tolearnfrom":null} diff --git a/var/data.json:w b/var/data.json:w new file mode 100644 index 0000000..e69de29