Telegram Bot (2021.01) === ###### tags: `Side Project`, `Python` ## Brief 接收用戶傳來bot的訊息,回傳一張喵喵圖片。 ## Result   ## Code ``` import telepot import time from telepot.loop import MessageLoop from pprint import pprint import requests import json bot = telepot.Bot('telegram bot token') def handle(msg): pprint(msg) chat_id = msg['chat']['id'] from_id = msg['from']['id'] #cat api DATA = "https://api.thecatapi.com/v1/images/search" req = requests.get(DATA) req_json = req.json() img_url = req_json[0]['url'] photo = img_url bot.sendPhoto(chat_id, photo) MessageLoop(bot, handle).run_as_thread() print("I'm listening...") while 1: time.sleep(5) ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up