Python-telegram-bot How to send InlineKeyboard via url - python

Trying to send message with callback keybord attached to it, bot no good. Tels me
TypeError: must be str, not ReplyKeyboardMarkup
Can't find any exemples how to do it correctly.
keyboard = [[InlineKeyboardButton("Выполнено", callback_data='Done')],
[InlineKeyboardButton("MAC", callback_data='MAC'),
InlineKeyboardButton("Phone", callback_data='Phone'),
InlineKeyboardButton("История", callback_data='History')]]
reply_markup = ReplyKeyboardMarkup(keyboard)
requests.post(url='https://api.telegram.org/bot{blah}/'
'sendMessage?chat_id=' + str(query.message.chat_id) + '&text="TEST"&reply_markup=' + reply_markup)

Firstly, you should use InlineKeyboardMarkup instead of ReplyKeyboardMarkup to craete the markup object made of InlineKeyboardButtons.
Then, you probably should simply use the bot object to send the message with bot.send_message(query.message.chat_id, 'TEST', reply_markup=reply_markup).
Lastly, if you really really need to use requests to do manual HTTP request, you should provide the parameters in the requests.post()'s data.
import json
import requests
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
keyboard = [[InlineKeyboardButton("Выполнено", callback_data='Done')],
[InlineKeyboardButton("MAC", callback_data='MAC'),
InlineKeyboardButton("Phone", callback_data='Phone'),
InlineKeyboardButton("История", callback_data='History')]]
reply_markup = InlineKeyboardMarkup(keyboard)
data = {"chat_id": query.message.chat_id,
"text": "TEST",
"reply_markup": json.dumps(reply_markup.to_dict())}
requests.post(url='https://api.telegram.org/bot{blah}/sendMessage', data=data)

Related

DeepAI API error - {"err": "error processing given inputs from request"}

I'm a noobie at Python and was just messing around, but now I'm really curious why it isn't working. I'm currently trying to build a telegram bot that generates an image based on the text given to the bot. I think there might be a problem with my DeepAI api? When I click this link: https://api.deepai.org/api/text2img i always get the error
{"err": "error processing given inputs from request"}
. The bot is linked but gives me the error as in the code below. My code below:
import requests
import json
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
def text_to_image(update, context):
# Get the text from the user
text = update.message.text
# Set up the DeepAI API request
api_key = "{{ api }}"
headers = {
"api-key": api_key
}
data = {
"text": text
}
# Make the request to the DeepAI API
response = requests.post("https://api.deepai.org/api/text2img", headers=headers, json=data)
if response.status_code != 200:
update.message.reply_text("An error occurred while generating the image. Please try again later.")
return
# Get the generated image from the response
response_json = response.json()
image_url = response_json["output_url"]
# Send the generated image to the user
context.bot.send_photo(chat_id=update.effective_chat.id, photo=image_url)
def main():
# Set up the Telegram bot
updater = Updater(token="{{ api }}", use_context=True)
dispatcher = updater.dispatcher
# Add the text_to_image handler
text_to_image_handler = MessageHandler(Filters.text, text_to_image)
dispatcher.add_handler(text_to_image_handler)
# Start the bot
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
I've tried changing code and different things but nothing seems to work, i'm really stuck right now
From the looks of it, it's an error on the API side of things. The status code of the API server depends on traffic and account/key.
Things to do:
Check if your API key is valid and wait when server works on google, then try to run your code

How to get users's location and Venue in a bot telegram by telepot

I'm creating a bot with Python telepot for the bus itineraries. I don't understand how to manage commands, can anyone explain to me how I can get the position (street name) of a user who uses my bot?
import json
import time
from pprint import pprint
import telepot
from telepot.loop import MessageLoop
import telepot.namedtuple
bot = telepot.Bot("token")
lista = ["New York","Los Angeles","Miami","Toronto","Berlin","Rome"]
seq = iter(lista)
keyboard = {"keyboard": [[{"text": i} for i in pair] for pair in zip(seq)]}
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
print(content_type, chat_type, chat_id)
if content_type == "text":
bot.sendMessage(chat_id, msg["text"], reply_markup=keyboard)
bot.editMessageLiveLocation(latitude, longitude) #i've tried here but nothing
MessageLoop(bot, handle).run_as_thread()
while 1:
time.sleep(10)
You didn't show us printed output for the msg response that came back.
Within the if, the 1st line makes sense, it conforms to
https://telepot.readthedocs.io/en/latest/reference.html#telepot.Bot.sendMessage.
The 2nd line makes little sense, it does not pass in the parameters that
https://telepot.readthedocs.io/en/latest/reference.html#telepot.Bot.editMessageLiveLocation
explains you need.
I see no msg_identifier.

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

