Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f6b812f

Browse files
Merge pull request avinashkranjan#334 from Sharanya2019/Telegram_bot
Telegram bot
2 parents 1a3a32f + e0de8e6 commit f6b812f

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

‎TelegramBot.py‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from telegram.ext import Updater, InlineQueryHandler, CommandHandler, MessageHandler,Filters
2+
import requests
3+
import re
4+
5+
#function to get contents of url(public api) using requests
6+
def gett():
7+
contents = requests.get('https://random.dog/woof.json').json()
8+
url = contents['url']
9+
return url
10+
11+
#function to check allowed extension
12+
def image_urll():
13+
extension = ['jpg','jpeg','png']
14+
ext = ''
15+
while ext not in extension:
16+
url = gett()
17+
ext = re.search("([^.]*)$",url).group(1).lower()
18+
return url
19+
20+
def get():
21+
contents = requests.get('https://xkcd.com/info.0.json').json()
22+
img = contents['img']
23+
return img
24+
25+
def image_url():
26+
extension = ['jpg','jpeg','png']
27+
ext = ''
28+
while ext not in extension:
29+
img = get()
30+
ext = re.search("([^.]*)$",img).group(1).lower()
31+
return img
32+
33+
#function to display dog picture
34+
def dog(update, context):
35+
url = image_urll()
36+
context.bot.send_photo(chat_id=update.effective_chat.id, photo = url)
37+
38+
#function to display meme
39+
def meme(update, context):
40+
img = image_url()
41+
context.bot.send_photo(chat_id=update.effective_chat.id, photo = img)
42+
43+
#Welcome message will be displayed when /hi command is sent
44+
def hi(update, context):
45+
context.bot.send_message(chat_id = update.effective_chat.id, text="Hi! I am telebot...How is it going.")
46+
47+
#tongue-twister will be displayed when /play command is sent
48+
def play(update, context):
49+
context.bot.send_message(chat_id = update.effective_chat.id, text="Let's have fun!Repeat this tongue twister 5 times:She sells seashells by the seashore.")
50+
51+
#Displays message when incorrect command is sent
52+
def unknown(update, context):
53+
context.bot.send_message(chat_id=update.effective_chat.id, text="Please type the right command.")
54+
55+
#your token token has to be provided in <<YOUR-TOKEN>> place
56+
def main():
57+
upd = Updater('<<YOUR-TOKEN>>', use_context=True)
58+
disp = upd.dispatcher
59+
unknown_handler = MessageHandler(Filters.command, unknown)
60+
hi_handler = CommandHandler('hi', hi)
61+
disp.add_handler(CommandHandler('meme', meme))
62+
disp.add_handler(CommandHandler('play', play))
63+
disp.add_handler(CommandHandler('dog',dog))
64+
disp.add_handler(hi_handler)
65+
disp.add_handler(unknown_handler)
66+
upd.start_polling()
67+
upd.idle()
68+
69+
main()

‎pic1 (1).jpeg‎

80.7 KB
Loading[フレーム]

‎pic2 (1).jpeg‎

101 KB
Loading[フレーム]

‎pic3 (1).jpeg‎

66 KB
Loading[フレーム]

‎readme.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#TelegramBot
2+
3+
1. Create a bot in telegram:
4+
5+
Go to @BotFather and click /start and type /newbot and give it a name. Then choose a username and get the token which has to be placed in "YOUR-TOKEN" place in the code
6+
7+
2. Install required packages:
8+
9+
pip install requests
10+
11+
pip install python-telegram-bot
12+
13+
3. Type /hi command to start conversation with the chatbot.
14+
15+
4. Pictures/memes are displayed based on the public apis given in the code

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /