What's the difference between chat and channel in telegram? - telethon

What's the difference between chat and channel in telegram?
When a user creates a new conversation in telegram, and maybe change it to a public group, will the type be changed?
by calling client.get_input_entity(url)
A peer to peer chat in telethon is a what type, Channel or Chat?
A private chat group in telethon is a what type, Channel or Chat?
A mega group in telethon is a what type, Channel or Chat?

the id of chat change depending of is an user or chat. If is user chat id be for example: 1233563 and if chat is in group, id chat be like: -233412312. The group chat id is negative

chats are in four type:
private chat (between two user)
group chat (normal groups)
supper groups
channel
when you get a message from telegram with telethon you have three flag:
is_private
is_group
is_channel
meaning of flags:
if is_private = 1 , is_group = 0 , is_channel = 0 chat is private chat
if is_private = 0 , is_group = 1 , is_channel = 0 chat is normal group
if is_private = 0 , is_group = 1 , is_channel = 1 chat is supper group
if is_private = 0 , is_group = 0 , is_channel = 1 chat is channel

Related

SQL Alchemy User <-> Customer relationship, many to one, returns wrong value

I'm trying to create a many to one relationship for a database model where multiple users can exist under one customer.
My code all works within the app but recently I realized when I added a new user record, the relationship returns the wrong customer record based on the relationship.
class User(db.Model):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
customer_id = Column(Integer, ForeignKey('customer.id'),default=None)
aws_sub = Column(String(50),unique=True)
email = Column(String(120), unique=True)
status = Column(Boolean, unique=False,default=False)
access_token = Column(String(2500))
refresh_token = Column(String(2500))
id_token = Column(String(2500))
resend_code = Column(String(32))
date_created = Column(DateTime(timezone=True), server_default=func.now())
date_updated = Column(DateTime(timezone=True), onupdate=func.now())
customer = relationship("Customer", uselist=False, back_populates="users",foreign_keys=[customer_id],remote_side=customer_id)
class Customer(db.Model):
__tablename__ = 'customer'
id = Column(Integer, primary_key=True)
name = Column(String(50), unique=True)
#False is trial account
status = Column(Boolean, unique=False,default=False)
#primary_user = Column(Integer, ForeignKey('users.id'), unique=True)
date_created = Column(DateTime(timezone=True), server_default=func.now())
date_updated = Column(DateTime(timezone=True), onupdate=func.now())
setup_complete = Column(Boolean, unique=False,default=False)
setup_step = Column(Integer, default=1)
users = relationship("User", uselist=False, back_populates="customer")
tickets = relationship("Ticket", uselist=False, back_populates="customer")
api = relationship("API", backref="Customer")
I have two users in the database stored with this model and when I try to retrieve the record on 2nd user record, who does not have a customer_id set (NULL), it incorrectly retrieves the customer_id 1, which is for the first user. There is only one record in the customer table, which corresponds to the first user. The second user has no associated customer record. Shouldn't the customer relationship for the user return null or None?
I'm trying to avoid initializing the parts of the data that aren't needed until they are necessary so the logic only works if data is expected, otherwise it will implicitly fail. The reason I need users AND customers is because I want my application to eventually support multiple user accounts but initially most user accounts will be tied to one "customer" or company account.
I'm using current_user.customer is some of my views with Flask and Flask-login so I want to make sure my views handle null cases or "relationship not yet established".
Users data:
id
customer_id
aws_sub
email
status
access_token
refresh_token
id_token
resend_code
date_created
date_updated
1
1
q34tq34tq34t3q4t
jandoe#yahoo.com
True
xxx...
zzz...
yyy...
xxxx
2021-05-20 04:27:01
2021-05-30 14:37:04
2
NULL
q34tq34tq34t
jsmith#yahoo.com
True
xxx...
xxxxx...
yyy...
zzz
2021-06-04 16:50:00
2021-06-04 16:50:46
Customer table:
id
name
status
date_created
date_updated
setup_complete
setup_step
1
Contoso
False
2021-05-21 03:21:56
2021-05-25 02:16:42
True
NULL
I resolved this. I was passing back_populates on both sides of the model since this is a bi-directional many to one (User -> Customer) relationship. However it seems you can do this by only specifying relationship from one side using backref, in this case:
class Customer(db.Model):
__tablename__ = 'customer'
....
users = relationship("User", uselist=False, backref="customer")
I commented out the existing relationship I had in the many side (user model) and current_user.customer now returns null if the customer_id for that user is null. I remember seeing somewhere that back_populates was newer so I guess I assumed that it was preferred...

How to send email when order status is updated ? prestashop

