Skip to content

Instantly share code, notes, and snippets.

@athphane
Last active May 13, 2020 15:02
Show Gist options
  • Save athphane/a723923e89daa01af9ec115f7a5e0342 to your computer and use it in GitHub Desktop.
Save athphane/a723923e89daa01af9ec115f7a5e0342 to your computer and use it in GitHub Desktop.
Get a pretty image of your code using carbon-now-cli in telegram.
"""
Turn your code into beautiful images using carbon-now-cli.
You will need npm installed on your machine to install carbon-now-cli..
Windows: npm install -g carbon-now-cli
Linux: sudo npm install -g carbon-now-cli --unsafe-perm=true --allow-root
MacOS: I assume almost the same as linux ¯\_(ツ)_/¯
"""
import os
from time import sleep
from pyrogram import Client, Filters, Message
API_ID = 123456
API_HASH = "super-secret-key-here"
CARBON_LANG = "py"
BOT = Client(
session_name="userbot",
api_id=API_ID,
api_hash=API_HASH,
)
@BOT.on_message(Filters.command("carbon", ".") & Filters.me)
def carbon_test(bot: BOT, message: Message):
"""
Receives text and makes a carbon image using the text
Eg: .carbon your code here (multi line supported)
"""
carbon_text = message.text[8:]
# Write the code to a file cause carbon-now-cli wants a file.
file = "carbon.{}".format(get_carbon_lang())
f = open(file, "w+")
f.write(carbon_text)
f.close()
message.edit_text("Carbonizing code...")
# Do the thing
os.system("carbon-now -h -t carbon {}".format(file))
message.edit_text("Carbonizing completed...")
# Send the thing
BOT.send_photo(message.chat.id, 'carbon.png')
message.delete()
@BOT.on_message(Filters.command('carbonlang', '.') & Filters.me)
def update_carbon_lang(bot: BOT, message: Message):
"""
Set language to use Carbon with.
Eg: .carbonlang js -> will set the file type to js
"""
global CARBON_LANG
cmd = message.command
if len(cmd) > 1:
type_text = " ".join(cmd[1:])
elif message.reply_to_message and len(cmd) is 1:
type_text = message.reply_to_message.text
elif not message.reply_to_message and len(cmd) is 1:
message.edit("Give me something to carbonize")
sleep(2)
message.delete()
return
CARBON_LANG = type_text
message.edit_text("Carbon type set to {}".format(type_text))
sleep(5)
message.delete()
@BOT.on_message(Filters.command('carbonlang', '!') & Filters.me)
def send_carbon_lang(bot: BOT, message: Message):
"""
Edits message to show current set carbon language
"""
message.edit_text(get_carbon_lang())
sleep(5)
message.delete()
def get_carbon_lang():
"""
Gets carbon language. Default py
"""
return CARBON_LANG
if __name__ == '__main__':
BOT.start()
BOT.idle()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment