我正在使用PyTelegramBotApi 库编写一个Telegram 机器人,我想实现检查用户对某个电报频道的订阅的功能,如果没有,则提供订阅。预先感谢您的回答!
试图了解以下问题的原因,我真的很头疼。我们正在使用以下库的组合:
第SQLAlchemy一次使用NullPool,现在配置为使用QueuePool. 我还使用以下习语为每个线程启动一个新的数据库会话(根据我的理解)
Session = sessionmaker(bind=create_engine(classes.db_url, poolclass=QueuePool))
@contextmanager
def session_scope():
session = Session()
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close()
@bot.message_handler(content_types=['document'])
def method_handler:
with session_scope() as session:
do_database_stuff_here(session)
Run Code Online (Sandbox Code Playgroud)
尽管如此,我仍然遇到这个烦人的异常: (sqlite3.ProgrammingError) SQLite objects created in a thread can only be used in that same thread
有任何想法吗?;) 特别是,我不明白如何在 db 操作之间出现另一个胎面......这可能是讨厌的异常的原因
更新 1:如果我将其更改poolclass为SingletonThreadPool,那么似乎不会再出现错误了。但是,文档SQLAlchemy表明它不是生产盛行。
我正在尝试开发一个简单的机器人,它可以从用户那里检索照片,然后对媒体文件进行多项操作。我正在使用 Telebot ( https://github.com/eternnoir/pyTelegramBotAPI ) 进行设计。
就我从 wiki 上看到的而言,我可以通过content_type使用特殊处理程序来划分收入消息。
但是,当我写了这么简单的方法时:
#main.py
@bot.message_handler(content_types= ["photo"])
def verifyUser(message):
print ("Got photo")
percent = userFace.verify(message.photo, config.photoToCompare)
bot.send_message(message.chat.id, "Percentage: " + str(percent))
def getData(json_string):
updates = telebot.types.Update.de_json(json_string)
bot.process_new_updates([updates])
#server.py
app = Flask(__name__)
@app.route("/", methods=["POST", "GET"])
def hello():
json_string = request.get_data()
getData(json_string)
print("....")
print(request.data)
return 'test is runing'
if __name__ == '__main__':
app.run(host='0.0.0.0')
Run Code Online (Sandbox Code Playgroud)
我遇到了这样的错误,我无法归类是我做错了什么还是 API 有问题
obj = cls.check_json(json_type)
File "/usr/local/lib/python2.7/dist-packages/telebot/types.py", line 77, in check_json
return json.loads(json_type)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return …Run Code Online (Sandbox Code Playgroud) 问题是关于使用 pyTelegramBotAPI 模块在 Telegram 机器人中使用 webhook。我正在使用 pythonanywhere.com 来托管机器人。
下面的代码工作正常:
from flask import Flask, request
import telebot
secret = "A_SECRET_NUMBER"
bot = telebot.TeleBot ('YOUR_AUTHORIZATION_TOKEN')
bot.set_webhook("https://YOUR_PYTHONANYWHERE_USERNAME.pythonanywhere.c..
}".format(secret), max_connections=1)
app = Flask(__name__)
@app.route('/{}'.format(secret), methods=["POST"])
def telegram_webhook():
update = request.get_json()
if "message" in update:
text = update["message"]["text"]
chat_id = update["message"]["chat"]["id"]
bot.sendMessage(chat_id, "From the web: you said '{}'".format(text))
return "OK"
Run Code Online (Sandbox Code Playgroud)
但是,当我使用示例中所示的消息处理程序时,我没有收到机器人的答复:
# Process webhook calls
@app.route(WEBHOOK_URL_PATH, methods=['POST'])
def webhook():
if flask.request.headers.get('content-type') == 'application/json':
json_string = flask.request.get_data().decode('utf-8')
update = telebot.types.Update.de_json(json_string)
bot.process_new_updates([update])
return ''
else:
flask.abort(403) …Run Code Online (Sandbox Code Playgroud) flask pythonanywhere telegram telegram-bot py-telegram-bot-api
目前正在使用pyTelegramBotAPI编写我的第一个机器人。我想禁用某些消息的链接预览。我该怎么做呢?
我最近使用 pyTelegramBotAPI (telebot) 创建了一个简单的电报机器人。我添加了一个消息处理程序,该处理程序应该处理每条消息,包括新用户加入时出现在组上的消息,它们仍然Message是非空属性的对象new_chat_members。
import telebot
bot = telebot.TeleBot(TOKEN)
[...]
@bot.message_handler(func=lambda m: True)
def foo(message):
bot.send_message(message.chat.id,"I got the message")
bot.polling()
Run Code Online (Sandbox Code Playgroud)
即便如此,当我添加新用户时,机器人不会回复“我收到消息”字符串,尽管它确实捕获了其他消息。
为什么会发生这种情况?这是消息处理程序的问题吗?是否有一个更通用的处理程序可以确保捕获每个更新?
谢谢
我正在尝试使用PRAW从 Reddit subreddits 检索消息。它在大多数情况下都能正常工作,但我收到以下错误消息:消息太长。我正在使用pytelegrambotApi
片段:
import praw
import telebot
bot = telebot.TeleBot(token)
reddit = praw.Reddit(
client_id=client, #these details are given accordingly and are correct. No errors here.
client_secret=secret,
user_agent="user_agent",
)
def get_posts(sub):
for submission in reddit.subreddit(sub).hot(limit=10):
print(submission)
if submission.author.is_mod:
continue
elif submission.selftext=="":
return submission.title,submission.url
else:
print("It's working")
print(submission.url)
return submission.title,submission.selftext
@bot.message_handler(func=lambda message: True)
def echo_message(message):
subreddit = message.text
title,post = get_posts(subreddit)
m = title + "\n" + post
bot.reply_to(message,m)
bot.infinity_polling()
Run Code Online (Sandbox Code Playgroud)
我正在给机器人写电报。我遇到了这样的问题。我需要机器人在单击它被复制时发送消息(文本)(作为来自@BotFather 的令牌)
pyTelegramBotAPI 版本 - 3.0.1
python版本:2.7/3.6.1
我想创建一个带参数的命令,例如:
/activate 1
/run programm
Run Code Online (Sandbox Code Playgroud)
怎么做?
我正在编写一个 Telegram 机器人。我想在电报频道中搜索特定消息并获取其消息 ID。是否可以?提前谢谢。
python ×8
telegram ×7
telegram-bot ×7
bots ×3
python-3.x ×2
api ×1
arguments ×1
flask ×1
praw ×1
python-2.7 ×1
reddit ×1
sqlalchemy ×1
sqlite ×1