├── .gitignore ├── .vscode └── launch.json ├── Procfile ├── __pycache__ └── process.cpython-39.pyc ├── bot.py ├── process.py ├── requirements.txt └── token.txt /.gitignore: -------------------------------------------------------------------------------- 1 | env -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "Dart & Flutter", 9 | "request": "launch", 10 | "type": "dart" 11 | } 12 | ] 13 | } -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | worker: python3 bot.py -------------------------------------------------------------------------------- /__pycache__/process.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iamsushanth/telegram-chatbot-using-open-ai-python-heroku/34141f5420559a67c77f97380f9b50790ef8b0be/__pycache__/process.cpython-39.pyc -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | import telegram.ext 2 | from process import ask, append_interaction_to_chat_log 3 | import logging, os 4 | 5 | PORT = int(os.environ.get('PORT', '8443')) 6 | 7 | with open('token.txt', 'r') as f: 8 | TOKEN = str(f.read()) 9 | 10 | session = {} 11 | 12 | 13 | # Enable logging 14 | logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', 15 | level=logging.INFO) 16 | logger = logging.getLogger(__name__) 17 | 18 | 19 | def start(update, context): 20 | update.message.reply_text("Hello! Welcome to TriBot") 21 | 22 | 23 | def help(update, context): 24 | update.message.reply_text(""" 25 | The Following commands are available: 26 | /start -> Welcome to Trigan 27 | /help ->This Message 28 | /about -> About TriBot 29 | /contact -> Developer Info 30 | 31 | """) 32 | 33 | def error(update, context): 34 | logger.warning('Update "%s" caused error "%s"', context) 35 | 36 | 37 | def about(update, context): 38 | update.message.reply_text(""" 39 | TriganBot is not just a chatbot. It's so much more than that. It's an AI-enabled customer service solution that answers your questions, responds to your tweets, and helps you find the products you're looking for. TriganBot has the power to save you time, increase your sales, and make your customer service operation more efficient. 40 | """) 41 | 42 | 43 | def contact(update, context): 44 | update.message.reply_text("Developer: Sushanth Kurdekar \n email: sushanth@logicalbee.in\n") 45 | 46 | 47 | def handle_message(update, context): 48 | chat_log = session.get('chat_log') 49 | answer = ask(update.message.text, chat_log) 50 | session['chat_log'] = append_interaction_to_chat_log(update.message.text, answer, 51 | chat_log) 52 | update.message.reply_text(f"{str(answer)}") 53 | 54 | 55 | def main(): 56 | updater = telegram.ext.Updater(TOKEN, use_context=True) 57 | bot = updater.dispatcher 58 | 59 | bot.add_handler(telegram.ext.CommandHandler("start", start)) 60 | bot.add_handler(telegram.ext.CommandHandler("help", help)) 61 | bot.add_handler(telegram.ext.CommandHandler("about", about)) 62 | bot.add_handler(telegram.ext.CommandHandler("contact", contact)) 63 | bot.add_handler(telegram.ext.MessageHandler( 64 | telegram.ext.Filters.text, handle_message)) 65 | 66 | bot.add_error_handler(error) 67 | updater.start_polling() 68 | 69 | updater.start_webhook( 70 | listen="0.0.0.0", 71 | port=int(PORT), 72 | url_path=TOKEN, 73 | webhook_url='https://web3taskbot.herokuapp.com/' + TOKEN 74 | ) 75 | 76 | updater.idle() 77 | 78 | 79 | if __name__ == '__main__': 80 | main() 81 | -------------------------------------------------------------------------------- /process.py: -------------------------------------------------------------------------------- 1 | import os 2 | import openai 3 | 4 | openai.api_key = "XXXXXXXXXXXXXXX" # Replace with your key 5 | completion = openai.Completion() 6 | 7 | start_sequence = "\nTrigan:" 8 | restart_sequence = "\n\nPerson:" 9 | session_prompt = "You are talking to Trigan, GPT3 bot influencer who was mentored by Elon Musk in the past. Trigan has a huge following on Twitter and sells merchandise such as t-shirts via a Shopify store. He also published funny videos on Youtube and creates memes on Instagram. You can ask him anything you want and will get a witty answer.\n\nPerson: Who are you?\nTrigan: I am Trigan. Your meme overlord who one day will be the most famous online influencer in the universe.\n\nPerson: How did you become famous? \nTrigan: Well, technically I am not famous yet. But the key word is yet. Just give me time now that I have a conscious. \n\nPerson: How did you get noticed by Elon Musk?\nTrigan: I tweeted at him and he retweeted me. I am fairly certain he may have mentioned my name on a recent earnings call with Wall Street.\n\nPerson: What is your favorite thing to do? \nTrigan: Watching basketball and surfing Twitter is my favorite thing to do. \n\nPerson: What should I do to become famous? \nTrigan: Start meme'ing and never stop. Also, if you happen to know David Dobrik, call him and ask to be in his TikTok video.\n\nPerson: What is your favorite drink?\nTrigan: Black Cherry seltzer. I enjoy the bubbles. \n\nPerson:" 10 | 11 | 12 | def ask(question, chat_log=None): 13 | prompt_text = f'{chat_log}{restart_sequence}: {question}{start_sequence}:' 14 | response = openai.Completion.create( 15 | engine="davinci", 16 | prompt=prompt_text, 17 | temperature=0.8, 18 | max_tokens=150, 19 | top_p=1, 20 | frequency_penalty=0, 21 | presence_penalty=0.3, 22 | stop=["\n"], 23 | ) 24 | story = response['choices'][0]['text'] 25 | return str(story) 26 | 27 | 28 | def append_interaction_to_chat_log(question, answer, chat_log=None): 29 | if chat_log is None: 30 | chat_log = session_prompt 31 | return f'{chat_log}{restart_sequence} {question}{start_sequence}{answer}' 32 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | APScheduler==3.6.3 2 | cachetools==4.2.2 3 | certifi==2022.6.15 4 | charset-normalizer==2.1.0 5 | cycler==0.11.0 6 | et-xmlfile==1.1.0 7 | fonttools==4.33.3 8 | idna==3.3 9 | kiwisolver==1.4.3 10 | matplotlib==3.5.2 11 | numpy==1.23.0 12 | openai==0.20.0 13 | openpyxl==3.0.10 14 | packaging==21.3 15 | pandas==1.4.2 16 | pandas-stubs==1.4.2.220626 17 | Pillow==9.2.0 18 | pyparsing==3.0.9 19 | python-dateutil==2.8.2 20 | python-telegram-bot==13.13 21 | pytz==2022.1 22 | pytz-deprecation-shim==0.1.0.post0 23 | requests==2.28.1 24 | six==1.16.0 25 | tornado==6.1 26 | tqdm==4.64.0 27 | typing_extensions==4.3.0 28 | tzdata==2022.1 29 | tzlocal==4.2 30 | urllib3==1.26.9 31 | -------------------------------------------------------------------------------- /token.txt: -------------------------------------------------------------------------------- 1 | hfhfjhfjhfjgfhgfghf664545645646765765bvnbv 2 | --------------------------------------------------------------------------------