How to turn any script into a Telegram bot

If you need a simple Telegram bot capable of executing scripts (written in any language) and responding with text and pictures, then under cat. There you will find a story about a bot engine that does what you want.



Telegram bot



A short list of engine features:



  • The engine can serve several bots at once
  • Bot executes scripts (written in any language)
  • , ( )
  • ( )
  • HTTP, , "" ( cron-)


: , IP-, SSL-… , . , , .



, .





Go. , root-. , , , , .



:



cd tmp
git clone https://github.com/michurin/cnbot.git
cd cnbot
go build ./cmd/...
./cnbot


( ) , . , .





, . : Telegram.



(config.yaml):



bots:
  firstBot:
    token: "22222222:AAAAAAAAAAAAAA"
    script: "/usr/bin/true"


( - , /bin/rm). (-i):



./cnbot -i -c config.yaml


, .



( -i)



./cnbot -c config.yaml


Telegram-.



user 500050880 is not allowed


user_id ( ),



bots:
  firstBot:
    token: "22222222:AAAAAAAAAAAAAA"
    script: "/bin/echo"
    allowed_users: [500050880]


, echo script. ( ) echo-. . hi, Hi!, -n hi.



(-n echo). : ; , , , ; .



. /bin/echo :



#!/bin/sh
env


Hello!



BOT_TEXT=Hello!
BOT_FROM_FIRSTNAME=Alexey
BOT_NAME=firstBot
BOT_CHAT=500050880
BOT_FROM=500050880


, , ID , , ( ; , ).



, . , white list . ID — . . demo.sh.



, PATH . , .





#!/bin/sh
echo '%!PRE'
env | sort


. markdown, . demo.sh.



, stdout:



#!/bin/sh
curl -qfs https://golang.org/lib/godoc/images/footer-gopher.jpg


, "empty", , "".



demo-, .





HTTP bind_address :



bots:
  firstBot:
    token: "22222222:AAAAAAAAAAAAAA"
    script: "/bin/echo"
    allowed_users: [500050880]
    bind_address: ":9091"


:



echo "ok" | curl -qfsX POST --data-binary @- "http://:9091/500050880"


, , . , output . , .



multipart/form-data:



curl -qfsX POST -F to=500050880 -F msg=ok "http://:9091"


, . , , . , demo-. http- . readme , , , .



, , — .



!



...



..., , .



- . , . , , . , . .



, . : , , VPN, … , , - , .



, , , Telegram API . - bash+curl+jq, , .



. . , , - .



, . (), … , , . , , , .



2018 Go , , Go. :-) , , Go . , Go, Go, "" ( ).



. cnbot, .



?



, . . - , — . .



But I would like to develop the engine towards embeddability: to single out some simple part from it that could be connected by a library to any Go program. Conventionally, if you already have a microservice for ... what is it good for ... even for rendering 3D scenes, you can embed a bot chat into it in one or two lines for operational diagnostics / monitoring / control ... This, it seems to me, would be useful. In fact, you can do that now. It's just not very convenient.




All Articles