|
| 1 | +import { Observable as $ } from 'rx' |
| 2 | +import { compose, prop, curryN } from 'ramda' |
| 3 | +import { stripIndents } from 'common-tags' |
| 4 | + |
| 5 | +import { reply, answerInlineQuery } from 'cycle-telegram' |
| 6 | + |
| 7 | +let prettify = (query, result) => ` |
| 8 | +Prelude> ${query} |
| 9 | +${result} |
| 10 | +` |
| 11 | + |
| 12 | +let answer = curryN(2, (u, text) => $.just( |
| 13 | + u.inline_query ? answerInlineQuery({ |
| 14 | + results: [ |
| 15 | + { |
| 16 | + type: 'article', |
| 17 | + title: text, |
| 18 | + input_message_content: { |
| 19 | + message_text: prettify(u.inline_query.query, text) |
| 20 | + } |
| 21 | + } |
| 22 | + ], |
| 23 | + cache_time: 0 |
| 24 | + }, u) : reply({ text: prettify(u.message.text, text) }, u))) |
| 25 | + |
| 26 | +function TryHaskell ({HTTP, props}, u) { |
| 27 | + let text = props[0] |
| 28 | + let res = HTTP |
| 29 | + .filter(res$ => res$.request.send.exp === text) |
| 30 | + .mergeAll() |
| 31 | + .map(compose(JSON.parse, prop('text'))) |
| 32 | + .flatMapLatest(({ |
| 33 | + error, |
| 34 | + success |
| 35 | + }) => error ? $.throw(error) : $.just(success)) |
| 36 | + |
| 37 | + return { |
| 38 | + bot: res |
| 39 | + .map(res => stripIndents` |
| 40 | +${res.stdout.join('')} |
| 41 | +${res.value} :: ${res.type} |
| 42 | + `) |
| 43 | + .flatMapLatest(answer(u)) |
| 44 | + .catch(answer(u)), |
| 45 | + |
| 46 | + HTTP: $.just({ |
| 47 | + method: 'POST', |
| 48 | + url: 'http://tryhaskell.org/eval', |
| 49 | + category: 'tryhaskell', |
| 50 | + send: { |
| 51 | + exp: text |
| 52 | + }, |
| 53 | + type: 'application/x-www-form-urlencoded', |
| 54 | + accept: 'application/json' |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +export default TryHaskell |
0 commit comments