尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
telegram - advanced-features
高级功能 - Telegram 机器人目录内联模式支付Telegram Stars迷你应用WebApps对话处理器FSM贴纸游戏Passport企业机器人Business Bots消息草稿流式输出内联模式允许用户在任何聊天中输入你的机器人 查询来使用机器人。启用与 BotFather 对话并发送/setinline。fromtelegramimportUpdate,InlineQueryResultArticle,InputTextMessageContentfromtelegram.extimportInlineQueryHandlerasyncdefinline_query(update:Update,context):queryupdate.inline_query.queryifnotquery:returnresults[InlineQueryResultArticle(id1,titlefResultado para:{query},input_message_contentInputTextMessageContent(message_textfVoce buscou:{query}),descriptionClique para enviar),InlineQueryResultArticle(id2,titleBusca em maiusculas,input_message_contentInputTextMessageContent(message_textquery.upper()))]awaitupdate.inline_query.answer(results,cache_time10)app.add_handler(InlineQueryHandler(inline_query))内联结果类型InlineQueryResultArticle- 通用文本InlineQueryResultPhoto- 带预览的照片InlineQueryResultGif- GIFInlineQueryResultVideo- 视频InlineQueryResultAudio- 音频InlineQueryResultDocument- 文档InlineQueryResultLocation- 位置InlineQueryResultVenue- 地点/场所InlineQueryResultContact- 联系人InlineQueryResultCachedPhoto- 已在 Telegram 服务器上的照片支付Telegram StarsTelegram 允许通过 Stars内部货币为数字商品进行支付。fromtelegramimportLabeledPrice# Enviar invoiceawaitbot.send_invoice(chat_idchat_id,titleAssinatura Premium,descriptionAcesso premium por 30 dias,payloadpremium_30days,currencyXTR,# XTR Telegram Starsprices[LabeledPrice(Assinatura Premium,100)],# 100 Stars)# Handler de pre-checkoutasyncdefprecheckout(update:Update,context):queryupdate.pre_checkout_queryifquery.invoice_payloadpremium_30days:awaitquery.answer(okTrue)else:awaitquery.answer(okFalse,error_messagePayload invalido)# Handler de pagamento concluidoasyncdefsuccessful_payment(update:Update,context):paymentupdate.message.successful_paymentawaitupdate.message.reply_text(fPagamento recebido!{payment.total_amount}Stars. fSeu acesso premium foi ativado.)app.add_handler(PreCheckoutQueryHandler(precheckout))app.add_handler(MessageHandler(filters.SUCCESSFUL_PAYMENT,successful_payment))外部支付提供商对于实物商品使用 Stripe、YooMoney 等提供商awaitbot.send_invoice(chat_idchat_id,titleCamiseta Bot Telegram,descriptionCamiseta tamanho M, algodao,payloadtshirt_m,provider_tokenSEU_PROVIDER_TOKEN,# do BotFathercurrencyBRL,prices[LabeledPrice(Camiseta,5990),# R$ 59.90 (em centavos)LabeledPrice(Frete,1500)# R$ 15.00],need_shipping_addressTrue,need_nameTrue,need_phone_numberTrue)迷你应用WebApps迷你应用是在 Telegram 内运行的 Web 应用。fromtelegramimportWebAppInfo,InlineKeyboardButton,InlineKeyboardMarkup# Botao que abre Mini AppkeyboardInlineKeyboardMarkup([[InlineKeyboardButton(Abrir App,web_appWebAppInfo(urlhttps://seu-app.com))]])awaitbot.send_message(chat_id,Clique para abrir:,reply_markupkeyboard)# Via Reply KeyboardfromtelegramimportReplyKeyboardMarkup,KeyboardButton keyboardReplyKeyboardMarkup([[KeyboardButton(Abrir App,web_appWebAppInfo(urlhttps://seu-app.com))]])接收迷你应用的数据asyncdefweb_app_data(update:Update,context):dataupdate.effective_message.web_app_data.data# data e uma string JSON enviada pelo Mini Appimportjson parsedjson.loads(data)awaitupdate.message.reply_text(fRecebi do app:{parsed})app.add_handler(MessageHandler(filters.StatusUpdate.WEB_APP_DATA,web_app_data))对话处理器FSM用于多步骤对话表单、向导fromtelegram.extimportConversationHandler,CommandHandler,MessageHandler,filters# EstadosNAME,AGE,CONFIRMrange(3)asyncdefstart_form(update:Update,context):awaitupdate.message.reply_text(Qual seu nome?)returnNAMEasyncdefget_name(update:Update,context):context.user_data[name]update.message.textawaitupdate.message.reply_text(Qual sua idade?)returnAGEasyncdefget_age(update:Update,context):context.user_data[age]update.message.text namecontext.user_data[name]agecontext.user_data[age]keyboardInlineKeyboardMarkup([[InlineKeyboardButton(Confirmar,callback_dataconfirm),InlineKeyboardButton(Cancelar,callback_datacancel)]])awaitupdate.message.reply_text(fNome:{name}\nIdade:{age}\n\nConfirma?,reply_markupkeyboard)returnCONFIRMasyncdefconfirm(update:Update,context):queryupdate.callback_queryawaitquery.answer()ifquery.dataconfirm:awaitquery.edit_message_text(Cadastro realizado com sucesso!)else:awaitquery.edit_message_text(Cadastro cancelado.)returnConversationHandler.ENDasyncdefcancel(update:Update,context):awaitupdate.message.reply_text(Operacao cancelada.)returnConversationHandler.END conv_handlerConversationHandler(entry_points[CommandHandler(cadastro,start_form)],states{NAME:[MessageHandler(filters.TEXT~filters.COMMAND,get_name)],AGE:[MessageHandler(filters.TEXT~filters.COMMAND,get_age)],CONFIRM:[CallbackQueryHandler(confirm)],},fallbacks[CommandHandler(cancel,cancel)],conversation_timeout300# 5 minutos timeout)app.add_handler(conv_handler)贴纸# Enviar sticker por file_idawaitbot.send_sticker(chat_id,stickerCAACAgIAAxkBAAI...)# Obter sticker setsticker_setawaitbot.get_sticker_set(set_name)forstickerinsticker_set.stickers:print(fEmoji:{sticker.emoji}, ID:{sticker.file_id})# Criar sticker set (requer imagem 512x512 PNG/WEBP)awaitbot.create_new_sticker_set(user_iduser_id,namemeupack_by_meubot,titleMeu Sticker Pack,stickers[InputSticker(stickeropen(sticker.webp,rb),emoji_list[],formatstatic)])# Adicionar sticker ao setawaitbot.add_sticker_to_set(user_iduser_id,namemeupack_by_meubot,stickerInputSticker(stickeropen(sticker2.webp,rb),emoji_list[],formatstatic))游戏# Enviar jogo (precisa registrar no BotFather com /newgame)awaitbot.send_game(chat_id,game_short_namemeu_jogo)# Handler de callback para jogoasyncdefgame_callback(update:Update,context):queryupdate.callback_queryawaitquery.answer(urlhttps://seu-jogo.com/?user_idstr(query.from_user.id))# Salvar scoreawaitbot.set_game_score(user_iduser_id,score150,chat_idchat_id,message_idgame_message_id)# Obter high scoresscoresawaitbot.get_game_high_scores(user_iduser_id,chat_idchat_id,message_idgame_message_id)企业机器人Business Bots用于 Telegram 企业账户的机器人# Receber conexao businessasyncdefbusiness_connection(update:Update,context):connupdate.business_connectionifconn.is_enabled:print(fBot conectado ao business de{conn.user.first_name})else:print(fBot desconectado do business de{conn.user.first_name})# Receber mensagens businessasyncdefbusiness_message(update:Update,context):msgupdate.business_message# Responder em nome do businessawaitcontext.bot.send_message(chat_idmsg.chat.id,textObrigado pela mensagem! Responderemos em breve.,business_connection_idmsg.business_connection_id)app.add_handler(BusinessConnectionHandler(business_connection))app.add_handler(BusinessMessagesHandler(business_message))消息草稿流式输出对于较长的回复如 AI 的回复使用草稿提供实时反馈importrequests TOKENSEU_TOKENBASEfhttps://api.telegram.org/bot{TOKEN}defstream_response(chat_id,full_text):Simula streaming enviando drafts parciais.wordsfull_text.split()partial# Enviar draft inicialfori,wordinenumerate(words):partialword ifi%50:# Atualizar a cada 5 palavrasrequests.post(f{BASE}/sendMessageDraft,json{chat_id:chat_id,text:partial.strip()...})# Enviar mensagem finalrequests.post(f{BASE}/sendMessage,json{chat_id:chat_id,text:full_text})注意sendMessageDraft是一个较新的方法。请在你使用的 API 版本中检查其可用性。
RELATED