foreach($order_to_convert as $odr_id) {
$objOrder = new Order($odr_id);
$history = new OrderHistory();
$history->id_order = (int)$objOrder->id;
//$history->changeIdOrderState((int)$sts_id, (int)($objOrder->id));
//$history->add(true);
$history->addWithemail(true);
//$history->save();
}
I have used above code to update order status & send email to customers but due to performance issue i have removed the code above & used query to perform the same. but i am not able to send email to customers.
Is their any way to send customer email when the order status is updated ?

SQLite remove rows two column crossly equal

I'm developing an android chat app. I'm using Room (SQLite persistence library) for storing data.
I have two tables: Friends and Messages. I want to display the latest message for every conversation.
Friends table:
data class FriendsModel(
#PrimaryKey
#SerialName("friendID") var friendID: String,
#SerialName("friendName") var friendName: String?,
#SerialName("friendPublicKey") var friendPublicKey: String?,
#SerialName("sharedSecretKey") var sharedSecretKey: String?
)
Messages table:
data class MessageModel(
#PrimaryKey(autoGenerate = true)
#SerialName("id") var _id: Int = 0,
#SerialName("from") var from: String,
#SerialName("to") var to :String,
#SerialName("message") var message: String?,
#SerialName("date") var date: String?
)
Fake data to give an example:
Notice User1 sends 3 messages to User2, as well as User2 sends 3 messages to User1. I want to get every latest message on every conversation. It doesn't matter who send the last message ('from' or 'to' fields)
Return should be something like this:
I tried to sort by date and group by both from and to. But It gives me last message sent by User1 to User2 and by User2 to User1 at the same time. I want to eliminate this case.

Problems with Facebook fql.query

I have some problems with this fql.query method:
select music
from user
where uid = ......
and music in (select music
from user
where uid = ......
)
I want to obtain common music interest between two users; it works with queries like this
select uid
from user
where uid = ......
and uid in (select uid
from user
where uid = ......
)
I think the problem is that the second query returns an integer and the first one returns a string array. Can anyone help me with this?
(Excuse my bad English! I'm from Spain ;) )
Does not the first query already gives you your answer?
I mean it does the following :
-- Display only music field
select music
from user
where uid = <first_USER> -- Selection of your first user
and music in ( -- We want to macth the string with another
select music -- Selects only the music field
from user
where uid = <second_USER> -- Selection of your second user
)
So you gets a music name that match in two user records.
Your second query seems a bit odd to me :
select uid
from user
where uid = <first_USER?>
and uid in (select uid
from user
where uid = <second_USER?>
)
Which basically translates to find the user_id matching user one and user two. Thing that does never happen since each user has it's own uid.
By the way, I don't know how facebook handles music string, but it may appear that user don't write their favorite music name in a common spelling and/or format. That may impede your matching system.
I tried this:
select music
from user
where uid=UID1
AND music IN (SELECT music from user where uid=UID2)
And it works fine when the UID1 music string matches exactly the UID2's one.

Django DB API equivalent of a somewhat complex SQL query

I'm new to Django and still having some problems about simple queries.
Let's assume that I'm writting an email application. This is the Mail
model:
class Mail(models.Model):
to = models.ForeignKey(User, related_name = "to")
sender = models.ForeignKey(User, related_name = "sender")
subject = models.CharField()
conversation_id = models.IntegerField()
read = models.BooleanField()
message = models.TextField()
sent_time = models.DateTimeField(auto_now_add = True)
Each mail has conversation_id which identifies a set of email messages
which are written and replyed. Now, for listing emails in inbox, I
would like as gmail to show only last email per conversation.
I have the SQL equivalent which does the job, but how to construct native Django query for this?
select
*
from
main_intermail
where
id in
(select
max(id)
from
main_intermail
group by conversation_id);
Thank you in advance!
Does this work? It would require Django 1.1.
from django.db.models import Max
mail_list = Mail.objects.values('conversation_id').annotate(Max('id'))
conversation_id_list = mail_list.values_list('id__max',flat=True)
conversation_list = Mail.objects.filter(id__in=conversation_id_list)
So, given a conversation_id you want to retrieve the related record which has the highest id. To do this use order_by to sort the results in descending order (because you want the highest id to come first), and then use array syntax to get the first item, which will be the item with the highest id.
# Get latest message for conversation #42
Mail.objects.filter(conversation_id__exact=42).order_by('-id')[0]
However, this differs from your SQL query. Your query appears to provide the latest message from every conversation. This provides the latest message from one specific conversation. You could always do one query to get the list of conversations for that user, and then follow up with multiple queries to get the latest message from each conversation.