How to send file from server using telebot? - python

I made a bot that sends files from an external server with a URL.
I want the bot to send files directly from your server.
What am I doing wrong? Why didn't the open() command work?
import telebot
bot = telebot.TeleBot("Token")
#bot.message_handler(commands=['start','help'])
def send_start_message(message):
bot.reply_to(message, "Hi.")
#bot.message_handler(func=lambda m: True)
def echo_all(message):
print(message.text)
duck = open('duck.png','r')
bot.send_photo(message.chat.id, duck)
bot.send_message(message.chat.id, "hi")
bot.polling()

You'll need to tell python3 to use binary mode;
duck = open('duck.png', 'rb')
Read more about open()
Minimal working example to send local image:
import telebot
bot = telebot.TeleBot("<YOUR-TOKEN-HERE>")
cid = <YOUR-ID-HERE>
img = open('image.jpg', 'rb')
bot.send_photo(cid, img)

Related

Discord.py 2.0 on Repl.it

I want to make discord bot that uses buttons I downloaded library and ewerything. It works but because I am using Repl.it i have to use web + uptimerobot so it will never turn off.
Even after that it seems every few hours to quickly turn off and back on, so it will lose all data except databases. I was thinking i can solve this by saving message to database and inside event on_ready delete message and create new one but bot after going off and on cant delete message from varriable.
Code for web:
from flask import Flask
from threading import Thread
app = Flask('')
#app.route('/')
def home():
return "I am working!"
def run():
app.run(host='0.0.0.0',port=8080)
def keep_alive():
t = Thread(target=run)
t.start()
I would like to solve turning off and on problem + it will be helpfull to fix deleting message from database after reseting.
Side question: Do you guys know any easy way to assign instead of reference value to varriable because of circular reference/dependency.
Make a JSON file to save the data and just have it not delete the data on restart. have it autosave when a user uses it.
kinda like this
#client.command()
async def add(ctx, meme_):
def add_memes(meme, file="memes.json"):
with open(file, "r+") as fw:
j = json.load(fw)
j["memes"].append(meme)
with open(file, "w+") as wp:
wp.write(json.dumps(j))
try:
with open("memes.json", "r"):
pass
except:
with open("memes.json", "w+") as wp:
wp.write('{"memes" : []}')
finally:
add_memes(meme_)
await ctx.send("Done!")
but this is a command for saving memes

a python-telegram-bot Unsupported url protocol

So, I was Trying To Create a Telegram Bot That By Using The PyTube Moudle, Will Download The Video To My Computer And Then Send It To The User By The send_document Function.
My Problem Is When I Need To Send The Video, I Get This Error:
telegram.error.BadRequest: Invalid file http url specified: unsupported url protocol
I've Tried It On a Regular Text File And Got The Same Results.
I Assume It's Because The Url of The File Is Breaking Some Rules of How It Should Look Like, But I Don't Know How To Fix It...
Also Here Is My Code:
import telegram.ext
import telegram
from telegram.ext.messagehandler import MessageHandler
from telegram.ext.commandhandler import CommandHandler
from telegram.ext.filters import Filters
import time
import pytube
import os
with open('C:/Users/EvilTwin/Desktop/stuff/Python/API/Telegram/Download Youtube Videos Bot/token.txt')as file:
API_KEY = file.read()
updater = telegram.ext.Updater(token=API_KEY, use_context=True)
def start(update, context):
update.message.reply_text(f'Hello {update.effective_user.first_name}, I\'m Roee\'s Video Downloader!')
time.sleep(1)
update.message.reply_text(
f'''
To Download Videos Please Enter:
/download + Video URL + Format
''')
def download(update, context):
URL = context.args[0]
FORMAT = context.args[1]
VIDEO = pytube.YouTube(URL)
FILE = VIDEO.streams.filter(progressive=True, file_extension=FORMAT).order_by('resolution').desc().first()
VD = 'C:/Users/EvilTwin/Desktop/stuff/Python/API/Telegram/Download Youtube Videos Bot/Videos'
FILE.download(VD)
banned = ['/', '/-\\', ':', '?', '!', '*', '>', '<', '"', '|']
for ban in banned:
VIDEO.title = VIDEO.title.replace(ban, '')
time.sleep(1)
DOC = f'{VD}/{VIDEO.title}.{FORMAT}'
chat_id=update.effective_chat.id
context.bot.send_document(chat_id = chat_id ,document=DOC, filename = f'video.{FORMAT}')
os.remove(DOC)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('download', download))
updater.start_polling()
updater.idle()
If Anyone Knows How To Fix It Please Help Me....
For local file you have to use file object (file handler) instead of file name
so you have to open it document=open(DOC, 'rb')
context.bot.send_document(chat_id=chat_id, document=open(DOC, 'rb'), filename=f'video.{FORMAT}')
Documentation shows also that you can use pathlib.Path or bytes (so it needs to read it document=open(DOC, 'rb').read()) but I didn't test it.
See doc: send_document()

