Skip to content

Forum in maintenance, we will back soon 🙂

Python Telegram bot
 
Notifications
Clear all

Python Telegram bot

10 Posts
3 Users
5 Reactions
874 Views
(@faizan)
Posts: 5
Active Member
Topic starter
 

Hello, so I recentyl got my hands on a telegram chatbot python script that connects chtagpt api to the telegram interface. Though I am not able to fully custmoise it. For instance I've created a english teaching bot but I want to add a timer to start when the session starts and and notify when the session ends and and want a second prompt after the session that tells to like this is your score or something and analysis of the conversation. Also I want to add a limit of texts a person can send like for a demo reason. I really hope you can help me out in making this project. Thanks Hassan!

 
Posted : 05/26/2023 5:13 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

Use the following prompt in GPT-4:

 
You are an expert in the Python programming language and I am going to be 
asking you questions about the best way to program a problem. You will answer 
in a step by step way to ensure the accuracy of your answer. I don't want to see 
the steps but the final answer after you have verified yourself on the possible 
answers. Also I would like a list of references that support your answer following 
the answer. Okay?

 

Then you ask how to do whatever.
 
NOTE: You need to be cautious of the code and be able to spot simple errors but you will get the ideas of how to do what you need.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 05/27/2023 1:43 am
Hasan Aboul Hasan
(@admin)
Posts: 1269
Member Admin
 

can you share your script workflow here. or attach the script

 
Posted : 05/27/2023 6:39 am
(@faizan)
Posts: 5
Active Member
Topic starter
 

Sure, here's the script. I recently learned about telegram web app. Thinking if that would be a better approach to get my end product. 

P.s: Is it possible to talk via email? Thanks!

from telegram.ext import Updater, MessageHandler, Filters
import telegram
import openai
from moviepy.editor import AudioFileClip
import time

openai.api_key = ""
TELEGRAM_API_TOKEN = ""

messages = [{"role": "assistant", "content": "Act as Emily, a friendly English teacher, you are a spoken english teacher"}]

def text_message(update, context):
messages.append({"role": "user", "content": update.message.text})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
ChatGPT_reply = response["choices"][0]["message"]["content"]
update.message.reply_text(text=f"*[Bot]:* {ChatGPT_reply}", parse_mode=telegram.ParseMode.MARKDOWN)
messages.append({"role": "assistant", "content": ChatGPT_reply})

def start_timer(duration):
if "for" in duration:
# Extract the duration from the phrase
duration = int(duration.split(" ")[4].split("-")[0])

# Start the timer
print(f"Starting the timer for {duration} minutes.")
time.sleep(duration * 60)
print("Timer is up!")

updater = Updater(TELEGRAM_API_TOKEN, use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(MessageHandler(Filters.text & (~Filters.command), text_message))
dispatcher.add_handler(MessageHandler(Filters.voice, voice_message))
updater.start_polling()
updater.idle()

This post was modified 2 years ago by Faizan
 
Posted : 05/30/2023 10:25 am
(@faizan)
Posts: 5
Active Member
Topic starter
 

@ssadvisor Thanks!

 
Posted : 05/31/2023 2:27 am
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@faizan How do you plan to use this; locally or deployed on a server?

Maybe check out framework libraries like Flask, FastAPI or Streamlit to help you.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 05/31/2023 6:08 pm
(@faizan)
Posts: 5
Active Member
Topic starter
 

@ssadvisor Thanks you for your reply sir. I am currently deploying it using Pythonanywhere. But the functionality is limited to me making requests and getting answers, just like chatgpt on telegram. But I wish to make it fully function for my project . I mean it would be possible if I took a separate web app approach, but using telegram, I am not too sure. I really want to make this project a reality!

This post was modified 2 years ago by Faizan
 
Posted : 06/01/2023 9:19 am
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@faizan I have a couple of suggestions:

1) Get it working locally before deploying it to Pythonanywere.

2) Create a project under your account on github for it so you can get other eyes on it easily.

Once you have the application working you can deploy it.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 06/01/2023 7:46 pm
(@faizan)
Posts: 5
Active Member
Topic starter
 

@ssadvisor Noted sir. I'll get to github. Though coming from a no-coding background I would require assitance.

 
Posted : 06/02/2023 4:30 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@faizan YouTube is your friend for tutorials. Plus github has been around for decades and the documentation is plentiful. Coding is nothing more than creating a map for your data and then telling the data what to do at each stop along the way.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 06/02/2023 5:20 pm
Share: