Telegram introduced Bot Web App View recently, and I need from my bot, which is running in Python, to send a custom payload of data to the site. I know that the site can get stuff like color schemes, user info and so on, but I need my custom packet as well. I really need to use the web app as just an interface to a really, really small database (~30 items) in json. (Other solutions, like storing the database with the website is not useful nor convenient to me)
Telegram最近推出了Bot Web App View,我需要我的机器人(它运行在Python中)向网站发送定制的有效数据负载。我知道,该网站可以获得的东西,如配色方案,用户信息等,但我也需要我的定制包。我真的需要将Web应用程序用作JSON中一个非常非常小的数据库(大约30个项目)的接口。(其他解决方案,如将数据库与网站一起存储,对我来说既没有用处,也不方便)
Is this even possible? I've tried looking everywhere, but I'm not sure.
这有可能吗?我到处都找过了,但我不确定。
更多回答
@CallMeStag I have read them and I launched my WebApp, but I'm not sure, if I can customize the data that telegram will send to the WebApp in python
@CallMeStag我已经读过了,我启动了我的网络应用程序,但我不确定我是否可以定制电报将发送到Python网络应用程序的数据
Custom data cannot be currently transmitted from the bot to the webapp as of April 2022.
从2022年4月起,自定义数据目前不能从机器人传输到网络应用程序。
ofcourse you can, it depends on the webapp you write, following python telegram bot manual i can use my webapp to send payload Json data for my bot.I used javascript in my webapp. for example
1. code in my webapp:
当然可以,这取决于你编写的网络应用程序,遵循python电报机器人手册,我可以使用我的网络应用程序为我的僵尸发送有效负载Json数据。我在我的网络应用程序中使用了Java脚本。例如1.我的网络应用程序中的代码:
<head>
<meta charset="UTF-8">
<script src="https://telegram.org/js/telegram-web-app.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@jaames/iro@5"></script>
</head>
<body>
<script type="text/javascript" defer>
Telegram.WebApp.ready();
Telegram.WebApp.MainButton.setText('Xác nhận').show().onClick(function () {
const data = JSON.stringify({
username: document.querySelector('input[name=username]').value,
password: document.querySelector('input[name=password]').value,
date_start: document.querySelector('input[name=dates]').value,
date_end: document.querySelector('input[name=datee]').value,
ei_type: document.getElementsByName('ei_type')[0].value,
download_ei: [].filter.call(document.getElementsByName('download_ei'), (c) => c.checked).map(c => c.value)
});
Telegram.WebApp.sendData(data);
Telegram.WebApp.close();
});
</script>
<script type="text/javascript">
Telegram.WebApp.expand();
</script>
</body>
2. my Python code:
2.我的Python代码:
#the async function where show up button press and open my webapp
async def downloadEI(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
user = update.callback_query.from_user
usertelid = user.id
msg_id = update.callback_query.message.message_id
userFname = user.first_name
userLname = user.last_name
telusername = user.username
if userFname is None: userFname = ''
if userLname is None: userLname = ''
if telusername is None: telusername = ''
if userFname == '' or userLname == '':
userFullname = telusername
else:
userFullname = userFname+' '+userLname
query = update.callback_query
logger.info("merLoadImgTop-User %s choose: %s",userFullname, str(query.data))
await query.edit_message_text(
f"@{telusername} processing your request..."
)
#sleep(3)
await update.callback_query.message.reply_text(
"press me!",
reply_markup=ReplyKeyboardMarkup.from_button(
KeyboardButton(
text="fill the info",
web_app=WebAppInfo(url="my webapp url")
),
)
),
)
return DOWNLOADEIPROCESS
#the function when catching webapp return data:
async def downloadEIProcess(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
data = update.effective_message.web_app_data.data
异步数据下载EIProcess(UPDATE:UPDATE,CONTEXT:ConextTypes.DEFAULT_TYPE)->无:DATA=update.Effect_Message.web_app_data.data
please give more imformation about your code in order to let me help you out.
请提供更多关于您的代码的信息,以便让我帮助您。
I don't know if you are needing this yet, but I found a way to do the comunication in bot -> webapp. It's the simpler way: with GET params :)
我不知道你是否还需要这个,但我找到了一种在BOT->Webapp中进行通信的方法。这是更简单的方法:使用GET PARAMS:)
You just need to pass the data you want to 'post' to the webapp into a function that converts, in my case, a JSON object format into an HTTP URL parameters. Like this:
您只需要将想要‘POST’到WebApp的数据传递到一个函数中,该函数将JSON对象格式转换为HTTP URL参数。就像这样:
from urllib.parse import quote as urlparse
def dict_to_url_params(params: dict):
# escape characters that are not allowed in url
# example: {'name': 'John Doe?'} -> 'name=John%20Doe%3F'
return "?" + '&'.join([f'{ urlparse(key) }={ urlparse(value) }' for key, value in params.items()])
Then, catch this data in JS, make what you want to, and with a press of a button call a function like this:
然后,在JS中捕获此数据,执行您想要的操作,并按下按钮调用如下函数:
let params = new URLSearchParams(window.location.search)
let name = params.get("name")
let age = params.get("age")
And to get this data in bot, do this:
要在BOT中获取这些数据,请执行以下操作:
import json
@bot.message_handler(content_types="web_app_data")
def answer(wmsg):
userid = wmsg.from_user.id
try:
response = json.loads(wmsg.web_app_data.data)
except:
pass
This is a repository of a real example:
https://github.com/arthurrogado/WebApp-Telegram-Bot
这是一个真实示例的存储库:https://github.com/arthurrogado/WebApp-Telegram-Bot
更多回答
我是一名优秀的程序员,十分优秀!