Hello Codex: an OpenAI Telegram Bot
Tags: python, bot, azure
Published: 2022-03-06 18:49:37

![Python for Ukraine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1tild3v0ys7jbr4lokas.png)

Tired of waiting for a Github Copilot invite, I decided to take matters into my own hands 🙈

OpenAI’s Codex is a a GPT-3 powerhouse that converts plain english into runnable code. It can also do many other tasks related to Code, like translate code from one language to another, generate docstrings and explain already written code in plain english.

![Codex](https://techcrunch.com/wp-content/uploads/2021/08/openai-plot.gif)

  
  

Here are the steps to creating your very own personal Codex Telegram bot.

> **Prerequisite:** For Codex API access you need to get an invite from OpenAI. This is free and I got approved after around one week. Join the waitlist [here](https://openai.com/blog/openai-codex/).

If you haven’t already done so, [download](https://telegram.org/) Telegram, they also offer a great desktop app.

**Request a Bot**
For this, you talk to [BotFather](https://t.me/botfather). He is your go-to for all things bot, from creation to settings etc.
![Botfather](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0v4geey4cj0xenws7ech.png) 

Type `/newbot` to begin.

The BotFather will ask you to pick a bot name (which must be universally unique) and a username, it then generates an authentication token for your new bot.

<br>**Codex Key**
Next, go ahead and [grab your API key](https://beta.openai.com/account/api-keys) from OpenAI.

![Codex API key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0gkrz3ak8ygbqeyaxcl.png)

 Take note of the 2 keys you just got, we’re going to need them soon.


> 💡 Dealing with secrets across environments can be a hassle. The folks at [Doppler](https://www.doppler.com/) offer a great (and free) product to handle it all.

<br>**Python Code**
Fire up your favorite editor ([VS Code](https://code.visualstudio.com/download) in my case) and lets have some fun!

<br>To start, we pip install the following two libraries:

  

``` bash
pip install openai
pip install pyTelegramBotAPI
```

Begin like so:

```python
import logging
from os import environ as env
from dotenv import load_dotenv # if you dont have dotenv yet: pip install python-dotenv

import telebot
import openai

logger = telebot.logger
telebot.logger.setLevel(logging.DEBUG)

load_dotenv()
bot = telebot.TeleBot(env["BOT_API_KEY"])
openai.api_key = env["OPENAI_API_KEY"]
```

Enabling logging is always a great idea when working with API’s. Eventually we’ll want to host this somewhere and so the more we log, the less in the dark we are.

Notice how I’m importing environ from os. Using `env[‘BOT_API_KEY’]` is just prettier/neater than os.environ 🤷‍♂️

Now, let’s establish the communiqué between our shiny new bot and Codex.

~~~ python
@bot.message_handler(func=lambda message: True)
def get_codex(message):
    response = openai.Completion.create(
        engine="code-davinci-001",
        prompt='"""\n{}\n"""'.format(message.text),
        temperature=0,
        max_tokens=1200,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0,
        stop=['"""'])
    
    bot.send_message(message.chat.id,
    f'```python\n{response["choices"][0]["text"]}\n```',
    parse_mode="Markdown")
        
bot.infinity_polling()
~~~

 Above, we first establish the bot’s decorator to pass on our message to the function below it.

We set `max_tokens` to `1200` this is because while Codex allows for a max of 4,096 tokens (including your prompt) per call, Telegram bots are capped at 4,096 UTF-8 characters per message. One token is equivalent to roughly 4 characters.

Learn more about the various `openai.Completion.create` parameters [here](https://beta.openai.com/docs/api-reference/completions/create), their docs are awesome.

In `bot.send_message` we take advantage of the fact that Telegram offers great Markdown support so the code displays nicely and also allows for tap-to-copy.

Finally, `bot.infinity_polling()` keeps a constant open ear for incoming messages.<br>

>There are lots of other cool things a Telegram bot can do and pyTelegramBotAPI [breaks it all down](https://github.com/eternnoir/pyTelegramBotAPI) in much detail.

<br>Before running the script, create a .env file in your current directory like so:
```
BOT_API_KEY = bot-key-here
OPENAI_API_KEY = openai-key-here
```
Here is what your full script should look like:

~~~ python
    import logging
    from os import environ as env
    from dotenv import load_dotenv # if you dont have dotenv yet: pip install python-dotenv

    import telebot
    import openai


    logger = telebot.logger
    telebot.logger.setLevel(logging.DEBUG)

    load_dotenv()
    bot = telebot.TeleBot(env["BOT_API_KEY"])
    openai.api_key = env["OPENAI_API_KEY"]

    @bot.message_handler(func=lambda message: True)
    def get_codex(message):
    response = openai.Completion.create(
        engine="code-davinci-001",
        prompt='"""\n{}\n"""'.format(message.text),
        temperature=0,
        max_tokens=1200,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0,
        stop=['"""'])

    bot.send_message(message.chat.id,
    f'```python\n{response["choices"][0]["text"]}\n```',
    parse_mode="Markdown")
        
    bot.infinity_polling()
~~~

Congrats! You now have your very own Codex always at your fingertips.

>For your bot to be always accessible you’ll want to host it in the cloud. Whether AWS or Heroku, go with your favorite; bear in mind though that `bot.infinity_polling()` will need a server setup (i.e. Flask) to run correctly.
<br>To avoid constant compute minutes we can set a webhook instead of polling. In the next article I’ll show you how I did this as an Azure Function app.