相关推荐

RS-232与DB-9串口通信完全指南:引脚定义、接线与万用表实测技巧

RS-232与DB-9串口通信完全指南:引脚定义、接线与万用表实测技巧

最近帮一个朋友调试一台老旧工控设备,对方很无奈地摊手说:“这破机器居然只有串口,连个网口都没有。”我拿起那根积灰多年的DB-9线缆,突然意识到一个事实:哪怕在USB、以太网、CAN FD满天飞的今天,RS-232和D…

📅 2026/10/12 4:22:05
LKDS3.Linux内核的双向链表代码解析(3) 进程树遍历(上) 前置知识: children、sibling、real_parent、parent以及ptrace()调试进程

LKDS3.Linux内核的双向链表代码解析(3) 进程树遍历(上) 前置知识: children、sibling、real_parent、parent以及ptrace()调试进程

目录 1.内核串联进程的方法 全局的进程链表 父子进程链表 children sibling real_parent和parent的含义和区别 实验1: 普通情况 测试父进程创建子进程父子进程不退出 lx_task_by_pid() 测试父进程创建子进程父进程退出子进程不退出 实验2: 特殊情况: ptrace() ptra…

📅 2026/11/24 9:32:29
对主从复的理解

对主从复的理解

1. 什么是主从复制?作用是什么? 参考答案: 主从复制就是把主节点(master) 的数据,同步到从节点(slave/replica)。 master:负责接收写请求;slave:复制master数据,一般只提供读服务。…