Unable to send file to Telegram Channel

Error below:
Failed to convert James Blake - Never Dreamed.mp4 to media. Not an existing file, an HTTP URL or a valid bot-API-like file ID
Function that I am running is here.
async def gramain():
# Getting information about yourself
me = await bott.get_me()
print(me.stringify())
filesww = os.listdir('media').pop()
now = os.getcwd()
mediadir = now + "/media/"
vid = open(mediadir+filesww, 'r')
# await bot.send_message(chat, 'Hello, friend!')
await bott.send_file(chat, filesww, video_note=True)
I trying to use this function from the library Telethon.
https://docs.telethon.dev/en/latest/modules/client.html#telethon.client.uploads.UploadMethods.send_file
I am unable to find out what the issue is. Please help!

Send a local photo from inline mode in a telegram bot

I use Python telegram bot API for my bot.
I want to generate photos locally and send them as inline results but InlineQueryResultPhoto accepts only photo URLs.
Suppose my project structure looks like this:
main.py
photo.jpg
How do I send photo.jpg as an inline result?
Here is the code of main.py:
from uuid import uuid4
from telegram.ext import InlineQueryHandler, Updater
from telegram import InlineQueryResultPhoto
def handle_inline_request(update, context):
update.inline_query.answer([
InlineQueryResultPhoto(
id=uuid4(),
photo_url='', # WHAT DO I PUT HERE?
thumb_url='', # AND HERE?
)
])
updater = Updater('TELEGRAM_TOKEN', use_context=True)
updater.dispatcher.add_handler(InlineQueryHandler(handle_inline_request))
updater.start_polling()
updater.idle()
There is no direct answer because Telegram Bot API doesn't provide it.
But there are two workaounds: you can use upload a photo to telegram servers and then use InlineQueryResultCachedPhoto or you can upload to any image server and then use InlineQueryResultPhoto.
InlineQueryResultCachedPhoto
This first option requires you to previously upload the photo to telegram servers before creating the result list. Which options do you have? The bot can message you the photo, get that information and use what you need. Another option is creating a private channel where your bot can post the photos it will reuse. The only detail of this method is getting to know the channel_id (How to obtain the chat_id of a private Telegram channel?).
Now lets see some code:
from config import tgtoken, privchannID
from uuid import uuid4
from telegram import Bot, InlineQueryResultCachedPhoto
bot = Bot(tgtoken)
def inlinecachedphoto(update, context):
query = update.inline_query.query
if query == "/CachedPhoto":
infophoto = bot.sendPhoto(chat_id=privchannID,photo=open('logo.png','rb'),caption="some caption")
thumbphoto = infophoto["photo"][0]["file_id"]
originalphoto = infophoto["photo"][-1]["file_id"]
results = [
InlineQueryResultCachedPhoto(
id=uuid4(),
title="CachedPhoto",
photo_file_id=originalphoto)
]
update.inline_query.answer(results)
when you send a photo to a chat/group/channel, you can obtain the file_id, the file_id of the thumbnail, the caption and other details I'm going to skip. What the problem? If you don't filter the right query, you may end up sending the photo multiple times to your private channel. It also means the autocomplete won't work.
InlineQueryResultPhoto
The other alternative is upload the photo to internet and then use the url. Excluding options like your own hosting, you can use some free image hostings that provides APIs (for example: imgur, imgbb). For this code, generating your own key in imgbb is simpler than imgur. Once generated:
import requests
import json
import base64
from uuid import uuid4
from config import tgtoken, key_imgbb
from telegram import InlineQueryResultPhoto
def uploadphoto():
with open("figure.jpg", "rb") as file:
url = "https://api.imgbb.com/1/upload"
payload = {
"key": key_imgbb,
"image": base64.b64encode(file.read()),
}
response = requests.post(url, payload)
if response.status_code == 200:
return {"photo_url":response.json()["data"]["url"], "thumb_url":response.json()["data"]["thumb"]["url"]}
return None
def inlinephoto(update, context):
query = update.inline_query.query
if query == "/URLPhoto":
upphoto = uploadphoto()
if upphoto:
results = [
InlineQueryResultPhoto(
id=uuid4(),
title="URLPhoto",
photo_url=upphoto["photo_url"],
thumb_url=upphoto["thumb_url"])
]
update.inline_query.answer(results)
This code is similar to the previous method (and that includes the same problems): uploading multiple times if you don't filter the query and you won't have the autocomplete when writing the inline.
Disclaimer
Both code were written thinking the images you want to upload are generated at the moment you receive the query, otherwise you can do the work previous to receiving the query, saving that info in a database.
Bonus
You can run your own bot to get the channel_id of your private channel with pyTelegramBotAPI
import telebot
bot = telebot.TeleBot(bottoken)
#bot.channel_post_handler(commands=["getchannelid"])
def chatid(message):
bot.reply_to(message,'channel_id = {!s}'.format(message.chat.id))
bot.polling()
To get the id you need to write in the channel /getchannelid#botname

