-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
76 lines (60 loc) · 2.68 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import json
import logging
from uuid import uuid4
from telegram import Update, InlineQueryResultArticle, InputTextMessageContent
from telegram.ext import (filters, ApplicationBuilder, ContextTypes,
CommandHandler, MessageHandler, CallbackQueryHandler,
InlineQueryHandler)
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
# reads your token from environment variables
TOKEN = os.getenv("TELEGRAM_TOKEN")
# the function that runs when /start is sent to the bot
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat_id = update.effective_chat.id
await context.bot.send_message(chat_id,
"""Hi I'm a telegram bot template! """)
# the function that recieves text messages from users
async def recieve_message(update: Update, context):
if update.effective_chat.type != 'private': # there's the inline mode for groups
return
await update.message.reply_text(
text='say something??',
reply_to_message_id=update.message.message_id,
parse_mode="HTML")
# recieves the keyboard button clicks
async def get_keyboad_reply(update: Update, context, optional_pram=None):
chat_id = update.effective_chat.id
message = update.callback_query.data
# recieves voice messages
async def get_voice(update: Update, context: ContextTypes.DEFAULT_TYPE):
audio_file = await context.bot.get_file(update.message.voice.file_id)
await audio_file.download_to_drive(update.message.voice.file_id + '.ogg')
await context.bot.send_message(update.effective_chat.id, 'listening...')
async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.inline_query.query
if query == '':
return
results = [
InlineQueryResultArticle(
id=str(uuid4()),
title='some item',
input_message_content=InputTextMessageContent('item_data'))
]
await update.inline_query.answer(results)
if __name__ == '__main__':
application = ApplicationBuilder().token(TOKEN).build()
application.add_handler(
CallbackQueryHandler(get_keyboad_reply, block=False))
application.add_handler(
CommandHandler(['start', 'help'], start, block=False))
application.add_handler(
MessageHandler(filters.TEXT & (~filters.COMMAND),
recieve_message,
block=False))
application.add_handler(MessageHandler(filters.VOICE, get_voice))
application.add_handler(InlineQueryHandler(inline_query))
application.run_polling()