8000 Create telegram_typping.py · gjmulder/llama-cpp-python-gary@eda3268 · GitHub
[go: up one dir, main page]

Skip to content

Commit eda3268

Browse files
Create telegram_typping.py
1 parent 32ca803 commit eda3268

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

llama_cpp/telegram_typping.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import logging
2+
import asyncio
3+
import aiohttp
4+
from telegram import ChatAction
5+
from telegram.ext import Updater, CommandHandler
6+
7+
logging.basicConfig(
8+
level=logging.INFO,
9+
format='%(asctime)s %(levelname)s: %(message)s',
10+
handlers=[
11+
logging.FileHandler('bot.log'),
12+
logging.StreamHandler()
13+
]
14+
)
15+
16+
TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
17+
18+
async def fetch(url):
19+
async with aiohttp.ClientSession() as session:
20+
async with session.get(url) as response:
21+
return await response.text()
22+
23+
def start(update, context):
24+
update.message.reply_text("Hi! Type /simulate to simulate the bot typing.")
25+
logging.info("User %s started the bot", update.message.from_user.id)
26+
27+
async def simulate(update, context):
28+
chat_id = update.message.chat_id
29+
context.bot.send_chat_action(chat_id, ChatAction.TYPING)
30+
logging.info("Bot is typing a response...")
31+
32+
url = 'https://httpbin.org/get'
33+
logging.info("Fetching API response from %s", url)
34+
35+
loop = asyncio.get_event_loop()
36+
response_text = await fetch(url)
37+
38+
update.message.reply_text("API response received: " + response_text)
39+
logging.info("Sent response to user %s", update.message.from_user.id)
40+
41+
def stop(update, context):
42+
update.message.reply_text("Stopping the bot...")
43+
logging.info("User %s stopped the bot", update.message.from_user.id)
44+
context.bot.stop()
45+
46+
def main():
47+
updater = Updater(TOKEN, use_context=True)
48+
49+
dp = updater.dispatcher
50+
dp.add_handler(CommandHandler("start", start))
51+
dp.add_handler(CommandHandler("simulate", simulate))
52+
dp.add_handler(CommandHandler("stop", stop))
53+
54+
updater.start_polling()
55+
logging.info("Bot started polling for updates...")
56+
updater.bot.send_message(chat_id="@YOUR_CHANNEL_ID", text="Bot started.")
57+
updater.idle()
58+
updater.bot.send_message(chat_id="@YOUR_CHANNEL_ID", text="Bot stopped.")
59+
logging.info("Bot stopped polling for updates.")
60+
61+
if __name__ == "__main__":
62+
asyncio.run(main())

0 commit comments

Comments
 (0)
0