How to send photo on telegram bot

i'm just implementing a simple bot who should send some photos and videos to my chat_id.
Well, i'm using python, this is the script
import sys
import time
import random
import datetime
import telepot
def handle(msg):
chat_id = msg['chat']['id']
command = msg['text']
print 'Got command: %s' % command
if command == 'command1':
bot.sendMessage(chat_id, *******)
elif command == 'command2':
bot.sendMessage(chat_id, ******)
elif command == 'photo':
bot.sendPhoto(...)
bot = telepot.Bot('*** INSERT TOKEN ***')
bot.message_loop(handle)
print 'I am listening ...'
while 1:
time.sleep(10)
In the line bot.sendphoto I would insert the path and the chat_id of my image but nothing happens.
Where am I wrong?
thanks
If you have local image path:
bot.send_photo(chat_id, photo=open('path', 'rb'))
If you have url of image from internet:
bot.send_photo(chat_id, 'your URl')
Just using the Requests lib you can do it:
def send_photo(chat_id, file_opened):
method = "sendPhoto"
params = {'chat_id': chat_id}
files = {'photo': file_opened}
resp = requests.post(api_url + method, params, files=files)
return resp
send_photo(chat_id, open(file_path, 'rb'))
I have used the following command while using python-telegram-bot to send the image along with a caption:
context.bot.sendPhoto(chat_id=chat_id, photo=
"url_of_image", caption="This is the test photo caption")
I've tried also sending from python using requests. Maybe it's late answer, but leaving this here for others like me.. maybe it'll come to use..
I succeded with subprocess like so:
def send_image(botToken, imageFile, chat_id):
command = 'curl -s -X POST https://api.telegram.org/bot' + botToken + '/sendPhoto -F chat_id=' + chat_id + " -F photo=#" + imageFile
subprocess.call(command.split(' '))
return
This is complete code to send a photo in telegram:
import telepot
bot = telepot.Bot('______ YOUR TOKEN ________')
# here replace chat_id and test.jpg with real things
bot.sendPhoto(chat_id, photo=open('test.jpg', 'rb'))
You need to pass 2 params
bot.sendPhoto(chat_id, 'URL')
sendPhoto requires at least two parameters; first one is target chat_id, and for second one photo you have three options:
Pass file_id if the photo is already uploaded to telegram servers (recommended because you don't need to reupload it).
If the photo is uploaded somewhere else, pass the full http url and telegram will download it (max photo size is 5MB atm).
Post the file using multipart/form-data like you want to upload it via a browser (10MB max photo size this way).

Categories

Resources