slack python api delete message

I am trying to use my bot to delete certain messages in a slack channel with this api call
import os
import time
import re
from slackclient import SlackClient
slack_client = SlackClient(
'xsssssseeeeeeee')
slack_mute_bot_id = None
def delete_message(slack_event):
for event in slack_event:
if event["type"] == "message":
message_text = event['text']
time_stamp = event['ts']
channel_id = event['channel']
slack_client.api_call(
'chat.delete',
channel=channel_id,
ts=time_stamp,
as_user=True
)
print(message_text + " delted")
if __name__ == "__main__":
if slack_client.rtm_connect(with_team_state=False):
slack_mute_bot_id = slack_client.api_call("auth.test")["user_id"]
while True:
# print(slack_client.rtm_read())
delete_message(slack_client.rtm_read())
time.sleep(1)
else:
print("Connection failed. Exception traceback printed above.")
I do not get any error message after doing this and the bot does not delete the message. I am using the bot user token. I have benn able to send message succesfully but the delete method does not work and still gives np responses
Refer - https://api.slack.com/methods/chat.delete
When used with a user token, this method may only delete messages
that user themselves can delete in Slack.
When used with a bot token, this method may delete only messages
posted by that bot.
I got stumbled into the same thing.

ReplyKeyboardMarkup doesn't work python?

I wrote a telegram bot. All things went well and it worked well. But when I want to use ReplyKeyboardMarkup as it mentioned in its document, it did not work! I mean the keyboard doesn't show up.
This JSON object has a key keyboard and its value ,according to its doc, is:
type: Array of Array of String.
description: Array of button rows, each represented by an Array of Strings
This is my code for sending the request:
reply_markup = {'keyboard': [['1'],['2']], 'resize_keyboard': True, 'one_time_keyboard': True}
params = urllib.urlencode({
'chat_id': str(chat_id),
'text': msg.encode('utf-8'),
'reply_markup': reply_markup,
'disable_web_page_preview': 'true',
# 'reply_to_message_id': str(message_id),
})
resp = urllib2.urlopen(BASE_URL + 'sendMessage', params).read()
You have to serialize reply_markup to JSON string separately, like in this answer Telegram bot custom keyboard in PHP
Because it still took me a bit of trial and error to get in right in Python even after reading the PHP answer linked to by Kostya, here is a the adapted Python code that works (you just need to add your bot's token and a chat ID to send the message to).
Note that I also had to update my Telegram client to the newest version (currently 3.1) to see the result.
import urllib
import urllib2
import json
TOKEN = "<your bot token>"
chat_id = <your chat id>
msg = "some string"
BASE_URL = "https://api.telegram.org/bot{}/".format(TOKEN)
reply_markup = {'keyboard': [['1'],['2']], 'resize_keyboard': True, 'one_time_keyboard': True}
reply_markup = json.dumps(reply_markup)
params = urllib.urlencode({
'chat_id': str(chat_id),
'text': msg.encode('utf-8'),
'reply_markup': reply_markup,
'disable_web_page_preview': 'true',
# 'reply_to_message_id': str(message_id),
})
resp = urllib2.urlopen(BASE_URL + 'sendMessage', params).read()
If you like you can try this method
import telegram
from telegram.ext import Updater
updater = Updater(token='BOT_TOKEN')
dispatcher = updater.dispatcher
updater.start_polling()
def test(bot, update):
results = bot.sendMessage(chat_id=update.message.chat_id, text="Test", reply_markup={"keyboard":[["Test1"], ["Test2"], ["Test3"], ["Test4"]})
print results
dispatcher.addTelegramCommandHandler('test', test)
This import makes things a lot more short, I just started using it today python-telegram-bot 3.4
The following snippet will work:
reply_markup = {
"keyboard": [[{"text":"1"}], [{"text":"2"}]],
"resize_keyboard": True,
"one_time_keyboard": True
}

Categories

Resources