avatarMateusz Kozłowski

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1833

Abstract

800/1*SRU0b2E7MbIHDfsRCmaOzg.png"><figcaption>Create a telegram bot</figcaption></figure><h1 id="b681">OPEN AI — getting access ke</h1><p id="3e6a">Next, go to <a href="https://beta.openai.com/account/api-keys">https://beta.openai.com/account/api-keys</a> and create a new secret key.</p><figure id="e5b5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*vXrWBVQFWF0pILUXJK7bVw.png"><figcaption>OPEN AI API KEYS</figcaption></figure><h1 id="0ce7">PYTHON CODE</h1><p id="b137">You can find my implementation on my <a href="https://github.com/mattkozlowski/openai-telegram-bot">GitHub</a>.</p><div id="f211"><pre><span class="hljs-keyword">import</span> openai <span class="hljs-keyword">from</span> telegram <span class="hljs-keyword">import</span> Update <span class="hljs-keyword">from</span> telegram.ext <span class="hljs-keyword">import</span> ApplicationBuilder, CommandHandler, ContextTypes

<span class="hljs-comment"># OpenAI API credentials</span> openai.api_key=<span class="hljs-string">"HERE-YOUR-OPEN-API-TOKEN"</span> <span class="hljs-comment"># Telegram Bot API credentials</span> token = <span class="hljs-string">"HERE-YOUR-TELEGRAM-API-KEY"</span>

<span class="hljs-keyword">async</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">hello</span>(<span class="hljs-params">update: Update, context: ContextTypes.DEFAULT_TYPE</span>) -> <span class="hljs-literal">None</span>: <span class="hljs-keyword">await</span> update.message.reply_text(<span class="hljs-string">f'Hello <span class="hljs-subst">{update.effective_user.first_name}</span>'</span>)

<span class="hljs-keyword">async</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">ask</span>(<span class="hljs-params">update, context</span>): <span class="hljs-comment"># Ge

Options

t the user's message</span> message = update.message.text.split(<span class="hljs-string">" "</span>, <span class="hljs-number">1</span>)[<span class="hljs-number">1</span>] <span class="hljs-built_in">print</span>(message) <span class="hljs-comment"># Use OpenAI to generate a response</span> response = openai.Completion.create( model=<span class="hljs-string">"text-davinci-003"</span>, prompt=<span class="hljs-string">f"<span class="hljs-subst">{message}</span>"</span>, max_tokens=<span class="hljs-number">2048</span>, n=<span class="hljs-number">1</span>, stop=<span class="hljs-literal">None</span>, temperature=<span class="hljs-number">0.5</span>, ).choices[<span class="hljs-number">0</span>].text

<span class="hljs-keyword">await</span> update.message.reply_text(<span class="hljs-string">f'Response <span class="hljs-subst">{response}</span>'</span>)

app = ApplicationBuilder().token(token).build() app.add_handler(CommandHandler(<span class="hljs-string">"hello"</span>, hello)) app.add_handler(CommandHandler(<span class="hljs-string">"ask"</span>, ask)) app.run_polling()</pre></div><p id="2393">Finally, use the code above, insert your API keys in the designated places and save file as server.py. Now you can run your code</p><div id="c436"><pre>python3 server.py</pre></div><h1 id="2391">TRY CHATTING WITH GPT3</h1><div id="bb1e"><pre>/hello</pre></div><div id="4c8c"><pre>/ask write <span class="hljs-selector-tag">a</span> tweet about why is important <span class="hljs-selector-tag">to</span> have work-life-balance</pre></div><p id="23fa">That’s it — it’s easy, right? Please let me know your feedback.</p><p id="4095">Matt Kozlowski <a href="https://twitter.com/matikozlowski">https://twitter.com/matikozlowski</a></p></article></body>

Create a chatbot in Telegram using OpenAI GPT-3.

Chatting with GPT-3 can be a great way to increase productivity. GPT-3 is an advanced artificial intelligence system that can generate natural language responses to questions and requests. By using GPT-3, you can quickly get answers to complex questions and tasks without having to spend time researching or thinking about the best solution. GPT-3 can also provide helpful advice and insights into how to best approach a given task or problem. Additionally, GPT-3 can help automate mundane tasks, freeing up time for more meaningful work. By leveraging GPT-3’s capabilities, you can become more productive and efficient in no time.

Example of simple task that can be done in 30sec by GPT-3

If you’re looking to create your own Telegram bot, you’ve come to the right place! On this page, you’ll find a comprehensive tutorial on how to create a Telegram bot from scratch and connect it to Open AI Chat GPT-3. We’ll walk you through the steps of setting up your bot, writing code for it, and running it. With a help, you’ll be able to create a fully-functional bot in no time!

Telegram bot setup

  1. Find BotFather on Telegram — type @BotFather in the search bar
BotFather

2. Then, type /newbot and give your bot a name and username.

3. You will then receive a token to access the HTTP API.

Create a telegram bot

OPEN AI — getting access ke

Next, go to https://beta.openai.com/account/api-keys and create a new secret key.

OPEN AI API KEYS

PYTHON CODE

You can find my implementation on my GitHub.

import openai
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes

# OpenAI API credentials
openai.api_key="HERE-YOUR-OPEN-API-TOKEN"
# Telegram Bot API credentials
token = "HERE-YOUR-TELEGRAM-API-KEY"

async def hello(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    await update.message.reply_text(f'Hello {update.effective_user.first_name}')

async def ask(update, context):
    # Get the user's message
    message = update.message.text.split(" ", 1)[1]
    print(message)
    # Use OpenAI to generate a response
    response = openai.Completion.create(
        model="text-davinci-003",
        prompt=f"{message}",
        max_tokens=2048,
        n=1,
        stop=None,
        temperature=0.5,
    ).choices[0].text

    await update.message.reply_text(f'Response {response}')

app = ApplicationBuilder().token(token).build()
app.add_handler(CommandHandler("hello", hello))
app.add_handler(CommandHandler("ask", ask))
app.run_polling()

Finally, use the code above, insert your API keys in the designated places and save file as server.py. Now you can run your code

python3 server.py

TRY CHATTING WITH GPT3

/hello
/ask write a tweet about why is important to have work-life-balance

That’s it — it’s easy, right? Please let me know your feedback.

Matt Kozlowski https://twitter.com/matikozlowski

AI
Gpt 3
Python
Machine Learning
Telegram Bot
Recommended from ReadMedium