📅 2026/10/16 21:38:48
MORE NEWS

更多资讯

📰

如何查真假低压电工证报考条件与考试时间

电工证太难考?工地太忙根本没时间复习考试,通过率揭秘来了 工地太忙根本没时间复习考试,这是很多低压电工朋友的真实写照。一边要赶工期,一边还得抽空备考特种作业操作证,这几乎是所有一线电工的“痛点”。特别是现在, 如何查真假低压电工证…

📰

电工证去那办理2026河南最新政策怎么考才靠谱

电工证去那办理2026河南最新政策怎么考才靠谱 怕考不过白交培训费,这事儿真不是开玩笑。去年周口就有不少应届工程类毕业生,花了几千块报了个电工证培训班,结果考试挂了,钱打了水漂,还耽误了找工作。2026河南最新政策对电工证的报考流程和考试标…

📰

之前考的证在外省能不能转过来?国家高压电工作业内部消息全知道

之前考的证在外省能不能转过来?国家高压电工作业内部消息全知道 之前考的证在外省能不能转过来?这是很多从业人员的痛点,尤其是常年在外打工、跨省流动的特种作业人员。 内部消息…

📰

宁波电工证报销流程在哪考?别被中介骗了,全流程看这篇就够了

宁波电工证报销流程在哪考?别被中介骗了,全流程看这篇就够了 不知道去哪报名怕被中介坑,这是很多想考电工证的朋友最担心的问题。尤其是在宁波这样的城市,电工证的需求量大,报考人数多,但真正靠谱的报名渠道却少之又少。很多考生一不小心就被中介骗了钱…

📰

工地太忙根本没时间复习考试?电工证去那办理指南来了,全国通用不愁找不到工作

工地太忙根本没时间复习考试?电工证去那办理指南来了,全国通用不愁找不到工作 工地太忙根本没时间复习考试?很多电工朋友都面临这个问题,尤其是刚入行或者刚换工作的人。手头活儿干不完,哪还有空去准备电工证?别急, 电工证去那办理…

📰

强电电工证有效吗官方回应来了 你还在纠结外省证怎么转

强电电工证有效吗官方回应来了 你还在纠结外省证怎么转 之前考的证在外省能不能转过来?这几乎是每个想转行或换城市工作的电工都问过的问题,特别是那些在新乡从事房建工程的朋友,不少人都遇到过类似的困惑。官方回应明确,…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